Skip to content

Commit 4bacdf7

Browse files
committed
initial commit
0 parents  commit 4bacdf7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# pythonscripts

show_commands.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import print_function
2+
from netmiko import ConnectHandler
3+
import sys
4+
import time
5+
import select
6+
import paramiko
7+
import re
8+
fd = open(r'/home/user/show_output.txt','w') # Where you want the file to save to.
9+
old_stdout = sys.stdout
10+
sys.stdout = fd
11+
platform = 'cisco_ios'
12+
username = 'user' # edit to reflect
13+
password = 'password' # edit to reflect
14+
15+
ip_add_file = open(r'/home/user/ipadd.txt','r') # a simple list of IP addresses you want to connect to each one on a new line
16+
17+
for host in ip_add_file:
18+
host = host.strip()
19+
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
20+
output = device.send_command('terminal length 0')
21+
output = device.send_command('enable') #Editable to be what ever is needed
22+
print('##############################################################')
23+
# print('...................CISCO COMMAND SHOW VER OUTPUT......................\n')
24+
output = device.send_command('sh run | in hostname')
25+
print(output)
26+
output = device.send_command('sh ver | in Base ethernet MAC Address')
27+
print(output)
28+
output = device.send_command('sh ver | in System serial number')
29+
print(output)
30+
# print('##############################################################')
31+
fd.close()
32+
33+
# opens the output file and deletes the line with $(hostname)
34+
with open("/home/user/show_output.txt","r") as f:
35+
lines = f.readlines()
36+
with open("/home/user/show_output.txt","w") as f:
37+
for line in lines:
38+
if line!="*** You are connected to $(hostname) ***"+"\n":
39+
f.write(line)
40+
41+
42+
# opens the output file and deletes the string System serial number and MAC address
43+
f = open("/home/user/show_output.txt").read()
44+
f_new = re.sub('System serial number : ','',f)
45+
open("/home/user/show_output.txt",'w').write(f_new)
46+
47+
f = open("/home/user/show_output.txt").read()
48+
f_new = re.sub('Base ethernet MAC Address : ','',f)
49+
open("/home/user/show_output.txt",'w').write(f_new)

0 commit comments

Comments
 (0)