Skip to content

Commit 1775b42

Browse files
authored
Create InterStat.py
1 parent be1e813 commit 1775b42

1 file changed

Lines changed: 236 additions & 0 deletions

File tree

InterStat.py

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import paramiko
2+
import getpass
3+
import time
4+
import os
5+
import sys
6+
import re
7+
import signal
8+
9+
10+
#define active directory credentials
11+
adUsername = input('AD-Username: ')
12+
adPassword = getpass.getpass('AD-Password: ')
13+
14+
#define local credentials
15+
locUsername = input('Local-Username: ')
16+
locPassword = getpass.getpass('Local-Password: ')
17+
18+
#define ssh port
19+
port = ('22')
20+
21+
#define path of directory
22+
path = (r'G:/InterStat/')
23+
24+
#ip list of devices
25+
with open('ip_list_messer.txt', 'r') as ip:
26+
ip_list_messer = ip.read().split('\n')
27+
28+
#graceful handling of keyboardInterruption
29+
def signal_handler(signal, frame):
30+
print("\n# Program exiting gracefully")
31+
socket.close(0)
32+
sys.exit(0)
33+
34+
signal.signal(signal.SIGINT, signal_handler)
35+
36+
#main programm
37+
def getStatus():
38+
for ip in ip_list_messer:
39+
try:
40+
print ('# Trying to log in to %s' % (ip))
41+
#create ssh session with active directory credentials
42+
ssh = paramiko.SSHClient()
43+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
44+
ssh.connect(
45+
ip,
46+
port = port,
47+
username = adUsername,
48+
password = adPassword,
49+
look_for_keys = False,
50+
allow_agent = False)
51+
ssh.slave = ssh.invoke_shell()
52+
print ('# Logged in with AD credentials')
53+
54+
#check ena
55+
def checkEna():
56+
time.sleep(3)
57+
ssh.slave.send('\n')
58+
time.sleep(1)
59+
enaOutput = ssh.slave.recv(9999).decode('UTF-8')
60+
if '#' in enaOutput:
61+
print ('# Logged in privilege mode')
62+
else:
63+
print ('# Trying to log in privilege mode')
64+
ssh.slave.send('ena\n')
65+
time.sleep(1)
66+
ssh.slave.send(locPassword)
67+
ssh.slave.send('\n')
68+
time.sleep(1)
69+
enaOutput = ssh.slave.recv(9999).decode('UTF-8')
70+
if '#' in enaOutput:
71+
print ('# Logged in privilege mode')
72+
else:
73+
print ('# Can not log in privilege mode, script closed')
74+
ssh.close()
75+
checkEna()
76+
77+
#disable paging
78+
def disPage():
79+
ssh.slave.send('term len 0\n')
80+
print ('# Paging disabled')
81+
ssh.slave.recv(9999).decode('UTF-8')
82+
time.sleep(1)
83+
disPage()
84+
85+
#lookup for hostname
86+
def checkHost():
87+
global hostname
88+
ssh.slave.send('show version\n')
89+
time.sleep(1)
90+
brandOutput = ssh.slave.recv(9999).decode('UTF-8')
91+
if re.search(r'Cisco', brandOutput) or re.search(r'cisco', brandOutput):
92+
print ('# Brand of device is Cisco')
93+
try:
94+
ssh.slave.send('show run | include hostname\n')
95+
time.sleep(5)
96+
hostOutput = ssh.slave.recv(9999).decode('UTF-8')
97+
hostname = hostOutput.split('\n')[1].split(' ')[1]
98+
print ('# Hostname of device is %s' % (hostname.strip()))
99+
except:
100+
print ('# Can not find out hostname, script closed')
101+
ssh.close()
102+
elif re.search(r'Dell', brandOutput) or re.search(r'dell', brandOutput):
103+
print ('# Brand of device is Dell')
104+
try:
105+
ssh.slave.send('show run | find hostname\n')
106+
time.sleep(5)
107+
hostOutput = ssh.slave.recv(9999).decode('UTF-8')
108+
hostname = hostOutput.split('\n')[1].split(' ')[1]
109+
print ('# Hostname of device is %s' % (hostname.strip()))
110+
except:
111+
print ('# Can not find out hostname, script closed')
112+
ssh.close()
113+
checkHost()
114+
115+
#inter status
116+
def interStat():
117+
global statusOutput
118+
ssh.slave.send('show inter status\n')
119+
print ('# Lookup for inter status')
120+
time.sleep(2)
121+
statusOutput = ssh.slave.recv(9999).decode('UTF-8')
122+
interStat()
123+
124+
#inter status file
125+
def writeStat():
126+
print ('# Write output of inter status to %s%s.txt' % (path, hostname.strip()))
127+
time.sleep(1)
128+
for line in statusOutput.splitlines():
129+
statFile = open('%sInterStatus-%s.txt' % (path, hostname.strip()), 'a')
130+
statFile.write(line)
131+
statFile.write('\n')
132+
statFile.close()
133+
print ('# Inter status output saved to %s%s.txt' % (path, hostname.strip()))
134+
print ('')
135+
writeStat()
136+
ssh.close()
137+
138+
except:
139+
#create ssh session with local credentials
140+
ssh = paramiko.SSHClient()
141+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
142+
ssh.connect(
143+
ip,
144+
port = port,
145+
username = locUsername,
146+
password = locPassword,
147+
look_for_keys = False,
148+
allow_agent = False)
149+
ssh.slave = ssh.invoke_shell()
150+
print ('# Logged in with local credentials')
151+
152+
#check ena
153+
def checkEna():
154+
time.sleep(3)
155+
ssh.slave.send('\n')
156+
time.sleep(1)
157+
enaOutput = ssh.slave.recv(9999).decode('UTF-8')
158+
if '#' in enaOutput:
159+
print ('# Logged in privilege mode')
160+
else:
161+
print ('# Trying to log in privilege mode')
162+
ssh.slave.send('ena\n')
163+
time.sleep(1)
164+
ssh.slave.send(locPassword)
165+
ssh.slave.send('\n')
166+
time.sleep(1)
167+
enaOutput = ssh.slave.recv(9999).decode('UTF-8')
168+
if '#' in enaOutput:
169+
print ('# Logged in privilege mode')
170+
else:
171+
print ('# Can not log in privilege mode, script closed')
172+
ssh.close()
173+
checkEna()
174+
175+
#disable paging
176+
def disPage():
177+
ssh.slave.send('term len 0\n')
178+
print ('# Paging disabled')
179+
ssh.slave.recv(9999).decode('UTF-8')
180+
time.sleep(1)
181+
disPage()
182+
183+
#lookup for hostname
184+
def checkHost():
185+
global hostname
186+
ssh.slave.send('show version\n')
187+
time.sleep(1)
188+
brandOutput = ssh.slave.recv(9999).decode('UTF-8')
189+
if re.search(r'Cisco', brandOutput) or re.search(r'cisco', brandOutput):
190+
print ('# Brand of device is Cisco')
191+
try:
192+
ssh.slave.send('show run | include hostname\n')
193+
time.sleep(5)
194+
hostOutput = ssh.slave.recv(9999).decode('UTF-8')
195+
hostname = hostOutput.split('\n')[1].split(' ')[1]
196+
print ('# Hostname of device is %s' % (hostname.strip()))
197+
except:
198+
print ('# Can not find out hostname, script closed')
199+
ssh.close()
200+
elif re.search(r'Dell', brandOutput) or re.search(r'dell', brandOutput):
201+
print ('# Brand of device is Dell')
202+
try:
203+
ssh.slave.send('show run | find hostname\n')
204+
time.sleep(5)
205+
hostOutput = ssh.slave.recv(9999).decode('UTF-8')
206+
hostname = hostOutput.split('\n')[1].split(' ')[1]
207+
print ('# Hostname of device is %s' % (hostname.strip()))
208+
except:
209+
print ('# Can not find out hostname, script closed')
210+
ssh.close()
211+
checkHost()
212+
213+
#inter status
214+
def interStat():
215+
global statusOutput
216+
ssh.slave.send('show inter status\n')
217+
print ('# Lookup for inter status')
218+
time.sleep(2)
219+
statusOutput = ssh.slave.recv(9999).decode('UTF-8')
220+
interStat()
221+
222+
#inter status file
223+
def writeStat():
224+
print ('# Write output of inter status to %s%s.txt' % (path, hostname.strip()))
225+
time.sleep(1)
226+
for line in statusOutput.splitlines():
227+
statFile = open('%sInterStatus-%s.txt' % (path, hostname.strip()), 'a')
228+
statFile.write(line)
229+
statFile.write('\n')
230+
statFile.close()
231+
print ('# Inter status output saved to %s%s.txt' % (path, hostname.strip()))
232+
print ('')
233+
writeStat()
234+
ssh.close()
235+
print ('Script ran successfully. Script closed.')
236+
getStatus()

0 commit comments

Comments
 (0)