I have been tasked with doing a survey and inventory of a very large network. I specifically need to get the serial numbers of 1000+ Avaya switches and routers. All are accessible via either telnet or ssh from my workstation. Almost all of them are accessible with the same user name and password. Some will have only telnet access and some will have only ssh access.

Once logged in, I can get the serial number like this:

en[crlf] (to enable cli command mode)

show tec[crlf] (returns technical information of device)

[3x spacebar] (to make sure serial number is displayed)

q (to stop displaying more data and return to cli prompt)

exit[crlf] (to end the terminal session)

From the output generated by that I can then grep the serial numbers.

So my problem is automating this in a bash script or something similar to generate a list of serial numbers against the given IP addresses of the devices.

The script should ideally:

1.Read the IP address of the device from a file.

2.Try to login with ssh credentials 1 (if failed, try credentials 2)

3.If failed, telnet credentials 1 (if failed, try credentials 2)

4.If all failed, write "Failed to login" to output file.

5.If successful, write device IP address and serial to output file.

6.Repeat for next device IP in list.

7.Using plain text username and passwords in this situation is not an issue.

I am using the latest Ubuntu Linux on my workstation.

Hoping someone can give me some advice on accomplishing this task. I do not look forward to manually logging into 1000 switches.

  • 1
    man expect. – AlexP May 9 '18 at 8:41
  • 2
    "[3x spacebar] (to make sure serial number is displayed)" -- if that's to skip over some "next page" prompts, you might want to see if the switches have some command to disable paging. Some Cisco-like CLI:s have no page for that. If there isn't something like that, you may need to take timing into account there, it might not be safe to assume that you can just spam the characters there up front. – ilkkachu May 9 '18 at 8:43
  • expect or Ansible if you are interesting in exploring that field. – Rui F Ribeiro May 9 '18 at 9:07
  • Could SNMP be another option? Like: snmpget -v2c -Ovq -c public "$ip_address" . – Stéphane Chazelas May 9 '18 at 9:22

For automation of interactive CLI processes you can use "Expect" or its alternatives: https://en.wikipedia.org/wiki/Expect. The simplest Expect script example:

spawn telnet foo.bar.com 
expect ogin {send luser\r}
expect assword {send TopSecret\r}
send "who am i\r" 
send "exit\r"
expect eof

Your Answer

