#############################################
#
# Expect 拷贝ssh公钥到远端机器,实现无key登录
#
#
# 2010.04.46 V 1.1
# by hu
#
#############################################
1. 一个主机列表 "host.list", 以行记录登录主机IP PWD
2. 一个被调用的expect脚本"key.exp",用来实现非人工方式的拷贝公钥
3. 一个主shell脚本"ssh.sh",用来实现轮询IP,去执行调用expect脚本
##############
#
# host.list
#
##############
192.168.14.220 123abc
192.168.14.221 456def
##############
#
# ssh.sh
#
##############
#!/bin/bash
for i in $(cat /root/host.list|cut -f1);do
/root/key.exp $i $(cat /root/host.list|cut -f2)
done
##############
#
# key.exp
#
##############
log_file /root/exp.log
# set env
set host [lindex $argv 0]
set name root
set password [lindex $argv 1]
# copy id_rsa.pub to remote
spawn scp /root/.ssh/id_rsa.pub $name@$host:/root/tmp
expect {
"(yes/no)?" { send "yes\n"; exp_continue }
"password:" { send "$password\n" }
}
expect "100%"
send "exit\n"
# ssh to doing
spawn ssh $name@$host
expect {
"(yes/no)?" { send "yes\n"; exp_continue }
"password:" { send "$password\n" }
}
expect "#"
send "mkdir /root/.ssh\n"
expect "#"
send "cat /root/tmp >> /root/.ssh/authorized_keys\n"
expect "#"
send "exit\n"
阅读(1105) | 评论(1) | 转发(0) |