Python Forum
Manipulating the filename of an output script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating the filename of an output script
#1
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")
Reply
#2
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?
Reply
#3
(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'
Reply
#4
If you look at device dictionary then you can’t find key named “ip”. Neither can Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Filename stamp script is needed ineuw 11 4,596 Sep-12-2023, 03:05 AM
Last Post: ineuw
  output provide the filename along with the input file processed. arjunaram 1 936 Apr-13-2023, 08:15 PM
Last Post: menator01
  Real-Time output of server script on a client script. throwaway34 2 2,054 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,349 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Import output from python script to another varietyjones 1 1,907 Oct-12-2020, 09:07 PM
Last Post: bowlofred
  Manipulating data from a CSV EvanS1 5 2,721 Jun-12-2020, 05:59 PM
Last Post: perfringo
  manipulating two lists rancans 8 3,188 Apr-16-2020, 06:00 PM
Last Post: deanhystad
  Manipulating index value, what is wrong with this code? Emun 1 1,751 Feb-05-2020, 07:18 AM
Last Post: perfringo
  Manipulating Excel with Python. Spacely 2 3,639 Jun-25-2019, 01:57 AM
Last Post: Dequanharrison
  Redircet output of Python script to .txt file gurbhej_singh 7 8,437 May-20-2019, 01:11 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020