linux expect自动登录ssh

 

#!/usr/bin/expect
# 传送文件并且执行
set timeout 30

set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set file [lindex $argv 3]
set center_password [lindex $argv 4]

spawn scp -P22 $file $username@$host:/tmp/tmp.sh
sleep 1
expect {
 "*yes/no*" {send "yes\n"; exp_continue}
 "*password:*" {send "$password\n" }
 }
interact

spawn ssh -p22 $username@$host
sleep 1
expect {
 "*yes/no*" {send "yes\n"; exp_continue}
 "*password:*" {send "$password\n" }
 }
sleep 1
expect "*$*"
send "sh /tmp/tmp.sh \n"
send "exit\n"
#interact 
expect eof

   

参考

http://blog.51yip.com/linux/1462.html

updatedupdated2024-12-222024-12-22