I have pieced together a script to login to each switch listed in the file switch.txt
, grab the serial number and place it with the IP address into the CSV serials.csv
. The problem I am having is that it only does the first device in the list. If I put the credentials in incorrectly, it does go through the rest of the devices, so it seems that when the Cisco device closes the connection, the loop stops.
Does anyone know how I can gracefully end the ssh session so it does not cause the script to stop running?
SCRIPT
#!/bin/bash
# File containing a list of switch IP addresses
SWITCH_LIST="./switch.txt"
# Loop through each IP address in the list
while read IP; do {
SERIAL=$(sshpass -p globalpass ssh globaluser@$IP "show version" | grep -i "System Serial Number" | awk '{print $NF}')
echo "$IP,$SERIAL" >> "serials.csv"
}
done < $SWITCH_LIST
SCRIPT OUTPUT
globaluser@netengmc:~$ ./get_sns.sh
Connection to 10.1.0.254 closed by remote host.
CONTENTS OF SERIALS.CSV
10.1.0.254,FCW3212C2D2
I appreciate any direction to a resolution.
Thank you!
Matthew "When you teach someone what you know, you gain a greater understanding of the subject, which increases your own retention of the knowledge."