交互shell—expect(部署ssh登录RSA认证)

#!/usr/bin/expect -f

# usage: ./shared_ssh_key.exp host root's password
# example: ./shared_ssh_key.exp 192.168.1.2 rootroot
set user root
set pubkeyfile /root/.ssh/id_rsa.pub
set des_keydir /root/.ssh
set ipaddr [lindex $argv 0]
set _pw [lindex $argv 1]
set timeout -1
spawn ssh-keygen -t rsa
expect {
".ssh/id_rsa" {send "\r"; exp_continue}
"Overwrite" {send "y\r"; exp_continue}
"passphrase):" {send "\r"; exp_continue}
"again:" {send "\r"; exp_continue}
}

spawn scp $pubkeyfile $user@$ipaddr:/tmp/key
match_max 100000
expect {
"(yes/no)" { send "yes\r"; exp_continue }
"password:" { send "$_pw\r"; exp_continue}
}

spawn ssh -q -t $user@$ipaddr "mkdir -p $des_keydir && cat /tmp/key >> $des_keydir/authorized_keys && chmod 600 $des_keydir/authorized_keys"
expect {
"(yes/no)?" { send "yes\r"; exp_continue}
"password:" { send "$_pw\r"; exp_continue}
}

posted @ 2013-04-27 21:52  596448745  阅读(398)  评论(0编辑  收藏  举报