cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3648
Views
0
Helpful
3
Replies

Automated Backup Python Script with Hostnames

Hello Everyone,

 

I'm using a basic script to backup the device configs from the file called as "myswitches" which has a list of IP addresses. However, the files are being saved with the name "switch x.x.x.x", where x.x.x.x is the IP of the device. Is there a way where I can get the script to save the backups with the name same as the hostname of the device?

 

Below is the script that I'm using:

 

#########################################################

import telnetlib
import getpass

user = input("Enter your username:")
password = getpass.getpass()

f = open('myswitches')

for IP in f:
IP=IP.strip()
print('Get running config from the switches' + (IP))
HOST = IP
tn = telnetlib.Telnet(HOST)
tn.set_debuglevel(1000)
tn.read_until(b"Username:")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password:")
tn.write(user.encode('ascii') + b"\n")
tn.write(b"enable\n")
tn.read_until(b"Password:")
tn.write(user.encode('ascii') + b"\n")
tn.write(b"terminal length 0\n")
tn.write(b"show run\n")
tn.write(b"exit\n")

readoutput = tn.read_all()
saveoutput = open("switch" + HOST, "w")
saveoutput.write(readoutput.decode('ascii'))
saveoutput.write("\n")
saveoutput.close

 

#########################################################

3 Replies 3

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

Despite my reservations about you using telnet for this script, I have tweaked it to extract the hostname from the config. If it cannot find it, then it uses the IP address as before:

 

import telnetlib
import getpass
import re

user = input("Enter your username:")
password = getpass.getpass()

f = open('myswitches')

for IP in f:
IP=IP.strip()
print('Get running config from the switches' + (IP))
HOST = IP
tn = telnetlib.Telnet(HOST)
tn.set_debuglevel(1000)
tn.read_until(b"Username:")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password:")
tn.write(user.encode('ascii') + b"\n")
tn.write(b"enable\n")
tn.read_until(b"Password:")
tn.write(user.encode('ascii') + b"\n")
tn.write(b"terminal length 0\n")
tn.write(b"show run\n")
tn.write(b"exit\n")

readoutput = tn.read_all()

match = re.search('^hostname\s(?P<hostname>\w+)$', readoutput, re.MULTILINE)

if match:
  suffix = result.group('hostname'))
else:
  suffix = HOST

saveoutput = open("switch" + suffix, "w")
saveoutput.write(readoutput.decode('ascii'))
saveoutput.write("\n")
saveoutput.close


 

 

Ca I suggest you take a look at this example of running commands via SSH:

https://github.com/sebrupik/srupik-apic-em-tools/blob/master/frozenPony/src/oneLinerSimple.py

 

cheers,

Seb.

 

 

Hello Seb, 

 

Thanks for your reply. 

 

I'm getting this error while running the script.

############################################

Traceback (most recent call last):
File "community.py", line 30, in <module>
match = re.search('^hostname\s(?P<hostname>\w+)$', readoutput, re.MULTILINE)
File "/usr/lib/python3.6/re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object

 

###########################################

 

Also, if you don't mind can you please explain what this command does, of it there is a reference link, so that I can refer to the syntax of this command and understand what it means.

 

As for using the SSH, I'm still new to python.. so I've started with this, but I'll go through the github link that you posted. Thanks for the info.

Hi there,

Looks like we need to the convert the bytes returned by tn.read_all() to a string. Replace the regex line with:

 

match = re.search('^hostname\s(?P<hostname>\w+)$', str(readoutput), re.MULTILINE)

As for the command we are searching the readoutput string using regex. We are looking for a line which starts with the string 'hostname' then has a single space (\s), the following words are all matched and assigned to a group called hostname (?P<hostname>) so we can reference it later. This group will match to the end of the line ($). If we find a match then the variable match will return true so we can use it in a conditional statement and then return the value of the group 'hostname' from the 'match' vairable. If no match was found then the else clause will use the value assigned to the HOST variable.

 

cheers,

Seb.