-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·109 lines (98 loc) · 3.68 KB
/
main.py
File metadata and controls
executable file
·109 lines (98 loc) · 3.68 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
#!/usr/bin/python3
import os
import subprocess
def command(command):
if command == "ping":
return "pong"
elif command == "list interfaces":
return " ".join(interfaces)
elif command == "list interfaces up":
tooutput = []
for i in interfaces:
j = open(f"/sys/class/net/{i}/operstate")
if j.read().strip() == "up":
tooutput.append(i)
return " ".join(tooutput)
elif command.startswith("interface"):
a = command.split(" ")
if a[2] == "wake":
if a[1].startswith("e") and a[1] in interfaces:
print(f"{a[1]} setting state up")
subprocess.run(["ip", "link", "set", a[1], "up"])
print(f"{a[1]} get network ip by dhcp")
subprocess.run(["dhcpcd", a[1]])
return "ok"
elif a[1].startswith("w") and a[1] in interfaces:
print(f"{a[1]} setting state up")
subprocess.run(["ip", "link", "set", a[1], "up"])
print(f"{a[1]} disable rfkill")
subprocess.run(["rfkill", "unblock", "wifi"])
print(f"{a[1]} create config")
try:
result = subprocess.run(["wpa_passphrase", a[3], a[4]],
capture_output=True,
text=True,
check=True)
except:
result = subprocess.run(["wpa_passphrase", a[3]],
capture_output=True,
text=True,
check=True)
file = open(f"/etc/{a[1]}_unet.conf", "w")
file.write(result.stdout)
file.close()
subprocess.run(["wpa_supplicant", "-B", "-i", a[1], "-c", f"/etc/{a[1]}_unet.conf"])
subprocess.run(["dhcpcd", "-w", a[1]])
return "ok"
elif a[2] in interfaces:
return "available, not supported"
else:
return "not exist"
elif a[1] == "scan" and a[2].startswith("w"):
return "\n".join(scanwifi(a[2]))
def scanwifi(iface):
result = subprocess.run(["iw", "dev", iface, "scan"],
capture_output=True,
text=True)
ssids = []
for line in result.stdout.splitlines():
line = line.strip()
if line.startswith("SSID:"):
ssid = line[len("SSID:"):].strip()
if ssid:
ssids.append(ssid)
return ssids
print("creating interface list...")
interfaces = [i for i in os.listdir("/sys/class/net/") if i != "lo"]
print("found interfaces ", end="")
for i in interfaces:
print(i + " ", end="")
print("")
FIFOin = "/tmp/uzbeknetwork.fifo.in"
FIFOout = "/tmp/uzbeknetwork.fifo.out"
print(f"FIFO input located at {FIFOin}")
if not os.path.exists(FIFOin):
os.mkfifo(FIFOin)
print(f"FIFO input created at {FIFOin}")
print(f"FIFO output located at {FIFOout}")
if not os.path.exists(FIFOout):
os.mkfifo(FIFOout)
print(f"FIFO output created at {FIFOout}")
print(f"running autorun commands...")
try:
for i in open("/etc/uzbeknetwork.autostart").read().split("\n"):
print(command(i))
print("autorun command list end")
except:
print("no autorun file, skipping...")
print("now polling fifo")
while True:
with open(FIFOin, "r") as fifo_in, open(FIFOout, "w") as fifo_out:
for line in fifo_in:
interfaces = [i for i in os.listdir("/sys/class/net/") if i != "lo"]
line = line.strip()
if not line:
continue
print(f"received line: {line}")
fifo_out.write(command(line) + "\n")
fifo_out.flush()