简单的expect脚本
时间:2010-07-16 来源:lxl0121
1.
cat ssh.exp
#!/usr/bin/expect -f
set HOST [lindex $argv 0]
set PASS "your-password"
spawn ssh $HOST
set timeout 10
expect {
"*(yes/no)*" { send "yes\r" }
"*password*" { send "${PASS}\r" }
}
expect "*$"
send "mkdir /tmp/test\r"
send "exit\r"
interact
使用:
expect ssh.exp your-ip 或者 ./ssh.exp your-ip
作用:
登录 your-ip 后执行 mkdir /tmp/test 退出
2.
cat scp.exp
#!/usr/bin/expect -f
set CMD0 [lindex $argv 0]
set CMD1 [lindex $argv 1]
set PASS "your-password"
spawn scp -r $CMD0 $CMD1
set timeout 10
expect {
"*(yes/no)*" { send "yes\r" }
"*password*" { send "${PASS}\r" }
}
interact
使用:
expect scp.exp root@your-ip:/tmp/test /tmp
expect scp.exp /tmp/test root@your-ip:/tmp/
用途:
将your-ip /tmp/test 目录scp到本机 /tmp
将本机 /tmp/test 目录scp到 your-ip /tmp
cat ssh.exp
#!/usr/bin/expect -f
set HOST [lindex $argv 0]
set PASS "your-password"
spawn ssh $HOST
set timeout 10
expect {
"*(yes/no)*" { send "yes\r" }
"*password*" { send "${PASS}\r" }
}
expect "*$"
send "mkdir /tmp/test\r"
send "exit\r"
interact
使用:
expect ssh.exp your-ip 或者 ./ssh.exp your-ip
作用:
登录 your-ip 后执行 mkdir /tmp/test 退出
2.
cat scp.exp
#!/usr/bin/expect -f
set CMD0 [lindex $argv 0]
set CMD1 [lindex $argv 1]
set PASS "your-password"
spawn scp -r $CMD0 $CMD1
set timeout 10
expect {
"*(yes/no)*" { send "yes\r" }
"*password*" { send "${PASS}\r" }
}
interact
使用:
expect scp.exp root@your-ip:/tmp/test /tmp
expect scp.exp /tmp/test root@your-ip:/tmp/
用途:
将your-ip /tmp/test 目录scp到本机 /tmp
将本机 /tmp/test 目录scp到 your-ip /tmp
相关阅读 更多 +