-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcisco_asa_config_capture_alt.py
executable file
·75 lines (59 loc) · 1.88 KB
/
cisco_asa_config_capture_alt.py
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
#!/usr/bin/python3
"""
Capture CISCO IOS device configurations. Used by CA Spectrum NCM
"""
import re
import sys
from netmiko import ConnectHandler, NetMikoAuthenticationException
HOSTNAME = sys.argv[1]
USER = sys.argv[2]
PASS = sys.argv[3]
ENABLE = sys.argv[4]
TIMEOUT = int(sys.argv[5])
CONFIG_TYPE = sys.argv[6] if len(sys.argv) > 6 else 'running'
GLOBAL_DELAY_FACTOR = 2
SHOW_DELAY_FACTOR = 5
def main():
cisco_device = {
'device_type': 'cisco_ios_telnet',
'ip': HOSTNAME,
'username': USER,
'password': PASS,
'secret': ENABLE,
'timeout': TIMEOUT,
'global_delay_factor': GLOBAL_DELAY_FACTOR
}
try:
net_connect = ConnectHandler(**cisco_device)
except NetMikoAuthenticationException:
cisco_device['password'] = ENABLE
try:
net_connect = ConnectHandler(**cisco_device)
except NetMikoAuthenticationException:
print('Authentication Error', file=sys.stderr)
exit(251)
except Exception:
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
exit(1)
try:
net_connect.enable()
except Exception:
pass
output = ''
if CONFIG_TYPE == 'startup':
output = net_connect.send_command('show startup-config', delay_factor=SHOW_DELAY_FACTOR)
elif CONFIG_TYPE == 'running':
output = net_connect.send_command('show running-config', delay_factor=SHOW_DELAY_FACTOR)
else:
net_connect.disconnect()
print('Invalid configuration type.\nPlease specify either running or startup', file=sys.stderr)
exit(5)
output.lstrip()
conf = ''
for line in output.split('\n'):
if not re.match('^(Using|Building configuration|Current configuration)', line):
conf += line + '\n'
print(conf.lstrip())
net_connect.disconnect()
if __name__ == '__main__':
main()