标签: expect语法编程注意事项 | 分类: perl,cgi,shell脚本编程 |
[root@str0121bk RPMS]# rpm -ivh expect-5.38.0-74.i386.rpm warning: expect-5.38.0-74.i386.rpm: V3 DSA signature: NOKEY, key ID db42a60e error: Failed dependencies: |
expect-5.43.0-sol10-x86-local.gz Expect is a program that performs programmed dialogue with other interactive programs - installs in /usr/local. You should also get tcl-8.5a4 , tk-8.5a4 and, if you don't have gcc installed, libgcc. |
expect "*$*" send "exit\n" | expect { |
spawn ftp 10.1.1.1 ... expect timeout exit 由于expect脚本是顺序执行的,那么当程序执行到这个expect timeout的时候就会阻塞,程序会一直等待到timeout然后退出 | timeout exit Connect } 换成括号,timeout只要被辨认,就会执行后面的exit |
set timeout 3 |
$ ./expecttest spawn ftp 192.168.1.11 ftp: connect: Connection refused ftp> macg Invalid command ftp> 008421 bin Invalid command ftp> ftp> Not connected. ftp> get asian_0264_4.mpg Not connected. ftp> quit success |
for {} {1} {} { expect eofbreak "*UNREF FILE*CLEAR?"{send "r "} "*BAD INODE*FIX?"{send "y "} "*?"{send "n "} } | for {} {1} {}{ expect eofbreak "*UNREF FILE*CLEAR?"{send "y "} "*BAD INODE*FIX?"{send "y "} "*?"{interact +} } |
expect "*[test*" 想匹配[test@localhost ~]$ |
系统报错 missing close-bracket "expect "*[test*" |
expect "ftp>" send "bin\n" exec sleep 1 send "get asian_0264_4.mpg\n" exec sleep 1 expect "ftp>" send "quit\n" ftp> get asian_0264_4.mpg local: asian_0264_4.mpg remote: asian_0264_4.mpg 227 Entering Passive Mode (192,168,1,11,4,10). 150 Opening data connection for asian_0264_4.mpg. quit 226 Transfer complete. |
expect "ftp>" send "bin\n" exec sleep 1 send "get asian_0264_4.mpg\n" exec sleep 4 expect "ftp>" send "quit\n" ftp> bin 200 Type set to I. ftp> get asian_0264_4.mpg local: asian_0264_4.mpg remote: asian_0264_4.mpg 227 Entering Passive Mode (192,168,1,11,4,11). 150 Opening data connection for asian_0264_4.mpg. 226 Transfer complete. 1269764 bytes received in 1.4 seconds (8.6e+02 Kbytes/s) ftp> quit 221 Goodbye! |
#!/usr/bin/expect set timeout 3 spawn ftp 192.168.1.11 expect "*Name*" exec sleep 1 send "macg\n" expect "*Password:*" exec sleep 1 send "008421\n" send "\n" expect "ftp>" send "bin\n" expect "*set to I*" send "get asian_0264_4.mpg\n" expect "*complete*" expect 合理的上一条指令的输出,好send下一条指令 send "quit\n" expect "*$*" send_user "success\n" |
$ ./expecttest spawn ftp 192.168.1.11 Connected to 192.168.1.11. 221 Goodbye! 500 'AUTH': command not understood. 500 'AUTH': command not understood. KERBEROS_V4 rejected as an authentication type Name (192.168.1.11:macg): macg 331 Password required for macg. Password: 230 User macg logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp> ftp> bin 200 Type set to I. ftp> get asian_0264_4.mpg local: asian_0264_4.mpg remote: asian_0264_4.mpg 227 Entering Passive Mode (192,168,1,11,4,70). 150 Opening data connection for asian_0264_4.mpg. 226 Transfer complete. 1269764 bytes received in 1.5 seconds (8.4e+02 Kbytes/s) ftp> quit 221 Goodbye! success |
$ vi expecttest #!/usr/bin/expect set timeout 3 spawn telnet 192.168.1.12 expect "*login:*" exec sleep 1 send "test\n" expect "*Password:*" exec sleep 1 send "008421\n" expect "*$*" |