Skip to content

Commit 8040cf8

Browse files
committed
Add various scripts
1 parent 783c230 commit 8040cf8

6 files changed

+449
-0
lines changed

Diff for: cisco_asa_config_capture.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/python3
2+
# pylint: disable=C0111
3+
"""
4+
Capture CISCO ASA device configurations. Used by CA Spectrum NCM
5+
"""
6+
7+
import re
8+
import sys
9+
10+
from netmiko import (ConnectHandler, NetMikoAuthenticationException, NetMikoTimeoutException)
11+
12+
HOSTNAME = sys.argv[1]
13+
USER = sys.argv[2]
14+
PASS = sys.argv[3]
15+
ENABLE = sys.argv[4]
16+
TIMEOUT = int(sys.argv[5])
17+
CONFIG_TYPE = sys.argv[6] if len(sys.argv) > 6 else'running'
18+
ENABLE_LEVEL = int(sys.argv[7]) if len(sys.argv) > 7 else 15
19+
20+
GLOBAL_DELAY_FACTOR = 2
21+
SHOW_DELAY_FACTOR = 5
22+
23+
24+
def enable_level(net_connect, level=1):
25+
if level < 15:
26+
prompt = net_connect.find_prompt().replace('/', '.')
27+
net_connect.send_command('enable {}'.format(level), expect_string='ssword:')
28+
net_connect.send_command(net_connect.secret, expect_string=prompt)
29+
return
30+
try:
31+
net_connect.enable()
32+
except Exception:
33+
return
34+
35+
36+
def get_conf(net_connect, command):
37+
prompt = net_connect.find_prompt().replace('/', '.') # Without this hack send_command hangs forever
38+
output = net_connect.send_command(command, delay_factor=SHOW_DELAY_FACTOR, expect_string=prompt).lstrip()
39+
40+
conf = ''
41+
for line in output.split('\n'):
42+
if not re.match('^(Using|Building configuration|Current configuration)', line):
43+
conf += line + '\n'
44+
45+
return conf.lstrip()
46+
47+
48+
def main():
49+
cisco_device = {
50+
'device_type': 'cisco_asa',
51+
'ip': HOSTNAME,
52+
'username': USER,
53+
'password': PASS,
54+
'secret': ENABLE,
55+
'timeout': TIMEOUT,
56+
'global_delay_factor': GLOBAL_DELAY_FACTOR
57+
}
58+
59+
try:
60+
net_connect = ConnectHandler(**cisco_device)
61+
except NetMikoTimeoutException:
62+
print('Connection Timeout', file=sys.stderr)
63+
exit(3)
64+
except NetMikoAuthenticationException:
65+
print('Authentication Error', file=sys.stderr)
66+
exit(252)
67+
except Exception:
68+
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
69+
exit(1)
70+
71+
enable_level(net_connect, ENABLE_LEVEL)
72+
73+
net_connect.send_command('terminal pager 0')
74+
75+
if CONFIG_TYPE == 'startup':
76+
command = 'show startup-config'
77+
elif CONFIG_TYPE == 'running':
78+
command = 'show running-config'
79+
else:
80+
net_connect.disconnect()
81+
print('Invalid configuration type.\nPlease specify either running or startup', file=sys.stderr)
82+
exit(5)
83+
84+
# check if the firewall is in multiple or single context mode
85+
asamode = net_connect.send_command('show mode', delay_factor=SHOW_DELAY_FACTOR)
86+
87+
output = ''
88+
if 'multiple' in asamode:
89+
output = 'ATTENTION: MULTIPLE CONTEXT MODE'.center(80, '!') + '\n'
90+
91+
# change to system context and get the configuration
92+
net_connect.send_command('changeto system')
93+
systemconf = get_conf(net_connect, command)
94+
output += '\n' + 'System Config'.center(80, '!') + '\n' + systemconf + '\n'
95+
96+
# get the list of contexts
97+
contexts = re.findall(r'^context (\w+)$', systemconf, re.MULTILINE)
98+
99+
# get the configuration of each context
100+
for context in contexts:
101+
net_connect.send_command('changeto context {}'.format(context))
102+
output += '\n' + 'Context {} Config'.format(context).center(80, '!') + '\n'
103+
output += get_conf(net_connect, command)
104+
else: # Assume single mode
105+
output = get_conf(net_connect, command)
106+
107+
net_connect.disconnect()
108+
109+
print(output)
110+
111+
112+
if __name__ == '__main__':
113+
main()

Diff for: cisco_asa_config_capture_alt.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python3
2+
"""
3+
Capture CISCO IOS device configurations. Used by CA Spectrum NCM
4+
"""
5+
6+
import re
7+
import sys
8+
9+
from netmiko import ConnectHandler, NetMikoAuthenticationException
10+
11+
HOSTNAME = sys.argv[1]
12+
USER = sys.argv[2]
13+
PASS = sys.argv[3]
14+
ENABLE = sys.argv[4]
15+
TIMEOUT = int(sys.argv[5])
16+
CONFIG_TYPE = sys.argv[6] if len(sys.argv) > 6 else 'running'
17+
18+
GLOBAL_DELAY_FACTOR = 2
19+
SHOW_DELAY_FACTOR = 5
20+
21+
22+
def main():
23+
cisco_device = {
24+
'device_type': 'cisco_ios_telnet',
25+
'ip': HOSTNAME,
26+
'username': USER,
27+
'password': PASS,
28+
'secret': ENABLE,
29+
'timeout': TIMEOUT,
30+
'global_delay_factor': GLOBAL_DELAY_FACTOR
31+
}
32+
33+
try:
34+
net_connect = ConnectHandler(**cisco_device)
35+
except NetMikoAuthenticationException:
36+
cisco_device['password'] = ENABLE
37+
try:
38+
net_connect = ConnectHandler(**cisco_device)
39+
except NetMikoAuthenticationException:
40+
print('Authentication Error', file=sys.stderr)
41+
exit(251)
42+
except Exception:
43+
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
44+
exit(1)
45+
46+
try:
47+
net_connect.enable()
48+
except Exception:
49+
pass
50+
51+
output = ''
52+
53+
if CONFIG_TYPE == 'startup':
54+
output = net_connect.send_command('show startup-config', delay_factor=SHOW_DELAY_FACTOR)
55+
elif CONFIG_TYPE == 'running':
56+
output = net_connect.send_command('show running-config', delay_factor=SHOW_DELAY_FACTOR)
57+
else:
58+
net_connect.disconnect()
59+
print('Invalid configuration type.\nPlease specify either running or startup', file=sys.stderr)
60+
exit(5)
61+
62+
output.lstrip()
63+
conf = ''
64+
65+
for line in output.split('\n'):
66+
if not re.match('^(Using|Building configuration|Current configuration)', line):
67+
conf += line + '\n'
68+
69+
print(conf.lstrip())
70+
71+
net_connect.disconnect()
72+
73+
74+
if __name__ == '__main__':
75+
main()

Diff for: cisco_ios_config_capture.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python3
2+
"""
3+
Capture CISCO IOS device configurations. Used by CA Spectrum NCM
4+
"""
5+
6+
import re
7+
import sys
8+
from platform import node
9+
10+
from netmiko import (ConnectHandler, NetMikoAuthenticationException,
11+
NetMikoTimeoutException)
12+
13+
HOSTNAME = sys.argv[1]
14+
USER = sys.argv[2]
15+
PASS = sys.argv[3]
16+
ENABLE = sys.argv[4]
17+
TIMEOUT = int(sys.argv[5])
18+
CONFIG_TYPE = sys.argv[6] if len(sys.argv) > 6 else 'running'
19+
20+
GLOBAL_DELAY_FACTOR = 2
21+
SHOW_DELAY_FACTOR = 5
22+
23+
if re.match('^cadsud1gnocnm005', node(), flags=re.IGNORECASE):
24+
GLOBAL_DELAY_FACTOR = 30
25+
SHOW_DELAY_FACTOR = 40
26+
27+
28+
def main():
29+
cisco_device = {
30+
'device_type': 'cisco_ios',
31+
'ip': HOSTNAME,
32+
'username': USER,
33+
'password': PASS,
34+
'secret': ENABLE,
35+
'timeout': TIMEOUT,
36+
'global_delay_factor': GLOBAL_DELAY_FACTOR
37+
}
38+
39+
try:
40+
net_connect = ConnectHandler(**cisco_device)
41+
except NetMikoTimeoutException:
42+
cisco_device['device_type'] = 'cisco_ios_telnet'
43+
try:
44+
net_connect = ConnectHandler(**cisco_device)
45+
except NetMikoTimeoutException:
46+
print('Connection Timeout', file=sys.stderr)
47+
exit(3)
48+
except NetMikoAuthenticationException:
49+
print('Authentication Error', file=sys.stderr)
50+
exit(251)
51+
except Exception:
52+
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
53+
exit(1)
54+
except NetMikoAuthenticationException:
55+
print('Authentication Error', file=sys.stderr)
56+
exit(252)
57+
except Exception:
58+
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
59+
exit(1)
60+
61+
try:
62+
net_connect.enable()
63+
except Exception:
64+
pass
65+
66+
output = ''
67+
68+
if CONFIG_TYPE == 'startup':
69+
output = net_connect.send_command('show startup-config', delay_factor=SHOW_DELAY_FACTOR)
70+
elif CONFIG_TYPE == 'running':
71+
output = net_connect.send_command('show running-config view full', delay_factor=SHOW_DELAY_FACTOR)
72+
if 'Invalid input detected' in output:
73+
output = net_connect.send_command('show running-config', delay_factor=SHOW_DELAY_FACTOR)
74+
else:
75+
net_connect.disconnect()
76+
print('Invalid configuration type.\nPlease specify either running or startup', file=sys.stderr)
77+
exit(5)
78+
79+
output.lstrip()
80+
conf = ''
81+
82+
for line in output.split('\n'):
83+
if not re.match('^(Using|Building configuration|Current configuration)', line):
84+
conf += line + '\n'
85+
86+
print(conf.lstrip())
87+
88+
net_connect.disconnect()
89+
90+
91+
if __name__ == '__main__':
92+
main()

Diff for: cisco_nx7k_config_capture.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/python3
2+
"""
3+
Capture Cisco Nexus 7k device configurations. Used by CA Spectrum NCM
4+
"""
5+
6+
import re
7+
import sys
8+
9+
from netmiko import (ConnectHandler, NetMikoAuthenticationException, NetMikoTimeoutException)
10+
11+
HOSTNAME = sys.argv[1]
12+
USER = sys.argv[2]
13+
PASS = sys.argv[3]
14+
ENABLE = sys.argv[4]
15+
TIMEOUT = int(sys.argv[5])
16+
CONFIG_TYPE = sys.argv[6] if len(sys.argv) > 6 else 'running'
17+
18+
GLOBAL_DELAY_FACTOR = 2
19+
SHOW_DELAY_FACTOR = 5
20+
21+
22+
def main():
23+
"""Connect to the device"""
24+
cisco_device = {
25+
'device_type': 'cisco_nxos',
26+
'ip': HOSTNAME,
27+
'username': USER,
28+
'password': PASS,
29+
'secret': ENABLE,
30+
'timeout': TIMEOUT,
31+
'global_delay_factor': GLOBAL_DELAY_FACTOR
32+
}
33+
34+
try:
35+
net_connect = ConnectHandler(**cisco_device)
36+
except NetMikoTimeoutException:
37+
print('Connection Timeout', file=sys.stderr)
38+
exit(3)
39+
except NetMikoAuthenticationException:
40+
print('Authentication Error', file=sys.stderr)
41+
exit(252)
42+
except Exception:
43+
print('Unexpected error:', sys.exc_info()[0], file=sys.stderr)
44+
exit(1)
45+
46+
# Enter enable mode
47+
try:
48+
net_connect.enable()
49+
except Exception:
50+
print('Unable to enter enable mode', file=sys.stderr)
51+
exit(252)
52+
53+
if CONFIG_TYPE == 'startup':
54+
command = 'show startup-config vdc-all'
55+
elif CONFIG_TYPE == 'running':
56+
command = 'show running-config vdc-all'
57+
else:
58+
net_connect.disconnect()
59+
print('Invalid configuration type.\nPlease specify either running or startup', file=sys.stderr)
60+
exit(5)
61+
62+
output = net_connect.send_command(command)
63+
64+
net_connect.disconnect()
65+
66+
print(output)
67+
68+
69+
if __name__ == '__main__':
70+
main()
71+
72+
73+

0 commit comments

Comments
 (0)