* 測試環(huán)境:Red Hat Enterprise Linux Server release 6.6 (Santiago)
1、創(chuàng)建密鑰
? ? ? ? [root@localhost ~]# ssh-keygen
? ? ? ? Generating public/private rsa key pair.
? ? ? ? Enter file in which to save the key (/home/alei/.ssh/id_rsa):
? ? ? ? Created directory '/home/alei/.ssh'.
? ? ? ? Enter passphrase (empty for no passphrase):
? ? ? ? Enter same passphrase again:
? ? ? ? Your identification has been saved in /home/alei/.ssh/id_rsa.
? ? ? ? Your public key has been saved in /home/alei/.ssh/id_rsa.pub.
? ? ? ? The key fingerprint is:
? ? ? ? e2:82:86:66:71:58:f1:2c:68:a0:aa:b6:ea:a2:02:a5 alei@localhost.localdomain
? ? ? ? The key's randomart image is:
? ? ? ? +--[ RSA 2048]----+
? ? ? ? |.? .? ? ? ? ? ? |
? ? ? ? |o . +? ? ? ? ? ? |
? ? ? ? |.o o o? ? ? ? ? |
? ? ? ? |o + .? ? ? ? ? ? |
? ? ? ? |.= .? . S? ? ? ? |
? ? ? ? |E.o. . .? ? ? ? |
? ? ? ? |++o . .? ? ? ? ? |
? ? ? ? |*..? .? ? ? ? ? |
? ? ? ? |Oo? ? ? ? ? ? ? |
? ? ? ? +-----------------+
2、安裝Expect工具
??????? ?A:搜索expect工具
????????????? [alei@localhost ~]$ yum search expect
????????????? ....
????????????? pexpect.noarch : Pure Python Expect-like module
????????????? expect.x86_64 : A program-script interaction and testing utility
????????????? expect-devel.i686 : A program-script interaction and testing utility
????????????? expect-devel.x86_64 : A program-script interaction and testing utility
????????????? expectk.x86_64 : A program-script interaction and testing utility
????????????? ....
? ? ? ? B:安裝expect工具
? ? ? ? ? ? ? [alei@localhost ~]# yum install? expect.x86_64? -y
????????????? ...
????????????? Resolving Dependencies
????????????? --> Running transaction check
????????????? ---> Package expect.x86_64 0:5.45-14.el7_1 will be installed
????????????? --> Finished Dependency Resolution
????????????? ...
????????????? Running transaction
??????????????? Installing : expect-5.45-14.el7_1.x86_64????????????????????????????????? 1/1
??????????????? Verifying? : expect-5.45-14.el7_1.x86_64????????????????????????????????? 1/1
????????????? Installed:
??????????????? expect.x86_64 0:5.45-14.el7_1????????????????????????????????????????
????????????? Complete!
3胳嘲、開發(fā)交互腳本
? ? ? ? ? ? ? [alei@localhost ~]# vim run.exp
????????????? #!/usr/bin/expect
????????????? set username [lindex $argv 0]
????????????? set userpwd [lindex $argv 1]
????????????? set serverip [lindex $argv 2]
????????????? set timeout 30
????????????? spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub $username@$serverip
????????????? expect {
????????????????????? timeout exit
????????????????????? yes/no? {send "yes\r";exp_continue}
????????????????????? password {send "$userpwd\r"}
????????????? }
????????????? interact
4痛阻、開發(fā)執(zhí)行腳本
? ? ? ? ? ? ? [alei@localhost ~]# vim run.sh
? ? ? ? ? ? ? #!/bin/sh
? ? ? ? ? ? ? if [ -z $* ];then
? ? ? ? ? ? ? ? ? ? ? echo "Usage: $0 <ipfile>"
? ? ? ? ? ? ? ? ? ? ? exit
? ? ? ? ? ? ? fi
? ? ? ? ? ? ? ipspath="$1"
? ? ? ? ? ? ? username="alei"
? ? ? ? ? ? ? userpwd="alei"
? ? ? ? ? ? ? successfile="./success"
? ? ? ? ? ? ? failedfile="./failed"
? ? ? ? ? ? ? rm -rf $successfile
? ? ? ? ? ? ? rm -rf $failedfile
? ? ? ? ? ? ? for ip in $(cat $ipspath)
? ? ? ? ? ? ? do
? ? ? ? ? ? ? ? ? ? ? echo "<<<<<<<<<<<<<<<<<<<<<<<<<$ip<<<<<<<<<<<<<<<<<<<<"
? ? ? ? ? ? ? ? ? ? ? ./run.exp $username $userpwd $ip
? ? ? ? ? ? ? ? ? ? ? if [ $? -eq 0 ];then
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo $ip >> $successfile
? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? echo $ip >> $failedfile
? ? ? ? ? ? ? ? ? ? ? fi
? ? ? ? ? ? ? done
5葡粒、準備要設置無密登錄的服務器
????????????? [alei@localhost ~]# vim ips_list
????????????? 192.168.0.10
????????????? 192.168.0.11
????????????? 192.168.0.12
????????????? 192.168.0.13
????????????? 192.168.0.14
????????????? 192.168.0.15
????????????? 192.168.0.16
6逼肯、開始設置無密登錄
????????????? [alei@localhost ~]$ sh run.sh ips_list
????????????? <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.10<<<<<<<<<<<<<<<<<<<<
????????????? spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub alei@192.168.0.10
????????????? alei@192.168.0.10's password:
? ? ? ? ? ? ? Now try logging into the machine, with "ssh 'alei@192.168.0.10'", and check in:
???????????? .ssh/authorized_keys
????????????? to make sure we haven't added extra keys that you weren't expecting.
????????????? <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.11<<<<<<<<<<<<<<<<<<<<
???????????? spawn ssh-copy-id -i /home/alei/.ssh/id_rsa.pub alei@192.168.0.11
???????????? alei@192.168.0.11's password:
???????????? Now try logging into the machine, with "ssh 'alei@192.168.0.11'", and check in:
???????????? .ssh/authorized_keys
???????????? to make sure we haven't added extra keys that you weren't expecting.
7肃弟、注意事項
? ? ? ? ? A:如果執(zhí)行腳本時發(fā)生以下錯誤玷室,檢查run.exp文件的執(zhí)行權限(alei@localhost ~]# ll run.exp ),若沒有需要添加([alei@localhost ~]# chmod +x? run.exp)
??????????????? <<<<<<<<<<<<<<<<<<<<<<<<<192.168.0.10<<<<<<<<<<<<<<<<<<<<
??????????????? run.sh: line 16: ./run.exp: Permission denied
?????????? B:如果使用以上腳本信息笤受,copy時每行前綴空格必須清除