首先安装expect

yum install expect
yum install expect-devel
yum install tcl

ssh-keygen -t rsa -P '' -f /home/dp/.ssh/id_rsa

cat /home/dp/.ssh/id_rsa.pub >/home/dp/.ssh/authorized_keys

#上面是自动生成公钥和私钥。不需要三次回车

chmod 600 /home/dp/.ssh/id_rsa
chmod 644 /home/dp/.ssh/authorized_keys

cat /home/dp/ip.txt|while read line   //里面有账号与密码

do

ip=`echo $line|awk '{print $1}'` #把赋给ip
echo $ip
pw=`echo $line|awk '{print $2}'` #把密码赋给变量pw
echo $pw
command1="ssh-copy-id -i /home/dp/.ssh/authorized_keys dp@$ip:/home/dp/"  #这里使用-i指定文件,和使用的绝对路径,就是怕其他错误的麻烦。这个命令复制公钥比使用scp简单。

expect -c "

spawn $command1;

expect {

\"password:\" {send\"$pw\r\"; exp_continue}

\"connecting(yes/no)?\" {send \"yes\r\"; exp_continue}

}

"
done

 

ssh -t -p 22 dp@$ip 'sed -i"s/^#RSAAuthentication\ yes/RSAAuthentication\ yes/g"/etc/ssh/sshd_config'

ssh -t -p 22 dp@$ip 'sed -i"s/^#PubkeyAuthentication\ yes/PubkeyAuthentication yes/g"/etc/ssh/sshd_config'

ssh -t -p 22 dp@$ip 'sed -i"s/^#PermitRootLogin\ yes/PermitRootLogin\ yes/g"/etc/ssh/sshd_config

chmod 644 /home/dp/.ssh/authorized_keys

'

 

posted on 2017-04-10 19:15  流浪在伯纳乌  阅读(1212)  评论(0编辑  收藏  举报