作用:
把当前机器的ssh公钥拷贝到其它机器。以实现从当前机器能够ssh无password登陆到其它机器
用法:
1)把要加入的ip地址写入到一个文件里。比方host
2)把脚本保存为sh文件,如 install_ssh.sh 设置可运行权限
3)改写脚本中的password设置。眼下仅仅支持固定几个password。
4) 运行命令
sh install_ssh.sh <host
代码:
#!/bin/sh
auto_ssh_copy_id() {
local HOST=$1
local PASSWD=$2
expect -c "set timeout -1;
spawn ssh-copy-id root@$HOST
expect {
*publickey* {exit 1;}
*(yes/no)* {send yes\r; exp_continue;}
*password* {send \"$PASSWD\r\"; exp_continue;}
** {send \"$PASSWD\r\"; exp_continue;}
}";
}
#eof {exit 0;}
passwd1='noSafeNoWork@2014'
passwd2='SafetyFirst@426'
passwd3='ddd@2008'
passwd4='123456'
passwd5='asdasd'
passwd6='12345'
while read line; do
echo "$line" >&2
isSucceed='FALSE'
for pw in $passwd1 $passwd2 $passwd3 $passwd4 $passwd5 $passwd6
do
if auto_ssh_copy_id "$line" $pw
then
isSucceed='TRUE'
break;
fi
done
if [ $isSucceed != 'TRUE' ]; then
echo 'NOTSUCC' $line
fi
done