分类: LINUX
2009-05-22 00:47:23
#!/usr/bin/expect -f
if {$argc<2} {
puts stderr “Usage: $argv0 host user passwaord timeout”
exit 1
}
set HOST [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWD [lindex $argv 2]
set TIMEOUT [lindex $argv 3]
set timeout $TIMEOUT
spawn ssh -l $USER $HOST
# 判断是否是第一次登录
expect_before “no)?” {
send “yes\r” }
sleep 1
# 输入密码
expect “password:”
send “$PASSWD\r”
#如果想直接进入交互模式,可以直接用下面这句
#interact
#自动在远端服务器执行命令
#判断提示符
sleep 2
expect “*#”
send “mkdir /blog.zhangjianfeng.com/tmp/abcd -p\r”
send “ls -l\r”
expect “*#”
exit
## END ##
如果需要调用多台主机,可以写一个shell脚本来调用上面的脚本expect.sh
#cat callexpect.sh
#!/bin/bash
TIMEOUT=2
for i in `cat list`
do
HOST=${i%% *}
USER=`echo $i | awk -F [\ :] ‘{print $2}’`
PASS=${i##*:}
./expect.sh $HOST $USER $PASS $TIMEOUT
done
#list文件格式是 ip:user:passwd