-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.py
More file actions
executable file
·202 lines (187 loc) · 8.42 KB
/
validator.py
File metadata and controls
executable file
·202 lines (187 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env /usr/bin/python
import re
import sys
import getpass
import socket
import threading
from Queue import Queue
# pip install pyping
import pyping
# pip install netmiko
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import NetMikoAuthenticationException
from paramiko.ssh_exception import SSHException
# git clone https://github.com/ekiojp/dhcp-relay-cisco/common.py
from common import login
from common import grep_all
__author__ = 'Emilio <ec@ekio.jp>'
__date__ = 'Jul-5-2018'
__version__ = '0.4'
def device_session(IP,scope,gu,gp,ju,jp,tu,tp,te,ku,kp,ke,output_q):
output_dict = {}
net_connect = login(IP,gu,gp,ju,jp,tu,tp,te,ku,kp,ke)
if net_connect:
prompt = re.sub('[>|#]', '', net_connect.find_prompt())
shint = net_connect.send_command('show ip interface brief | i ' + IP).split('\n')
if len(shint) > 1:
for x in range(len(shint)):
if shint[x].split()[1] == IP:
shrunint = net_connect.send_command('show run interface '
+ shint[x].split()[0]
+ ' | i helper-address|dhcp relay address')
if re.search('helper-address', shrunint):
output = ',IOS,' + prompt + ',' + IP + ',' + shint[x].split()[0]
+ ',None,None,None\n'
elif re.search('dhcp relay address', shrunint):
output = ',NX,' + prompt + ',' + IP + ',' + shint[x].split()[0]
+ ',None,None,None\n'
else:
output = ',None,' + prompt + ',' + IP + ',' + shint[x].split()[0]
+ ',None,None,None\n'
else:
shstb = net_connect.send_command('show standby brief | i ' + IP)
if 'Invalid' not in shstb:
m = re.search('.* ' + IP, shstb)
if m:
regex = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
peerip = re.search(regex, shstb).group()
peer_connect = login(peerip,
gu,
gp,
ju,
jp,
tu,
tp,
te,
ku,
kp,
ke
)
if peer_connect:
peer_prompt = re.sub('[>|#]', '', peer_connect.find_prompt())
shint = peer_connect.send_command('show ip interface brief | i ' + peerip).split('\n')
if len(shint) == 1:
peerint = shint[0].split()[0]
else:
peerint = 'None'
peer_connect.disconnect()
else:
peer_prompot = 'None,None,None'
output = (',IOS,' + prompt + ',' + IP + ',' + m.group().split()[0] + ','
+ peer_prompt + ',' + peerip + ',' + peerint + '\n')
else:
shhsrp = net_connect.send_command('show hsrp brief | i ' + IP)
m = re.search('.* ' + IP, shhsrp)
if m:
regex = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
peerip = re.search(regex, shhsrp).group()
peer_connect = login(peerip,
gu,
gp,
ju,
jp,
tu,
tp,
te,
ku,
kp,
ke
)
if peer_connect:
peer_prompt = re.sub('[>|#]', '' , peer_connect.find_prompt())
shint = peer_connect.send_command('show ip interface brief | i ' + peerip).split('\n')
if len(shint) == 1:
peerint = shint[0].split()[0]
else:
peerint = 'None'
peer_connect.disconnect()
else:
peer_prompt = 'None,None,None'
output = (',NX,' + prompt + ',' + IP + ',' + m.group().split()[0]
+ ',' + peer_prompt + ',' + peerip + ',' + peerint + '\n')
net_connect.disconnect()
else:
output = ',NA,NA,'+IP+',NA,NA,NA,NA\n'
output_dict[scope] = output
output_q.put(output_dict)
if __name__ == "__main__":
if len(sys.argv) <= 2:
print 'Author: ' + __author__
print 'Date: ' + __date__
print 'Version: ' + __version__
print '\nUsage: ' + sys.argv[0] + ' <dhcp-dump> <scope-list>'
print ('\n<dhcp-dump> input file with Windows DHCP'
+ ' dump format (after cleaner.py)')
print ('<scope-list> CSV output format: '
+ '<Scope>,<IOS/NX/None>,<ACT_hostname>,<ACT_IP>,'
+ '<ACT_Intf>,<STB_hostname>,<STB_IP>,<STB_Intf>\n')
sys.exit(0)
INPUT = sys.argv[1]
OUTPUT = sys.argv[2]
salida = open(OUTPUT,'w')
scopelist=[]
with open(INPUT,'r') as sfile:
for line in sfile:
if line not in ['\n', '\r\n']:
scopelist.append(line.split()[1])
mm = set(scopelist)
scopelist = list(mm)
gu = raw_input('First TACACS Username: ')
gp = getpass.getpass('First TACACS Password: ')
ju = raw_input('Second TACACS Username: ')
jp = getpass.getpass('Second TACACS Password: ')
tu = raw_input('Third Local Username: ')
tp = getpass.getpass('Third Local Password: ')
te = getpass.getpass('Third Local Enable: ')
ku = raw_input('Fourth Local Username: ')
kp = getpass.getpass('Fourth Local Password: ')
ke = getpass.getpass('Fourth Local Enable: ')
output_q = Queue()
for x in range(len(scopelist)):
regex = 'Scope ' + scopelist[x] + ' set optionvalue 3 IPADDRESS .*'
aver = grep_all(regex, INPUT)
if len(aver) == 1:
IP = re.sub('"','',aver[0].split()[6])
response = pyping.ping(IP,count=1)
if response.ret_code == 0:
my_thread = threading.Thread(target=device_session,
args=(IP,
scopelist[x],
gu,
gp,
ju,
jp,
tu,
tp,
te,
ku,
kp,
ke,
output_q)
)
my_thread.start()
else:
print (
'ERROR: Scope ' + scopelist[x]
+ ' can\'t ping scope gateway (' + IP + ')'
)
salida.write(scopelist[x]+',NotPing,,,,,,\n')
elif len(aver) > 1:
print (
'ERROR: Scope ' + scopelist[x]
+ ' have too many gateways'
)
salida.write(scopelist[x]+',ManyGateway,,,,,,\n')
else:
print 'ERROR: Scope ' + scopelist[x] + ' doesn\'t have a gateway'
salida.write(scopelist[x]+',NoGateway,,,,,,\n')
main_thread = threading.currentThread()
for some_thread in threading.enumerate():
if some_thread != main_thread:
some_thread.join()
while not output_q.empty():
my_dict = output_q.get()
for k, val in my_dict.iteritems():
salida.write(k+val)
salida.close()