问题
I have the following scripts which is supposed to pull the IP address from a file device-list.txt
and then telnet to the device, login and execute a show run. The script logs in to the device successfully, but gives me an error after that. Any ideas where im going wrong?
for device in `cat device-list.txt`; do ./test4 $device;done
cat test4
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user myusername
set pass mypassword
set timeout 10
log_file -a ~/results.log
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "Username:"
send "$user\n"
expect "Password:"
send "$pass\n"
expect "#"
send “term len 0\n”
send “show running-config\n”
expect “end\r”
send “\n”
send “exit\n”
User Access Verification
Username: myusername
Password:
ROUTER#usage: send [args] string
while executing
"send “term len 0\n”"
(file "./test4" line 26)
回答1:
Your problem is due to incorrect double quotes. You have to use double quotes "
, not smart quotes “
send “term len 0\n” ; # Wrong
send "term len 0\r"; # Correct
Note :
Basically, Expect
will work with two feasible commands such as send
and expect
. If send
is used, then it is mandatory to have expect
(in most of the cases) afterwards. (while the vice-versa is not required to be mandatory)
This is because without that we will be missing out what is happening in the spawned process as Expect
will assume that you simply need to send one string value and not expecting anything else from the session.
So, we recommend to use expect
after each send
, to make sure our commands actually sent to the spawned process. (You have not used expect
after sending some commands such as term len 0\r
). Also, use \r
instead of \n
.
来源:https://stackoverflow.com/questions/33383706/expect-telnet-to-multiple-cisco-devices-and-execute-show-run