- 论坛徽章:
- 0
|
我的需求是这样的,写一个脚本自动登录到网络设备,执行几条命令,然后将其中的一些文件put到ftp_server上。里面有一个问题是在执行一条命令时,需要等待一段时间,这个时间不固定。
下面是我写的脚本及出现的问题,初次接触linux脚本,请各位大侠帮忙看看该怎么弄,谢谢!!:
设备名称脚本:list.conf
sysname1 192.168.170.2 h3c h3c
执行脚本:test.sh
#! /bin/sh
while read ip
do
./test.exp $ip
done < list.conf
命令脚本:test.exp
#!/usr/bin/expect
set sysname [lindex $argv 0]
set ip [lindex $argv 1]
set username [lindex $argv 2]
set password [lindex $argv 3]
#set superpassword [lindex $argv4]
set ftpserver 192.168.170.46
set ftpuser h3c
set ftppasswd h3c
log_file /root/log/$ip.log
spawn telnet $ip
expect "*name:"
send "$username\r"
expect "*word:"
send "$password\r"
expect "*>"
send "screen-length disable\r"
expect "*>"
send "sys\r"
expect "*]"
#save logbuffer
send "logfile save\r"
expect "*]"
send "_h\r"
expect "cmd]"
#save diagfile buffer
send "_diagnose-file save\r"
expect "*]"
#save default.diag
前面这些都执行的比较顺利,下面可以执行完命令,由于这里的display diagnostic-information需要一段时间,这里不知道等多久,使用expect等待字符也没有用直接回跳到下一个ip再执行上面的动作
个人的感觉应该是系统会有一个超时机制,由于这里执行命令需要一段时间(但是这个时间不固定没办法用timeout来等),超过了这个超时时间,命令自动向下执行了,但是不知道怎么回继续往下面expect下面执行,感觉只要前面出现过,就算执行过一样。。。这里搞了个八小时,还是没搞定,哪个大侠给指条明路吧,感谢!!
send "display diagnostic-information\r"
expect "*/N]:"
send "y\r\r\r"
expect {
"*N]:" {send "y\r"}
"successfully." {}
}
expect "*]"
send "quit\r"
#access CF Card to send files to FTP Server
expect "*>"
send "cd cfa1:/\r"
expect "cfa1: FAT*"
send "cd logfile/\r"
expect "*>"
send "ftp $ftpserver\r"
expect "*e)):"
send "$ftpuser\r"
expect "*assword:"
send "$ftppasswd\r"
expect "*tp]"
send "mkdir $sysname\r"
expect "*successfully"
send "\r\r"
send "put logfile1.log\r"
expect "*tp]"
send "put /diaglog/diagfile1.log\r"
expect "*tp]"
send "put /default.diag\r"
expect "*tp]"
send "bye\r"
expect "*>"
send "quit\r"
expect eof
执行和报错:
Username:h3c
Password:
<switch>screen-length disable
% Screen-length configuration is disabled for current user.
<switch>sys
System View: return to User View with Ctrl+Z.
[switch]logfile save
Saved the log file buffer to file cfa1:/logfile/logfile7.log successfully.
[switch]_h
Now you enter a hidden command view for developer's testing, some commands may
affect operation by wrong use, please carefully use it with our engineer's
direction.
[switch-hidecmd]_diagnose-file save
Info: Save all the contents in the diagnostic file buffer into file cfa1:/diaglog/diagfile10.log successfully.
[switch-hidecmd]display diagnostic-information
Save or display diagnostic information (Y=save, N=display)? [Y/N]:y
Please input the file name(*.diag)[cfa1:/default.diag]:
The file already exists, overwrite it? [Y/N]:
Before pressing ENTER you must choose 'YES' or 'NO'[Y/N]:y
Diagnostic information is outputting to cfa1:/default.diag.
Please wait...
Save successfully.
[switch-hidecmd]cd logfile/
^
% Unrecognized command found at '^' position.
[switch-hidecmd]^Cspawn telnet
usage: telnet [-l user] [-a] host-name [port]
send: spawn id exp5 not open
while executing
"send "$username\r""
(file "./test.exp" line 16)
[root@rconfig50 ~]# vim test.exp
|
|