autoexpect让expect脚本更加简单!
时间:2010-03-02 来源:lonelysoul011
由于工作需要写一些自动化测试脚本。而BASH脚本却无法做到完全自动化,比如ssh连接时。而expect提供了完全自动化功能。苦于刚接触expcet,只好用autoexpect替代。
autoexpect能够自动录制脚本。不需手动编辑,只要将需要执行的步骤执行一遍就可以录制.
下面介绍一个最简单的用法。
我需要自动SSH登录到192.168.1.210,可以使用下面步骤:
[root@localhost tmp]# autoexpect -p -f ssh.exp
--开始脚本录制 将要执行的步骤保存到ssh.exp中。
autoexpect started, file is ssh.exp
[root@localhost tmp]# ssh 192.168.1.210
[email protected]'s password:
Last login: Wed Mar 3 03:21:38 2010 from 192.168.1.210
[root@localhost ~]# echo $$
3724
[root@localhost ~]# exit
logout
Connection to 192.168.1.210 closed.
[root@localhost tmp]# exit
exit
autoexpect done, file is ssh.exp 生成的expect脚本主体程序如下: set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]0;root@localhost:/tmp\[root@localhost tmp\]# "
send -- "ssh 192.168"
expect -exact [K"
send -- ".1.210\r"
expect -exact "[email protected]'s password: "
send -- "huawei\r"
expect -exact "]0;root@localhost:~\[root@localhost ~\]# "
send -- "echo \$\$\r"
expect -exact "]0;root@localhost:~\[root@localhost ~\]# "
send -- "exit\r"
expect -exact "]0;root@localhost:/tmp\[root@localhost tmp\]# "
send -- "exit\r"
expect eof
autoexpect started, file is ssh.exp
[root@localhost tmp]# ssh 192.168.1.210
[email protected]'s password:
Last login: Wed Mar 3 03:21:38 2010 from 192.168.1.210
[root@localhost ~]# echo $$
3724
[root@localhost ~]# exit
logout
Connection to 192.168.1.210 closed.
[root@localhost tmp]# exit
exit
autoexpect done, file is ssh.exp 生成的expect脚本主体程序如下: set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]0;root@localhost:/tmp\[root@localhost tmp\]# "
send -- "ssh 192.168"
expect -exact [K"
send -- ".1.210\r"
expect -exact "[email protected]'s password: "
send -- "huawei\r"
expect -exact "]0;root@localhost:~\[root@localhost ~\]# "
send -- "echo \$\$\r"
expect -exact "]0;root@localhost:~\[root@localhost ~\]# "
send -- "exit\r"
expect -exact "]0;root@localhost:/tmp\[root@localhost tmp\]# "
send -- "exit\r"
expect eof
相关阅读 更多 +