-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecover-Disabled-Interfaces.py
More file actions
132 lines (117 loc) · 6.89 KB
/
Recover-Disabled-Interfaces.py
File metadata and controls
132 lines (117 loc) · 6.89 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
from netmiko import ConnectHandler, ssh_exception
from time import sleep
import subprocess
import getpass
import time
import sys
# function to connect device and get interface status
def get_ports(network_devices):
"""Connection to device to get current port statused"""
for device in network_devices:
try:
Connect_to_device = ConnectHandler(**device)
Connect_to_device.enable()
find_err_ints = Connect_to_device.send_command('Show interface status err-disabled')
print(find_err_ints)
for interface in find_err_ints.splitlines():
if 'psecure-violation' in interface:
print ('\n----------- Found disabled interface cause of "p-secure" -----------')
_connect_fix_print_psecure_ports(interface, Connect_to_device)
elif 'bpduguard' in interface:
print ('\n\n\n----------- Found disabled interface cause of "bpduguard" -----------')
_connect_fix_print_bpduguard_ports(interface, Connect_to_device)
elif 'loopback' in interface:
print ('\n\n\n----------- Found disabled interface cause of "loopback" -----------')
_connect_fix_print_loopback_ports(interface, Connect_to_device)
elif 'dhcp-rate-limit' in interface:
print ('\n\n\n----------- Found disabled interface cause of "dhcp-rate-limit" -----------')
_connect_fix_print_bpduguard_ports(interface, Connect_to_device)
except (ssh_exception.AuthenticationException, EOFError):
print(f'Authentication Error Device: {host} . Authentication Error')
except ssh_exception.NetmikoTimeoutException:
print(f'Could not connect to {host} . Reason: Connection Timeout')
# Function for bpduguard violation
def _connect_fix_print_bpduguard_ports(interface, Connect_to_device):
try:
try:
To_Excecute = Connect_to_device.send_config_set([f'interface {interface.split()[0]}' ,'shut','no shut'])
_loading()
print('interface shutdown and no shutdown')
To_Excecute = Connect_to_device.send_command(f'show interfaces {interface.split()[0]} status')
if 'err-disabled' in To_Excecute:
print('interface still disabled!')
else:
print('\n-------------------------------- interfaces status -------------------------------- ')
print(To_Excecute)
print("--------------------------------------------------------------------------------------")
except (ssh_exception.AuthenticationException, EOFError):
print(f'Authentication Error Device. Authentication Error')
except ssh_exception.NetmikoTimeoutException:
print(f'Could not connect. Reason: Connection Timeout')
except IndexError:
pass
# Function for p-secure violation
def _connect_fix_print_psecure_ports(interface, Connect_to_device):
try:
try:
Connect_to_device.send_command(f'clear port-security sticky interface {interface.split()[0]}')
To_Excecute = Connect_to_device.send_config_set([f'interface {interface.split()[0]}' ,'shut','no shut'])
_loading()
print('MAC address cleared from interface!')
print('interface shutdown and no shutdown')
To_Excecute = Connect_to_device.send_command(f'show interfaces {interface.split()[0]} status')
if 'err-disabled' in To_Excecute:
print('MAC address cleared but interface still in err-disabled status!\nTrying to clear all sticky MAC address ...')
Connect_to_device.send_command('clear port-security sticky')
Connect_to_device.send_config_set([f'interface {interface.split()[0]}' ,'shut','no shut'])
_loading()
To_Excecute = Connect_to_device.send_command(f'show interfaces {interface.split()[0]} status')
print('All Sticky MAC addresses cleared!')
print('\n-------------------------------- interfaces status -------------------------------- ')
print(To_Excecute)
else:
print('\n-------------------------------- interfaces status -------------------------------- ')
print(To_Excecute)
print("--------------------------------------------------------------------------------------")
except (ssh_exception.AuthenticationException, EOFError):
print(f'Authentication Error Device. Authentication Error')
except ssh_exception.NetmikoTimeoutException:
print(f'Could not connect. Reason: Connection Timeout')
except IndexError:
pass
# Function for loopback violation
def _connect_fix_print_loopback_ports(interface, Connect_to_device):
try:
try:
To_Excecute = Connect_to_device.send_config_set([f'interface {interface.split()[0]}' ,'shut','no shut'])
_loading()
print('interface shutdown and no shutdown')
print(To_Excecute)
if 'err-disabled' in To_Excecute:
print('Interface still in Loopback Error!\nThe source interface receives the keepalive packet that it sent out!')
else:
To_Excecute = Connect_to_device.send_command(f'show interfaces {interface.split()[0]} status')
print(To_Excecute)
except ssh_exception.NetmikoTimeoutException:
print(f'Could not connect. Reason: Connection Timeout')
except IndexError:
pass
#Function for loading
def _loading():
print("Working! ")
#animation = ["10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"]
animation = ["[■□□□□□□□□□]","[■■□□□□□□□□]", "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]", "[■■■■■■□□□□]", "[■■■■■■■□□□]", "[■■■■■■■■□□]", "[■■■■■■■■■□]", "[■■■■■■■■■■]"]
for i in range(len(animation)):
time.sleep(0.3)
sys.stdout.write("\r" + animation[i % len(animation)])
sys.stdout.flush()
print("\n")
print('Please enter Device IP address: ')
host = (str(input()))
print('Please enter your username: ')
get_username = (str(input()))
get_pass = getpass.getpass(prompt='Please enter your password: ', stream=None)
print('Please Wait...')
network_devices = [{'host':host,'username':'get_username','password':'get_pass','device_type':'cisco_ios'}]
get_ports(network_devices):
print("\n-------------------------- End --------------------------")