-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwakeonlan_script.py
More file actions
136 lines (130 loc) · 4.41 KB
/
wakeonlan_script.py
File metadata and controls
136 lines (130 loc) · 4.41 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
133
134
135
136
# -*- coding: utf-8 -*-
import sys
import tkinter
import wakeonlan
# GUI mode switch
EnableGUI = False
# Default MAC address
DefaultAddress = "FF:FF:FF:FF:FF:FF"
# Exit button definite
def GUIEnding():
WindowUI.destroy()
sys.exit(0)
# WoL, GUI mode
def GUIAction():
# Get MAC address
InputMacAddress = TextMAC.get()
# Get IP address
BroadcastAddress = TextIP.get()
# Get port
BroadcastPort = TextPort.get()
# Using default MAC adress
if str(InputMacAddress) == "":
InputMacAddress = DefaultAddress
else:
pass
# Using default IP adress
if str(BroadcastAddress) == "":
BroadcastAddress = None
else:
pass
# Using default port
if str(BroadcastPort) == "":
BroadcastPort = None
else:
pass
# Translate and check
PayloadString = wakeonlan.AddressTranslatedintoBytes(InputMacAddress)
# If format incorrect
if type(PayloadString) is int:
del InputMacAddress
# If format correct
elif type(PayloadString) is bytes:
wakeonlan.LocalBroadcasting(InputMacAddress,AddressConfig=BroadcastAddress,PortConfig=BroadcastPort)
del InputMacAddress,BroadcastAddress,BroadcastPort
# WoL, CLI mode
def WakeupScript(DefaultAddress):
# MAC address
InputMacAddress = input(f"Enter MAC address, default is {DefaultAddress} : ")
# Default MAC address
if str(InputMacAddress) == "":
InputMacAddress = DefaultAddress
else:
pass
# Check MAC addrrss
AddressCheck = wakeonlan.AddressTranslatedintoBytes(InputMacAddress)
# Incorrect MAC address input
if type(AddressCheck) is int:
return 401
# Check pass
elif type(AddressCheck) is bytes:
# Input IP address
BroadcastAddress = input("Enter IP address, default is Broadcast: ")
if str(BroadcastAddress) == "":
BroadcastAddress = None
else:
pass
# Input port config
BroadcastPort = input("Enter Port number, default is port 9: ")
if str(BroadcastPort) == "":
BroadcastPort = None
else:
pass
# Broadcasting magic packet
print("Magic Packet Broadcasting...")
return wakeonlan.LocalBroadcasting(InputMacAddress,AddressConfig=BroadcastAddress,PortConfig=BroadcastPort)
# Runtime
try:
# CLI mode
if EnableGUI is False:
CheckResult = WakeupScript(DefaultAddress)
if type(CheckResult) is int:
print("Error occurred, please retry.\r\n")
sys.exit(0)
else:
SuccessTime = wakeonlan.GetTime()
print(f"{SuccessTime} | Magic packet broadcast successfully.\r\n")
sys.exit(0)
# GUI, Tkinter mode
elif EnableGUI is True:
# Tkinter main windows configuration
WindowUI = tkinter.Tk()
WindowUI.title("Wake-on-LAN GUI")
WindowUI.minsize(330,320)
WindowUI.maxsize(330,320)
WindowUI.grid_columnconfigure(0,weight=1)
# Top description
Label = tkinter.Label(WindowUI,text=f"Enter MAC address, default is {DefaultAddress}")
Label.grid(row=0,ipadx=5,pady=5)
# Mac address input entry
TextMAC = tkinter.Entry(WindowUI,justify="center")
TextMAC.grid(row=1,ipadx=5,pady=5)
# Top description
Label = tkinter.Label(WindowUI,text="Enter IP address, default is Broadcast")
Label.grid(row=2,ipadx=5,pady=5)
# IP address input entry, Default is broadcast mode
TextIP = tkinter.Entry(WindowUI,justify="center")
TextIP.grid(row=3,ipadx=5,pady=5)
# Broadcasting port input entry
Label = tkinter.Label(WindowUI,text="Enter broadcasting port, default is 9")
Label.grid(row=4,ipadx=5,pady=5)
# Mac address input entry
TextPort = tkinter.Entry(WindowUI,justify="center")
TextPort.grid(row=5,ipadx=5,pady=5)
# Command
ButtonWake = tkinter.Button(WindowUI,text="Wake",width=20,command=GUIAction)
ButtonWake.grid(row=8,ipadx=5,pady=5)
# Exit button
ButtonExit = tkinter.Button(WindowUI,text="Exit",width=20,command=GUIEnding)
ButtonExit.grid(row=9,ipadx=5,pady=5)
# Loop Render
WindowUI.mainloop()
# Command quit
except KeyboardInterrupt:
print("Script ending. Goodbye...\r\n")
sys.exit(0)
# Error
except Exception:
print("Script has stopped working due to Error occurred.\r\n")
sys.exit(0)
# 2024.03.28