(5)验证:
[root@linuxidc ~]# ./sshkey.exp 192.168.1.201 root PASSWORD |grep ssh-rsa >> ~/.ssh/authorized_keys
[root@linuxidc ~]# ./noscp.exp ~/.ssh/authorized_keys 192.168.1.201:~/.ssh root PASSWORD
spawn scp /root/.ssh/authorized_keys root@192.168.1.201:~/.ssh
root@192.168.1.201's password:
authorized_keys
这样,就能无密码登陆了!
[root@linuxidc ~]# ssh 192.168.1.201
Last login: Fri Sep 23 18:33:21 2016 from 192.168.1.7
[root@vm-002 ~]#
--------------------------------------------------------------------------
如果是多台机器的话,可以结合shell脚本进行批量执行
[root@linuxidc ~]# cat /root/ip.list
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
......
......
[root@linuxidc ~]# cat sshkey.sh
#!/bin/bash
user='root'
password='PASSWORD'
for ip in `cat /root/ip.list`
do
/root/sshkey.exp $ip $user $password |grep ssh-rsa >> ~/.ssh/authorized_keys
/root/noscp.exp ~/.ssh/authorized_keys $user@$ip:~/.ssh root PASSWORD
done