-
Notifications
You must be signed in to change notification settings - Fork 5
/
mac-change.py
81 lines (64 loc) · 2 KB
/
mac-change.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
76
77
78
79
80
81
#!/usr/bin/env python3
# Requires Python 3 and Netmiko
# imports
from netmiko import Netmiko
from getpass import getpass
# ask for MAC in HHHH.HH format
userMAC = input("\nVendor MAC for the devices. Must be HHHH.HH format: ")
# ask for VLAN
userVLAN = input("VLAN would you like the devices to be in: ")
# ask for switch IP
userSwitch = input("IP of the switch the devices connect to: ")
# log into switchIP
while True:
try:
username = input("\nUsername: ")
password = getpass()
myDevice = {
'host': userSwitch,
'username': username,
'password': password,
'device_type': 'cisco_ios',
}
print("\nLogging in now...")
net_connect = Netmiko(**myDevice)
net_connect.enable()
break
except:
print("Login failed. Please try again.")
continue
print("Searching for MAC addresses...")
# run sh mac add | inc userMAC
showMAC = net_connect.send_command("show mac add | inc "+userMAC)
# grabs interfaces
interfaces = [];
for line in showMAC.splitlines():
#only grabs interfaces that are not equal to userVLAN
if line[2:4] != userVLAN:
interfaces.append(line[38:47].strip())
print("\nFound these interfaces:")
print(interfaces)
# starts a loop to iterate
for intf in interfaces:
output = net_connect.send_command("sh int "+intf+" status");
# skip if trunk
if "trunk" in output:
print("\n" +intf)
print("Skipping, port is a trunk.")
else:
print("\n" +intf)
print("Modifying, please wait...")
# issue commands
config_commands = [
'int '+intf,
'shut',
'swi acc vlan '+userVLAN,
'no shut']
net_connect.send_config_set(config_commands)
print("Done!")
# write mem
print("\nWriting to memory, please wait...")
net_connect.send_command('write mem')
# exit program
print("\nVLAN changes completed! Exiting program...")
exit()