* 测试环境:Red Hat Enterprise Linux Server release 6.6 (Santiago)
1、创建密钥
[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、开发交互脚本
[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、开发执行脚本
[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:如果执行脚本时发生以下错误,检查run.exp文件的执行权限(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时每行前缀空格必须清除