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

 

 

 

posted @ 2020-10-18 20:23  六月OvO  阅读(110)  评论(0编辑  收藏  举报