Reputation: 45
New to python and I'm trying to export some information from network devices and format it into a text file. I have the below code I'm running;
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
output = net_connect.send_command('show hostname')
pickle.dump(output, open("hostname.p", "wb"))
output = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:')
output = output.strip()
pickle.dump(output, open("IOSimage.p", "wb"))
output = pickle.load(open("hostname.p","rb"))
print('Hostname: ' + output)
output = pickle.load(open("IOSimage.p","rb"))
print(output)
I'm trying to change the output from looking like:
Hostname: LD6-AGG-SW01
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
Hostname: LD6-AGG-SW02
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
to:
Hostname: LD6-AGG-SW01
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
Hostname: LD6-AGG-SW02
NXOS image file is: bootflash:///nxos.7.0.3.I3.1.bin
Any ideas on how I'd go about this?
Upvotes: 0
Views: 91
Reputation: 168824
I'm assuming net_connect.send_command()
returns a string with a trailing newline.
If that's correct,
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
hostname = net_connect.send_command('show hostname').strip()
ios_image = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:').strip()
print('Hostname:', hostname)
print('Image:', ios_image)
print()
– i.e. strip()
ing the outputs – should do the trick.
Upvotes: 1
Reputation: 1942
Use .strip()
to remove the unwanted \n
character in your output:
for device in (LD6AGGSW01 , LD6AGGSW02 ):
net_connect = ConnectHandler(**device)
output = net_connect.send_command('show hostname')
pickle.dump(output, open("hostname.p", "wb"))
output = net_connect.send_command('sh ver | i NXOS | i image |i file |i is:')
output = output.strip()
pickle.dump(output, open("IOSimage.p", "wb"))
output = pickle.load(open("hostname.p","rb"))
output = output.strip() # Strip output
print('Hostname: ' + output)
output = pickle.load(open("IOSimage.p","rb"))
output = output.strip() # Strip output
print(output)
print() # Call print function to insert extra line
Upvotes: 0