分类: LINUX
2013-02-24 23:57:51
平时维护机器比较多时候,每登录一台需要输入密码,是无聊工作,这里有个常用的脚本,这里脚本网上比较多。
输入 expect go2.exp 192.168.1.104,也可以将 go2.exp 放在path路径下,输入更省事点
#!/usr/bin/expect ##author:stevenrao ##打开 ssh 终端,省去密码交互脚本 if { $argc==0 } { send "usage: $argv0 ip \[port\]\n"; exit } #要打开的远程机器ip 端口 set ip [lindex $argv 0] set port 22 if { $argc==2 } { set port [lindex $argv 1] } #日志文件,一ip为文件名 log_file $ip log_user 1 #用户名密码 set user stevenrao set password stevenrao #启动ssh登录进程 spawn /usr/bin/ssh -p $port $user@${ip} set myid $spawn_id set timeout 10 while { [string length $myid] } { #提示符 set prompt "(%|#|\\$|>|\\])" catch {set prompt $env(EXPECT_PROMPT)} expect { "*tcsetattr failed*" { send_log "\n${ip},sshsyscallerror\n"; break } "*Authentication failed*" { break } "*yes/no*" { send "yes\n" } "*password: " { send "$password\n" } timeout { send_log "\n${ip},logintimeout\n" exit } -re $prompt { set timeout 1 send_log "\n${ip},loginsuccess\n" ##登录成功,这里放你想要执行的交互脚本 send "/bin/ls -l\n" interact;exit expect eof { send_log "eof\n" exit } } } }