-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWLC_Utility.py
205 lines (150 loc) · 6.44 KB
/
WLC_Utility.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
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
203
204
205
### Wireless LAN Controller Utility 2.0 - Jarred Reid
### Changelog:
### Version 1.0 (5/20/2019) : Initial Release
### Version 1.6 (7/3/2019): Rewrote program. Ability to config individual controllers has been added.
### Version 2.0 (12/18/2019): Batch support is being been added to the program. Use "wlc_config.ini" to add IPs. Hostname/MAC support will be added very shortly.
import time, paramiko, os, sys
wlc_session = paramiko.SSHClient()
wlc_session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
### As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter
os.chdir(sys.path[0])
def get_Credential():
credentials = ["username", "password"]
credentials[0] = input("Enter the administrator's username: ")
credentials[1] = input("Enter the administrator's password: ")
return credentials
def get_WLC_Info():
wlc = ["ip_addr", "port", "int"]
wlc[0] = input("Enter the WLC's IP address: ")
wlc[1] = input("Enter the WLC's port (leave blank for default port 22): ")
wlc[2] = input("Enter the WLC interface name: ")
### In order for the interface name to be parsed correctly by the CLI, spaces must be appended before and after the defined interface string.
wlc[2] = (" " + wlc[2] + " ")
if wlc[1] == "":
wlc[1] = "22"
return wlc
def import_Config():
from configparser import ConfigParser
config_file = 'wlc_config.ini'
parser = ConfigParser()
parser.read(config_file)
import_IP = parser.get('CONNECTION', 'ip',).split(',')
import_Port = parser.get('CONNECTION', 'port').split(',')
import_Int = parser.get('CONNECTION', 'interface').split(',')
wlc = ["ip_addr", "port", "int"]
### Figure out how to parse the list entries 1,2 as strings and strip the list brackets and quotes so that all settings are configured from the ini
wlc[0] = import_IP
wlc[1] = "22"
wlc[2] = "keynet"
return wlc
def get_Device_Info():
mac_addr = input("Please enter the MAC address: ")
### The MAC address will not be accepted by the CLI unless the hyphens are replaced by colons.
if "-" in mac_addr:
mac_addr = mac_addr.replace('-', ':')
dev_description = input("Enter the device's description: ")
return mac_addr, dev_description
def set_Operation():
print("""
[1] Add device to whitelist
[2] Remove device from whitelist
""")
set_op = int(input("Please make a selection: "))
if set_op == 1:
dev_operation = "add"
if set_op == 2:
dev_operation = "delete"
elif set_op != 1 or 2:
print("Please enter a valid selection from above: ")
return dev_operation
def start_Operation():
### Initiate SSH tunnel to the WLC
wlc_session.connect(wlc[0], port=wlc[1], username='null', password='null')
### Print the stdout to client terminal
wlc_ssh_class = wlc_session.invoke_shell()
time.sleep(0.1)
wlc_ssh_class.send(credentials[0] + '\n')
time.sleep(0.1)
wlc_ssh_class.send(credentials[1] + '\n')
time.sleep(0.1)
### Disable paging to display properly
wlc_ssh_class.send('config paging disable'+'\n')
time.sleep(0.1)
strip_login_text = wlc_ssh_class.recv(1024).decode('utf-8')
### Performing the requested operation on the WLC
wlc_ssh_class.send('config macfilter ' + dev_operation + ' ' + mac_addr + ' 1 ' + wlc[2] + ' ' + dev_description + '\n')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
print(output)
### Saving the running config
wlc_ssh_class.send('save config')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
wlc_ssh_class.send('y')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
print(output)
def auto_Operation(ip, wlc, credentials, dev_operation, mac_addr, dev_description):
### Initiate SSH tunnel to the WLC
wlc_session.connect(ip, port=wlc[1], username='null', password='null')
### Print the stdout to client terminal
wlc_ssh_class = wlc_session.invoke_shell()
time.sleep(0.1)
wlc_ssh_class.send(credentials[0] + '\n')
time.sleep(0.1)
wlc_ssh_class.send(credentials[1] + '\n')
time.sleep(0.1)
### Disable paging to display properly
wlc_ssh_class.send('config paging disable'+'\n')
time.sleep(0.1)
strip_login_text = wlc_ssh_class.recv(1024).decode('utf-8')
### Performing the requested operation on the WLC
if dev_operation == "add":
wlc_ssh_class.send('config macfilter ' + dev_operation + ' ' + mac_addr + ' 1 ' + wlc[2] + ' ' + dev_description + '\n')
if dev_operation == "remove":
wlc_ssh_class.send('config macfilter ' + dev_operation + ' ' + mac_addr + '\n')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
print(output)
### Saving the running config
wlc_ssh_class.send('save config')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
wlc_ssh_class.send('y')
time.sleep(0.1)
output = wlc_ssh_class.recv(2048).decode('utf-8', 'backslashreplace')
print(output)
def start_Main():
if prompt_Config == "1":
use_Config = "TRUE"
wlc = import_Config()
mac_addr, dev_description = get_Device_Info()
dev_operation = set_Operation()
print(dev_description + " with MAC address: " + mac_addr + " will be " + dev_operation + "ed to/from the following controller IP(s): ")
print(wlc[0])
print()
for ip in wlc[0]:
print("Adding to Controller with IP: " + ip)
print()
print("Controller Output: ")
auto_Operation(ip, wlc, credentials, dev_operation, mac_addr, dev_description)
if prompt_Config == "2":
use_Config = "FALSE"
wlc = get_WLC_Info()
mac_addr, dev_description = get_Device_Info()
dev_operation = set_Operation()
start_Operation()
print("""
cisco Wireless LAN Controller Utility v2
----------------------------------------
""")
credentials = get_Credential()
prompt_Config = input("Press [1] to import WLC info from configuration file, otherwise press enter: ")
while True:
start_Main()
# get credentials
# get wireless controller information
# define operation
# get MAC info
# execute
# loop/exit