(1)安装expect-5.42.1-1.x86_64.rpm,可以用 yum -y install 安装最新版本的 expect
(2)
#!/bin/bash
set timeout 30
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
echo $i
expect<<-END
spawn ssh root@$i ""
expect "Are you sure you want to continue connecting (yes/no)? "
send "yes\n"
expect eof
exit
END
done
(3)
#!/bin/bash
set timeout 30
expect<<-END
spawn ssh-keygen -t rsa
expect "Enter file in which to save the key (/root/.ssh/id_rsa): "
send "\n"
expect "Overwrite (y/n)? "
send "\n"
expect eof
exit
END
for i in `cat ./serverlist.ini |grep "="|awk -F= '{print $2}'`
do
# echo $i
expect<<-END
# spawn scp /root/.ssh/id_rsa.pub root@192.168.1.2:.ssh/id_rsa.pub
# expect "password: "
# send "km@123\n"
spawn ssh root@$i "mkdir /root/.ssh/"
expect "password: "
send "123456789\n"
expect eof
exit
END
expect<<-END
spawn scp /root/.ssh/id_rsa.pub root@$i:/root/.ssh/id_rsa.pub
expect "password: "
send "123456789\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "touch /root/.ssh/authorized_keys"
expect "password: "
send "123456789\n"
expect eof
exit
END
expect<<-END
spawn ssh root@$i "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
expect "password: "
send "123456789\n"
expect eof
exit
END
# #scp *.sh $i:/root/
done
#set timeout 30
#spawn ssh root@127.0.0.1
#expect "password: "
#send "df@123\n"
#expect {
# "(yes/no)?" {
# send "yes\n"
#
# }
# "pwd:"{
# ...
# }
#}
#expect{
# "yes/no" {send "yes\r"; exp_continue}
# "pwd:{send"abc\n"}"
#}
阅读(1059) | 评论(0) | 转发(0) |