Script telnet for device changes

So you just kicked your roommate out and you need to change the username and password on 250 switches.

You can do it with a quick bash loop and pipe it into telnet. (ssh is way better, but isn’t enabled by default on most network devices)

Make a new file named credentials.txt and put the username on the first line and the password on the second.

Then make a new file named devicelist.txt and place the IP address or hostname of all of the switches in this file. (each on it’s own line)

username=`head -n 1 credentials.txt`
password=`tail -n 1 credentials.txt`
for device in `cat devicelist.txt`
echo open $device
sleep 1

echo ” “
(only required for devices with a banner before username prompt)
sleep 1

echo $username
sleep .5
echo $password
sleep .5
echo “command to execute on device”
sleep 1
echo “another command to execute on device”
) | telnet

If some of the devices have longer delays between prompts you can adjust the sleep values. (telnet isn’t smart enough to know when the device is ready for a command)

This script will obviously need to be modified depending on the device you’re connecting to. It will get all of those passwords changed automatically for you. You should be using a RADIUS or TACACS+ server for authentication with that many switches. 🙂

It does work well to invoke backups to tftp servers, etc.

Leave a Reply