用expect实现shell脚本的自动交互
2010-06-23 12:58
866 查看
用expect实现shell脚本的自动交互
对于复杂的交互,甚至结合屏幕输出的不同进行不同的处理,都是非常有效的。
而且一般的linux、unix都有。非常易用却又很强大
spawn telnet XXX
expect “username”
send “xxxxx/r”
expect “password”
send “xxxx/r”
expect “last login*”
send “xxx”
…
利用expect实现自动交互
Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:
(Expect is a software suite for automating interactive tools。
Expect的官方主页对它作了如下介绍:
Expect is a tool for automating interactive applications such as telnet,
ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff
trivial. Expect is also useful for testing these same applications. And
by adding Tk, you can also wrap interactive applications in X11 GUIs.
Expect是基于TCL的,作为一个脚本语言,expect能在无需管理员参与的情况下实现
自动交互(比如passwd,fsck,telnet等),expect也能用于自动测试一些应用程序。
expect的语法和shell的语法非常相似,它支持函数调用,有while语句,switch
语句。
1) expect使用spawn调用其他的执行程序,比如
spawn telnet 218.199.20.98 2600
但是在使用的过程中发现spawn不支持管道和重定向,也就是说对于
ls |more ; mysql -p < update.sql 这样的命令spawn不能正确解析。
解决的办法是把这些命令放到一个shell脚本里面,在用spawn执行这个shell
脚本。
2) expect 创建子函数使用proc标志,也即:
proc functionname { parameter1,parameter2 } {
......
}
调用子函数非常简单
functionname $param1 $param2
3) expect 使用expect ,send 组合实现自动交互 ,语法如下:
expect {
"login;" { send "$user/n" }
"passwd:" { send "$passwd/n" }
}
使用send的使用后面的内容不显示给用户,如要显示给用户,应使用send_user
4) 注意点:
1. expect里面基本是都是使用{} 而不是使用(),比如函数参数输入外面应用{},
应该是while { } 而不是 while ( ).
2. { 应和其他符合有空格, expect { 正确,expect{ 就会报错.
3. spawn 不支持管道和重定向.
5) 下面是实现的mysql数据库自动更新的expect脚本:
proc do_console_login {pass} {
set timeout 5
set done 1
while { $done } {
expect {
"password:" {
send "$pass/n"
}
eof {
set done 0
}
}
}
}
if {$argc<1} {
puts stderr "Usage: $argv0 password./n "
exit 1
}
set PASS [lindex $argv 0]
spawn /usr/local/mysql/data/updatemysql
do_console_login $PASS
对于复杂的交互,甚至结合屏幕输出的不同进行不同的处理,都是非常有效的。
而且一般的linux、unix都有。非常易用却又很强大
spawn telnet XXX
expect “username”
send “xxxxx/r”
expect “password”
send “xxxx/r”
expect “last login*”
send “xxx”
…
利用expect实现自动交互
Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:
(Expect is a software suite for automating interactive tools。
Expect的官方主页对它作了如下介绍:
Expect is a tool for automating interactive applications such as telnet,
ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff
trivial. Expect is also useful for testing these same applications. And
by adding Tk, you can also wrap interactive applications in X11 GUIs.
Expect是基于TCL的,作为一个脚本语言,expect能在无需管理员参与的情况下实现
自动交互(比如passwd,fsck,telnet等),expect也能用于自动测试一些应用程序。
expect的语法和shell的语法非常相似,它支持函数调用,有while语句,switch
语句。
1) expect使用spawn调用其他的执行程序,比如
spawn telnet 218.199.20.98 2600
但是在使用的过程中发现spawn不支持管道和重定向,也就是说对于
ls |more ; mysql -p < update.sql 这样的命令spawn不能正确解析。
解决的办法是把这些命令放到一个shell脚本里面,在用spawn执行这个shell
脚本。
2) expect 创建子函数使用proc标志,也即:
proc functionname { parameter1,parameter2 } {
......
}
调用子函数非常简单
functionname $param1 $param2
3) expect 使用expect ,send 组合实现自动交互 ,语法如下:
expect {
"login;" { send "$user/n" }
"passwd:" { send "$passwd/n" }
}
使用send的使用后面的内容不显示给用户,如要显示给用户,应使用send_user
4) 注意点:
1. expect里面基本是都是使用{} 而不是使用(),比如函数参数输入外面应用{},
应该是while { } 而不是 while ( ).
2. { 应和其他符合有空格, expect { 正确,expect{ 就会报错.
3. spawn 不支持管道和重定向.
5) 下面是实现的mysql数据库自动更新的expect脚本:
proc do_console_login {pass} {
set timeout 5
set done 1
while { $done } {
expect {
"password:" {
send "$pass/n"
}
eof {
set done 0
}
}
}
}
if {$argc<1} {
puts stderr "Usage: $argv0 password./n "
exit 1
}
set PASS [lindex $argv 0]
spawn /usr/local/mysql/data/updatemysql
do_console_login $PASS
相关文章推荐
- shell脚本调用expect命令实现命令行自动交互
- shell脚本调用expect命令实现命令行自动交互
- shell脚本通过expect实现自动单边无密登录
- shell自动交互之expect脚本_转
- shell脚本实现无密码交互的SSH自动登陆
- shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化
- Linux基于expect(tcl)实现shell自动交互
- 使用expect实现shell脚本中scp,ssh自动输入口令
- 无密钥登录的自动脚本实现(ssh-copy-id、expect免交互输入脚本)
- 使用expect实现shell自动交互
- shell+expect实现多台服务器自动运行脚本。
- 用expect实现的自动登录到多台服务器的shell脚本
- 用expect实现的自动登录到多台服务器的shell脚本
- 使用expect实现shell自动交互
- Shell脚本中调用expect,实现自动输入密码的功能
- shell脚本实现无密码交互的SSH自动登陆
- 使用expect实现shell自动交互
- 无密钥登录的自动脚本实现(ssh-copy-id、expect免交互输入脚本)
- shell脚本实现无密码交互的SSH自动登陆
- 使用expect实现shell自动交互