cancel
Showing results for 
Search instead for 
Did you mean: 

Create file with sysName in filename using Python

Create file with sysName in filename using Python

dleboeuf
New Contributor

Hello all,

I recently started using Netmiko with Python to script out data collection on EXOS/VOSS switches, and I am trying to export the outputs of commands to a file using the switch name as well as the command that was ran. The script is working, but for some reason I can't get the switch name to attach to the filename, it just keeps exporting with the command that was ran and not the hostname. Here is the code below (bold where I am entering the command to send) where I am trying to get it to work. I am sure it is something simple, I am just a noob with Python!

for switch in switches:
try:
net_connect = ConnectHandler(device_type=values[3], ip=switch, username=values[0], password=values[1])
except:
filename = switch + ".txt"
show = "Can't connect to switch: " + switch
print(show)
f = open(filename,'w')
f.write(show)
f.close
next
if values[3] == "voss":
switchname = net_connect.send_command("show running-config | in sys-name", max_loops=5000)
else:
switchname = net_connect.send_command("show system | in SysName: ", max_loops=5000).split(" ")[1]
switchname = switchname.replace("\n","")
print("Executing "+ values[4] + " on " + switchname)
show = net_connect.send_command(values[4], max_loops=5000)
filename = switchname.replace(" ","_") + "-" + values[4].replace(" ", "_") + ".txt"
f = open(filename,'w')
f.write(show)
f.close
print("File " + filename + " generated with the output")

I tried using ("sho config | in sysName") but that isn't working either, it just uses 'snmp' instead of the sysName. I have it working on Cisco devices using ("show run | i hostname") so not sure where I am going wrong, not sure if the double quotes are throwing it off or what.

I appreciate the help!

2 REPLIES 2

Oliviasalik
New Contributor

All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.


@dleboeufMyPaymentsPlus App wrote:

Hello all,

I recently started using Netmiko with Python to script out data collection on EXOS/VOSS switches, and I am trying to export the outputs of commands to a file using the switch name as well as the command that was ran. The script is working, but for some reason I can't get the switch name to attach to the filename, it just keeps exporting with the command that was ran and not the hostname. Here is the code below (bold where I am entering the command to send) where I am trying to get it to work. I am sure it is something simple, I am just a noob with Python!

for switch in switches:
try:
net_connect = ConnectHandler(device_type=values[3], ip=switch, username=values[0], password=values[1])
except:
filename = switch + ".txt"
show = "Can't connect to switch: " + switch
print(show)
f = open(filename,'w')
f.write(show)
f.close
next
if values[3] == "voss":
switchname = net_connect.send_command("show running-config | in sys-name", max_loops=5000)
else:
switchname = net_connect.send_command("show system | in SysName: ", max_loops=5000).split(" ")[1]
switchname = switchname.replace("\n","")
print("Executing "+ values[4] + " on " + switchname)
show = net_connect.send_command(values[4], max_loops=5000)
filename = switchname.replace(" ","_") + "-" + values[4].replace(" ", "_") + ".txt"
f = open(filename,'w')
f.write(show)
f.close
print("File " + filename + " generated with the output")

I tried using ("sho config | in sysName") but that isn't working either, it just uses 'snmp' instead of the sysName. I have it working on Cisco devices using ("show run | i hostname") so not sure where I am going wrong, not sure if the double quotes are throwing it off or what.

I appreciate the help!




The output of the command

show system | in SysName

is splitted like this

['SysName:', '', '', '', '', '', '', '', '', '', 'foo-sysname']

So you should do

.split(" ")[10]

GTM-P2G8KFN