expect交互编程

安装

1
2
Ubuntu:apt-get install expect
mac: brew install homebrew/dupes/expect

设置变量

1
set password  rootddd

获取参数

1
set filename [lindex $argv 0]

一般格式

开头一定要是expect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/expect

set filename [lindex $argv 0]
set password example_pass
# 开始执行命令
spawn ssh root@example.com
# 匹配(如果包含,则执行下面的send)
expect "password:"
# \r回车
send "$password\r"
send "ls\r"
send "exit\r"
# 结束标志
interact