2

I've made an expect/bash script to retrieve cisco devices configuration; it's simple copy cisco running-configuration and save it using tftp.

#!/bin/bash
while read line;
    do

device=$line;
expect << EOF

spawn telnet $device
expect "Username:"
   send "username\n"
   expect "Password:"
  send "password\n"
send "copy running-config tftp://192.168.244.14\r"
expect "Address or name of remote host"
send "\r"
expect "Destination filename "
send "\r"
expect "secs"
send "exit\r"
close
EOF

done < /home/marco/Scrivania/Host.txt

exit 0

My issue is that I've several devices, some configured to accept telnet connections, other to accept only ssh connections. So, in my script,I would add something such as:

try to connect to device using telnet if isn't no response after 3 minutes, cancel 'spawn telnet...' command and try connect using ssh.

In witch way can I implement this?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Marco
  • 10,283
  • 4
  • 23
  • 22

1 Answers1

0

This is not the right way to go about this, really. You're be much better off having two separate lists: one for telnet, and one for ssh. It just keeps things simpler in the long run.

If you're really set on letting the connection hang and time out, though, you can certainly do that. Just set a timeout value in seconds, and an action to take if the timeout is period is exceeded.

For example:

spawn telnet $device
expect -timeout 180 {
    "Username:" {send "username\r\n"; sleep 1; send "password\r\n"}
    timeout     { your_ssh_actions_or_proc }
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199