Python Forum

Full Version: Manipulating the filename of an output script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using this script to pull the Running-Configs from my switches. It creates a filename for each output that includes "IP Address + today's date". What I'd like to do is have that filename display the hostname of the switch instead of the IP address. Can someone please help me accomplish this?

from netmiko import ConnectHandler
import getpass
import sys
import time

##getting system date 
day=time.strftime('%d')
month=time.strftime('%m')
year=time.strftime('%Y')
today=day+"-"+month+"-"+year

##initialising device
device = {
    'device_type': 'cisco_ios',
    'username': 'username',
    'password': 'password',
    'secret':'password'
    }
##opening IP file
ipfile=open("iplist.txt")
print ("Script to take backup of devices, Please enter your credential")
device['username']="username"
device['password']="password"
print("Enter enable password: ")
device['secret']="password"

##taking backup
for line in ipfile:
 try:
     device['ip']=line.strip("\n")
     print("\n\nConnecting Device ",line)
     net_connect = ConnectHandler(**device)
     net_connect.enable()
     time.sleep(1)
     print ("Reading the running config ")
     output = net_connect.send_command('show run')
     time.sleep(3)
     filename=device['ip']+'-'+today+".txt"
     saveconfig=open(filename,'w+')
     print("Writing Configuration to file")
     saveconfig.write(output)
     saveconfig.close()
     time.sleep(2)
     net_connect.disconnect()
     print ("Configuration saved to file",filename)
 except:
           print ("Access to "+device['ip']+" failed,backup did not taken")

ipfile.close()
print ("\nAll device backup completed")
So, do a reverse DNS lookup where you have the IP and want the hostname?
import socket
info = socket.gethostbyaddr("8.8.4.4")
print(info[0])
Output:
dns.google
Is that what you wanted?
(Jan-15-2020, 05:12 PM)jefsummers Wrote: [ -> ]So, do a reverse DNS lookup where you have the IP and want the hostname?
import socket
info = socket.gethostbyaddr("8.8.4.4")
print(info[0])
Output:
dns.google
Is that what you wanted?

Yes, and then I want to put the hostname as part of the name of the file that the script it creates. So, based on your response, could I then change the filename line to this?

filename=(info[0])+'-'+today+".txt"

I changed my script to the following based on your input hoping to get the filename to include the hostname instead of IP.

from netmiko import ConnectHandler
import getpass
import sys
import time
import socket

##getting system date 
day=time.strftime('%d')
month=time.strftime('%m')
year=time.strftime('%Y')
today=month+"-"+day+"-"+year

##initialising device
device = {
    'device_type': 'cisco_ios',
    'username': 'username',
    'password': 'password',
    'secret':'password'
    }
##opening IP file
ipfile=open("iplist.txt")
print ("Script to take backup of devices, Please enter your credential")
device['username']="svc_viagra"
device['password']="L!ttl3Blu3P1lL$"
print("Enter enable password: ")
device['secret']="SWMcp1169"

info = socket.gethostbyaddr(device['ip'])

##taking backup
for line in ipfile:
 try:
     device['ip']=line.strip("\n")
     print("\n\nConnecting Device ",line)
     net_connect = ConnectHandler(**device)
     net_connect.enable()
     time.sleep(1)
     print ("Reading the running config ")
     output = net_connect.send_command('show run')
     time.sleep(3)
     filename=(info[0])+'-'+today+".txt"
     saveconfig=open(filename,'w+')
     print("Writing Configuration to file")
     saveconfig.write(output)
     saveconfig.close()
     time.sleep(2)
     net_connect.disconnect()
     print ("Configuration saved to file",filename)
 except:
           print ("Access to "+device['ip']+" failed,backup did not taken")

ipfile.close()
print ("\nAll device backup completed")
Then I received this output:

Output:
Traceback (most recent call last): File "3850-autobackup-TEST.py", line 28, in <module> info = socket.gethostbyaddr(device['ip']) KeyError: 'ip'
If you look at device dictionary then you can’t find key named “ip”. Neither can Python.
(Jan-15-2020, 06:11 PM)perfringo Wrote: [ -> ]If you look at device dictionary then you can’t find key named “ip”. Neither can Python.

What would be the proper coding to do what I'm trying to do?