expect用法
远程登录并做一些事情
[root@m01 /shell4]# vim expect.sh
#!/usr/bin/env expect
set ip 172.16.1.7
set pass 1
set timeout 5
#开启一个程序
spawn ssh $ip
#捕获相关内容
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"password:" { send "$pass\r" }
}
expect "#"
send "rm -rf /tmp/*\r"
send "hostname\r"
send "date +%F\r"
send "touch /tmp/file{1..3}\r"
expect eof
传递参数
[root@m01 /shell4]# vim expect.sh
#!/usr/bin/env expect
set ip [ lindex $argv 0 ]
set pass [ lindex $argv 1 ]
set timeout 5
#开启一个程序
spawn ssh $ip
#捕获相关内容
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"password:" { send "$pass\r" }
}
expect "#"
send "rm -rf /tmp/*\r"
send "hostname\r"
send "date +%F\r"
send "touch /tmp/file{1..3}\r"
expect eof
本文来自博客园,作者:六月OvO,转载请注明原文链接:https://www.cnblogs.com/chenlifan/p/13836937.html