Aruba WIFI Guest帳號建立自動化 Part 2

3.6k 詞

前言

上次寫完程式整個運作流程,今天來寫寫這支程式的編寫概念吧~
では はじまるよ(〃∀〃)

首先說一下整個架構,程式一共有以下幾個

  1. Aruba.py #主程式
  2. create.py #建置帳號
  3. change.py #修改
  4. delete.py #刪除
  5. getlist.py #取得名單
  6. getlist2.py #取得名單
    我知道getlist應該可以兩個合成一個使用,但因為這樣還要再花不少時間修改能力不夠 所以我還是拆成兩個寫比較簡單,當然這樣的話運作速度肯定會受影響,但還是比以前的方式快很多了(́◉◞౪◟◉‵)

Aruba.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
import tkinter as tk
import create, getlist, getlist2

def handle_selection(event, listbox):
selection_index = listbox.curselection()
if selection_index:
if selection_index[0] == 0:
create.main()
elif selection_index[0] == 1:
getlist.main()
elif selection_index[0] == 2:
getlist2.main()

def main():
root = tk.Tk()
root.title('Wifi 申請調整')
root.geometry('200x200')

listbox = tk.Listbox(root)
listbox.insert(1, '新建Guest帳號')
listbox.insert(2, '調整帳號時間')
listbox.insert(3, '刪除帳號')
listbox.pack()

listbox.bind('<Double-Button-1>', lambda event: handle_selection(event, listbox))
root.protocol("WM_DELETE_WINDOW", root.quit)
root.mainloop()

if __name__ == "__main__":
main()

透過這支程式來建構出所謂的 Main 選單

create.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
'''
製作人:Luca Yao
時間:2024/04/02
'''

import paramiko, os, requests, json, urllib3, re, win32print, sys
import tkinter as tk
from tkinter import simpledialog
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

urllib3.disable_warnings()

def main():
global START_TIME
global END_TIME
global MM_USERNAME

END_TIME = None
START_TIME = None

def validate_datetime_format(input_str):
datetime_pattern = r"\d{2}/\d{2}/\d{4} \d{2}:\d{2}"

if re.match(datetime_pattern, input_str):
return True
else:
return False

def get_start_time():
global START_TIME
start_time = datetime.now().strftime("%m/%d/%Y %H:%M")
if start_time:
START_TIME = start_time.strip()

def get_end_time():
global END_TIME
root = tk.Tk()
root.withdraw()

end_time = simpledialog.askstring("無線WIFI", "請輸入結束時間(MM/DD/YYYY TT:MM):")

if not end_time:
return

if validate_datetime_format(end_time.strip()):
END_TIME = end_time.strip()
return
else:
tk.messagebox.showwarning("警告", "日期格式不正確,請參照 MM/DD/YYYY TT:MM 格式重新輸入。")

if end_time:
END_TIME = end_time.strip()

def get_mm_username():
global MM_USERNAME
root = tk.Tk()
root.withdraw()

mm_username = simpledialog.askstring("請輸入帳號", "請輸入使用者帳號")

if mm_username:
MM_USERNAME = mm_username.strip()

return mm_username

def get_mm_password():
global MM_PASSWORD
root = tk.Tk()
root.withdraw()

mm_password = simpledialog.askstring("請輸入密碼", "請輸入使用者密碼")

if mm_password:
MM_PASSWORD = mm_password.strip()

return mm_password

def get_mm_comments():
global MM_COMMENTS
root = tk.Tk()
root.withdraw()

while True:
mm_comments = simpledialog.askstring("請說明用途", "例如:給誰使用,少輸入五個字元")

if mm_comments is None:
return None
if mm_comments and len(mm_comments.strip()) >= 5:
MM_COMMENTS = mm_comments.strip()
break
else:
tk.messagebox.showwarning("警告", "請至少輸入五個字元")
return MM_COMMENTS

def connect_to_aruba_mm(hostname, username, password, command):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=username, password=password, port=22)

stdin, stdout, stderr = ssh_client.exec_command(command)

output = stderr.read().decode('utf-8')

if stderr.channel.recv_exit_status() != 0:
print("Error:", stderr.read().decode('utf-8'))

ssh_client.close()

return output
except Exception as e:
print(f"Error: {e}")
return None
finally:
if ssh_client is not None:
ssh_client.close()

def print_to_default_printer(output):
try:
if output:
desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
file_path = os.path.join(desktop_path, "Wifi Guest Account.txt")
with open(file_path, "a") as file:
lines = output.splitlines()
filterd_lines = lines[1:]
adjusted_output = '\n'.join(filterd_lines)
file.write(adjusted_output)
else:
print("Output is empty. Print operation aborted.")
except Exception as e:
print(f"Error printing to printer: {e}")

def loginmc (device):
url="https://{}:4343/v1/api/login".format(device)
params = {
"username": "username",
"password": "password"
}

try:
# 使用 requests.get() 函式發送 GET 請求,將參數放在 params 中
response = requests.get(url, verify=False, params=params, timeout=2)

# 檢查回應的狀態碼,如果是 200 表示請求成功
if response.status_code == 200:
# 取得回應中的 cookie
cookie = response.json()['_global_result']['UIDARUBA']
print("Logged into Mobility Controller")
print("Cookie is: {}".format(cookie))
return cookie
else:
print("Error logging into Mobility Controller. Status code:", response.status_code)
return None
except requests.exceptions.RequestException as e:
print("Error logging into Mobility Controller:", e)
return None

def logoutmc (device):
url="https://{}:4343/v1/api/logout".format(device)
try:
respone = requests.post(url, timeout=2, verify=False)
print("Logged out from Mobility Controller")
except:
print("Error logging out of Mobility Controller")

def getRESTmc (device, cookie, url, filter):
aoscookie = dict(SESSION = cookie)
fullurl = "https://{}:4343/v1{}{}".format(device,url,filter)

print("Full URL for this request is : ")
print(fullurl)

try:
# If the response contains information, the content is converted to json format
response = requests.get(fullurl, verify=False, cookies=aoscookie, timeout=2)
return response.json()
except Exception as e:
print("Error", e)
return "No data"

def get_ip_address():
device = "192.168.1.1"
filter = "&command=show conductor-redundancy"
cookie = loginmc (device)

if cookie != "401":
url="/configuration/showcommand?UIDARUBA={}".format(cookie)
print("VLAN information for this controller:")
vlan_info = getRESTmc(device, cookie, url, filter)
print(vlan_info)

if "BACKUP" in vlan_info['_data'][1]:
match = re.search(r'IP Address is (\d+\.\d+\.\d+\.\d+)', vlan_info['_data'][2])
if match:
ip_address = match.group(1)
print("Found IP address:", ip_address)
return ip_address
elif "MASTER" in vlan_info['_data'][1]:
print("Found 'MASTER' in vlan info. Exiting...")
ip_address = device
print("Master:", ip_address)
return ip_address
logoutmc(device)
else:
print("Cannot obtain information from the controller")
return ip_address

def send_mail(aruba_mm_username, output):
sender_email = "aruba_reply@example.com"
receiver_email = f"it@example.com"
subject = f"【Aruba通報】由 {aruba_mm_username} 帳號建立 Wifi帳號"
body = f"Wifi帳號: {aruba_mm_username}\n\n {output}。\n\n"

# 建立郵件內容
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))

# 連接到 SMTP 伺服器並發送郵件
smtpobj = smtplib.SMTP("mail.protection.outlook.com", 25)
smtpobj.sendmail(sender_email, receiver_email, message.as_string())

get_mm_username()
get_mm_password()
get_mm_comments()
if not MM_COMMENTS:
exit()
get_end_time()
if not END_TIME:
exit()
else:
get_start_time()
default_printer = win32print.GetDefaultPrinter()
ip_address = get_ip_address()
aruba_mm_username = MM_USERNAME
aruba_mm_password = MM_PASSWORD
aruba_mm_comments = MM_COMMENTS
aruba_mm_command = f'local-userdb-guest add generate-username generate-password start-time {(START_TIME)} expiry time {(END_TIME)} comments {(aruba_mm_comments)}'
output = connect_to_aruba_mm(ip_address, aruba_mm_username, aruba_mm_password, aruba_mm_command)
if output is None:
tk.messagebox.showwarning("警告", "帳號密碼錯誤")
print(output)
if "successfully" in output:
tk.messagebox.showwarning("通知", "帳號建立完成\n(ゝ∀・)b")
print_to_default_printer(output)
send_mail(aruba_mm_username, output)
exit()
else:
print("Failed to get output. Print operation aborted.")
exit()

getlist.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import paramiko, os, requests, json, urllib3, re, win32print, sys, change
import tkinter as tk
from tkinter import simpledialog
from tkinter import ttk
from datetime import datetime

urllib3.disable_warnings()

def main():
global START_TIME
global END_TIME

END_TIME = None
START_TIME = None

def validate_datetime_format(input_str):
datetime_pattern = r"\d{2}/\d{2}/\d{4} \d{2}:\d{2}"

if re.match(datetime_pattern, input_str):
return True
else:
return False

def get_end_time():
root = tk.Tk()
root.withdraw()
end_time = simpledialog.askstring("無線WIFI", "期望修改的結束時間(MM/DD/YYYY TT:MM):")
if not end_time:
return

if validate_datetime_format(end_time.strip()):
END_TIME = end_time.strip()
return
else:
tk.messagebox.showwarning("警告", "日期格式不正確,請參照 MM/DD/YYYY TT:MM 格式重新輸入。")

if end_time:
END_TIME = end_time.strip()

def connect_to_aruba_mm(hostname, username, password, command):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=username, password=password, port=22)
stdin, stdout, stderr = ssh_client.exec_command(command)
output = stderr.read().decode('utf-8')

if stderr.channel.recv_exit_status() != 0:
print("Error:", stderr.read().decode('utf-8'))

ssh_client.close()
return output
except Exception as e:
print(f"Error: {e}")
return None

def print_to_default_printer(output):
try:
if output:
desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
file_path = os.path.join(desktop_path, "Wifi Guest Account.txt")
with open(file_path, "a") as file:
lines = output.splitlines()
filterd_lines = lines[1:]
adjusted_output = '\n'.join(filterd_lines)
file.write(adjusted_output)
else:
print("Output is empty. Print operation aborted.")
except Exception as e:
print(f"Error printing to printer: {e}")

def loginmc (device):
url="https://{}:4343/v1/api/login".format(device)
params = {
"username": "username",
"password": "password"
}

try:
# 使用 requests.get() 函式發送 GET 請求,將參數放在 params 中
response = requests.get(url, verify=False, params=params, timeout=2)

# 檢查回應的狀態碼,如果是 200 表示請求成功
if response.status_code == 200:
# 取得回應中的 cookie
cookie = response.json()['_global_result']['UIDARUBA']
print("Logged into Mobility Controller")
print("Cookie is: {}".format(cookie))
return cookie
else:
print("Error logging into Mobility Controller. Status code:", response.status_code)
return None
except requests.exceptions.RequestException as e:
print("Error logging into Mobility Controller:", e)
return None

def logoutmc (device):
url="https://{}:4343/v1/api/logout".format(device)
try:
respone = requests.post(url, timeout=2, verify=False)
print("Logged out from Mobility Controller")
except:
print("Error logging out of Mobility Controller")

def getRESTmc (device, cookie, url, filter):
aoscookie = dict(SESSION = cookie)
fullurl = "https://{}:4343/v1{}{}".format(device,url,filter)

print("Full URL for this request is : ")
print(fullurl)

try:
# If the response contains information, the content is converted to json format
response = requests.get(fullurl, verify=False, cookies=aoscookie, timeout=2)
return response.json()
except Exception as e:
print("Error", e)
return "No data"

def get_ip_address():
device = "192.168.1.1"
filter = "&command=show conductor-redundancy"
cookie = loginmc (device)

if cookie != "401":
url="/configuration/showcommand?UIDARUBA={}".format(cookie)
print("VLAN information for this controller:")
vlan_info = getRESTmc(device, cookie, url, filter)
print(vlan_info)

if "BACKUP" in vlan_info['_data'][1]:
match = re.search(r'IP Address is (\d+\.\d+\.\d+\.\d+)', vlan_info['_data'][2])
if match:
ip_address = match.group(1)
print("Found IP address:", ip_address)
return ip_address
elif "MASTER" in vlan_info['_data'][1]:
print("Found 'MASTER' in vlan info. Exiting...")
ip_address = device
return ip_address
logoutmc(device)
else:
print("Cannot obtain information from the controller")
return ip_address

def get_mm_username():
root = tk.Tk()
root.withdraw()

mm_username = simpledialog.askstring("請輸入帳號", "請輸入使用者帳號")

if mm_username:
MM_USERNAME = mm_username.strip()

return mm_username

def get_mm_password():
root = tk.Tk()
root.withdraw()

mm_password = simpledialog.askstring("請輸入密碼", "請輸入使用者密碼")

if mm_password:
MM_PASSWORD = mm_password.strip()

return mm_password

def get_account_list(ip_address, mm_username, mm_password):
aruba_mm_command = f'show local-userdb-guest verbose'
output = connect_to_aruba_mm(ip_address, mm_username, mm_password, aruba_mm_command)
return output

def extract_user_info(output):
# 使用正則表達式來匹配 Name 和 Expiry 的欄位
pattern = r'([A-Za-z0-9-]+)\s+\*+\s+\w+\s+([\w-]+)\s+\w+\s+\w+\s+\d+/\d+/\d+\s+\d+:\d+\s+(\d+/\d+/\d+\s+\d+:\d+)'
matches = re.findall(pattern, output)
matches = [(match[0], match[2], match[1]) for match in matches]
print("print extract_user_info: ", matches)
return matches

def on_double_click(event, tree):
item = tree.selection()[0]
# 獲取所選項目的值
values = tree.item(item, "values")
# 獲取用戶名
username = values[0] if values else None
# 如果用戶名不為空,則打印用戶名
if username:
ip_address = get_ip_address()
change.main(username, ip_address, mm_username, mm_password)
tree.winfo_toplevel().destroy()
return
else:
print("No User selected")

def show_windows(user_info):
global root
root = tk.Tk()
root.title('請點選需要調整的帳號')
root.geometry('400x200')
tree = ttk.Treeview(root)

tree['columns'] = ('Name', 'Expiry Date', 'Comments')
tree.column('#0', width=0, stretch=tk.NO)
tree.column('Name', anchor=tk.W, width=100)
tree.column('Expiry Date', anchor=tk.W, width=100)
tree.column('Comments', anchor=tk.W, width=100)

tree.heading('#0', text='', anchor=tk.W)
tree.heading('Name', text='帳號', anchor=tk.W)
tree.heading('Expiry Date', text='到期日', anchor=tk.W)
tree.heading('Comments', text='備註', anchor=tk.W)

for index, (username, expiry_date, comments) in enumerate(user_info, start=1):
tree.insert('', 'end', text=index, values=(username, expiry_date, comments))

tree.pack(expand=True, fill='both')
tree.bind('<Double-1>', lambda event: on_double_click(event, tree))
root.mainloop()


ip_address = get_ip_address()
mm_username = get_mm_username()
mm_password = get_mm_password()
output = get_account_list(ip_address, mm_username, mm_password)
if output is None:
tk.messagebox.showwarning("警告", "帳號密碼錯誤")
user_info = extract_user_info(output)
root = show_windows(user_info)

if __name__ == "__main__":
main()

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
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
'''
製作人:Luca Yao
時間:2024/04/02
'''

import paramiko, os, requests, json, urllib3, re, win32print, sys, getlist
import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

urllib3.disable_warnings()

def main(username, ip_address, mm_username, mm_password):
global START_TIME
global END_TIME
print(username)
execute_change(username, ip_address, mm_username, mm_password)

def execute_change(username, ip_address, mm_username, mm_password):
get_end_time()
if not END_TIME:
return
else:
default_printer = win32print.GetDefaultPrinter()
aruba_mm_command = f'local-userdb-guest modify username {(username)} expiry time {(END_TIME)}'
output = connect_to_aruba_mm(ip_address, mm_username, mm_password, aruba_mm_command)
if output:
messagebox.showwarning("失敗", "修改失敗")
else:
messagebox.showinfo("成功", "修改成功")
send_mail(username, END_TIME)
return

def validate_datetime_format(input_str):
datetime_pattern = r"\d{2}/\d{2}/\d{4} \d{2}:\d{2}"

if re.match(datetime_pattern, input_str):
return True
else:
return False

def get_end_time():
global END_TIME
root = tk.Tk()
root.withdraw()

end_time = simpledialog.askstring("無線WIFI", "請輸入結束時間(MM/DD/YYYY TT:MM):")
if not end_time:
return

if validate_datetime_format(end_time.strip()):
END_TIME = end_time.strip()
return
else:
tk.messagebox.showwarning("警告", "日期格式不正確,請參照 MM/DD/YYYY TT:MM 格式重新輸入。")

if end_time:
END_TIME = end_time.strip()

def connect_to_aruba_mm(hostname, username, password, command):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=username, password=password, port=22)

stdin, stdout, stderr = ssh_client.exec_command(command)

output = stderr.read().decode('utf-8')

if stderr.channel.recv_exit_status() != 0:
print("Error:", stderr.read().decode('utf-8'))

ssh_client.close()
return output
except Exception as e:
print(f"Error: {e}")
return None

def print_to_default_printer(output):
try:
if output:
desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
file_path = os.path.join(desktop_path, "Wifi Guest Account.txt")
with open(file_path, "a") as file:
lines = output.splitlines()
filterd_lines = lines[1:]
adjusted_output = '\n'.join(filterd_lines)
file.write(adjusted_output)
else:
print("Output is empty. Print operation aborted.")
except Exception as e:
print(f"Error printing to printer: {e}")

if __name__ == "__main__":
main()

else:
print("IP address not found")

def send_mail(username, END_TIME):
sender_email = "aruba_reply@example.com"
receiver_email = f"it@example.com"
subject = f"【Aruba通報】 {username} 帳號使用時間延長"
body = f"Wifi帳號: {username}\n\n已將時間延長至{END_TIME}。\n\n"

# 建立郵件內容
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))

# 連接到 SMTP 伺服器並發送郵件
smtpobj = smtplib.SMTP("mail.protection.outlook.com", 25)
smtpobj.sendmail(sender_email, receiver_email, message.as_string())

getlist2.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import paramiko, os, requests, json, urllib3, re, win32print, sys, delete
import tkinter as tk
from tkinter import simpledialog
from tkinter import ttk
from datetime import datetime

urllib3.disable_warnings()

def main():
global START_TIME
global END_TIME

END_TIME = None
START_TIME = None

def validate_datetime_format(input_str):
datetime_pattern = r"\d{2}/\d{2}/\d{4} \d{2}:\d{2}"

if re.match(datetime_pattern, input_str):
return True
else:
return False

def get_end_time():
root = tk.Tk()
root.withdraw()
end_time = simpledialog.askstring("無線WIFI", "期望修改的結束時間(MM/DD/YYYY TT:MM):")
if not end_time:
return

if validate_datetime_format(end_time.strip()):
END_TIME = end_time.strip()
return
else:
tk.messagebox.showwarning("警告", "日期格式不正確,請參照 MM/DD/YYYY TT:MM 格式重新輸入。")

if end_time:
END_TIME = end_time.strip()

def connect_to_aruba_mm(hostname, username, password, command):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=username, password=password, port=22)
stdin, stdout, stderr = ssh_client.exec_command(command)
output = stderr.read().decode('utf-8')

if stderr.channel.recv_exit_status() != 0:
print("Error:", stderr.read().decode('utf-8'))

ssh_client.close()
return output
except Exception as e:
print(f"Error: {e}")
return None

def print_to_default_printer(output):
try:
if output:
desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
file_path = os.path.join(desktop_path, "Wifi Guest Account.txt")
with open(file_path, "a") as file:
lines = output.splitlines()
filterd_lines = lines[1:]
adjusted_output = '\n'.join(filterd_lines)
file.write(adjusted_output)
else:
print("Output is empty. Print operation aborted.")
except Exception as e:
print(f"Error printing to printer: {e}")

def loginmc (device):
url="https://{}:4343/v1/api/login".format(device)
params = {
"username": "username",
"password": "password"
}

try:
# 使用 requests.get() 函式發送 GET 請求,將參數放在 params 中
response = requests.get(url, verify=False, params=params, timeout=2)

# 檢查回應的狀態碼,如果是 200 表示請求成功
if response.status_code == 200:
# 取得回應中的 cookie
cookie = response.json()['_global_result']['UIDARUBA']
print("Logged into Mobility Controller")
print("Cookie is: {}".format(cookie))
return cookie
else:
print("Error logging into Mobility Controller. Status code:", response.status_code)
return None
except requests.exceptions.RequestException as e:
print("Error logging into Mobility Controller:", e)
return None

def logoutmc (device):
url="https://{}:4343/v1/api/logout".format(device)
try:
respone = requests.post(url, timeout=2, verify=False)
print("Logged out from Mobility Controller")
except:
print("Error logging out of Mobility Controller")

def getRESTmc (device, cookie, url, filter):
aoscookie = dict(SESSION = cookie)
fullurl = "https://{}:4343/v1{}{}".format(device,url,filter)

print("Full URL for this request is : ")
print(fullurl)

try:
# If the response contains information, the content is converted to json format
response = requests.get(fullurl, verify=False, cookies=aoscookie, timeout=2)
return response.json()
except Exception as e:
print("Error", e)
return "No data"

def get_ip_address():
device = "192.168.1.1"
filter = "&command=show conductor-redundancy"
cookie = loginmc (device)

if cookie != "401":
url="/configuration/showcommand?UIDARUBA={}".format(cookie)
print("VLAN information for this controller:")
vlan_info = getRESTmc(device, cookie, url, filter)
print(vlan_info)

if "BACKUP" in vlan_info['_data'][1]:
match = re.search(r'IP Address is (\d+\.\d+\.\d+\.\d+)', vlan_info['_data'][2])
if match:
ip_address = match.group(1)
print("Found IP address:", ip_address)
return ip_address
elif "MASTER" in vlan_info['_data'][1]:
print("Found 'MASTER' in vlan info. Exiting...")
ip_address = device
return ip_address
logoutmc(device)
else:
print("Cannot obtain information from the controller")
return ip_address

def get_mm_username():
root = tk.Tk()
root.withdraw()

mm_username = simpledialog.askstring("請輸入帳號", "請輸入使用者帳號")

if mm_username:
MM_USERNAME = mm_username.strip()

return mm_username

def get_mm_password():
root = tk.Tk()
root.withdraw()

mm_password = simpledialog.askstring("請輸入密碼", "請輸入使用者密碼")

if mm_password:
MM_PASSWORD = mm_password.strip()

return mm_password

def get_account_list(ip_address, mm_username, mm_password):
aruba_mm_command = f'show local-userdb-guest verbose'
output = connect_to_aruba_mm(ip_address, mm_username, mm_password, aruba_mm_command)
return output

def extract_user_info(output):
pattern = r'([A-Za-z0-9-]+)\s+\*+\s+\w+\s+([\w-]+)\s+\w+\s+\w+\s+\d+/\d+/\d+\s+\d+:\d+\s+(\d+/\d+/\d+\s+\d+:\d+)'
matches = re.findall(pattern, output)
matches = [(match[0], match[2], match[1]) for match in matches]
print(matches)
return matches

def on_double_click(event, tree):
item = tree.selection()[0]
# 獲取所選項目的值
values = tree.item(item, "values")
# 獲取用戶名
username = values[0] if values else None
# 如果用戶名不為空,則打印用戶名
if username:
ip_address = get_ip_address()
delete.main(username, ip_address, mm_username, mm_password)
tree.winfo_toplevel().destroy()
return
else:
print("No User selected")

def show_windows(user_info):
global root
root = tk.Tk()
root.title('請點選需要調整的帳號')
root.geometry('400x200')
tree = ttk.Treeview(root)

tree['columns'] = ('Name', 'Expiry Date', 'Comments')
tree.column('#0', width=0, stretch=tk.NO)
tree.column('Name', anchor=tk.W, width=100)
tree.column('Expiry Date', anchor=tk.W, width=100)
tree.column('Comments', anchor=tk.W, width=100)

tree.heading('#0', text='', anchor=tk.W)
tree.heading('Name', text='帳號', anchor=tk.W)
tree.heading('Expiry Date', text='到期日', anchor=tk.W)
tree.heading('Comments', text='備註', anchor=tk.W)

for index, (username, expiry_date, comments) in enumerate(user_info, start=1):
tree.insert('', 'end', text=index, values=(username, expiry_date, comments))

tree.pack(expand=True, fill='both')
tree.bind('<Double-1>', lambda event: on_double_click(event, tree))
root.mainloop()

ip_address = get_ip_address()
mm_username = get_mm_username()
mm_password = get_mm_password()
output = get_account_list(ip_address, mm_username, mm_password)
if output is None:
tk.messagebox.showwarning("警告", "帳號密碼錯誤")
user_info = extract_user_info(output)
root = show_windows(user_info)

if __name__ == "__main__":
main()

delete.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
'''
製作人:Luca Yao
時間:2024/04/02
'''

import paramiko, os, requests, json, urllib3, re, win32print, sys, getlist2
import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


urllib3.disable_warnings()

def main(username, ip_address, mm_username, mm_password):
global START_TIME
global END_TIME
print(username)
execute_delete(username, ip_address, mm_username, mm_password)

def execute_delete(username, ip_address, mm_username, mm_password):
aruba_mm_command = f'local-userdb-guest del username {(username)} '
output = connect_to_aruba_mm(ip_address, mm_username, mm_password, aruba_mm_command)
if output:
messagebox.showwarning("失敗", "修改失敗")
else:
messagebox.showinfo("成功", "修改成功")
send_mail(username, mm_username)
return

def connect_to_aruba_mm(hostname, username, password, command):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=username, password=password, port=22)

stdin, stdout, stderr = ssh_client.exec_command(command)

output = stderr.read().decode('utf-8')

if stderr.channel.recv_exit_status() != 0:
print("Error:", stderr.read().decode('utf-8'))

ssh_client.close()
return output
except Exception as e:
print(f"Error: {e}")
return None

if __name__ == "__main__":
main()

else:
print("IP address not found")

def send_mail(username, mm_username):
sender_email = "aruba_reply@example.com"
receiver_email = f"it@example.com"
subject = f"【Aruba通報】 {username} 帳號已刪除"
body = f"Wifi帳號: {username}\n\n已由{mm_username}手動刪除 。\n\n"

# 建立郵件內容
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain"))

# 連接到 SMTP 伺服器並發送郵件
smtpobj = smtplib.SMTP("mail.protection.outlook.com", 25)
smtpobj.sendmail(sender_email, receiver_email, message.as_string())

以上就是整個程式運作的程式碼了,其實還可以再做修改啦,但想說一個小程式就不要花時間來做這個了,所以就這樣吧(。◕∀◕。)

但要給使用者使用,可不是寫好,丟給使用者也不會用阿~
所以下一篇我們來講講該怎麼打包,這樣使用者就會只看到如第一篇所說的小才犬囉
那麼我們下次見吧~

あばよ!