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()
|