-
Notifications
You must be signed in to change notification settings - Fork 0
/
konzola.py
executable file
·297 lines (249 loc) · 9.1 KB
/
konzola.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python3
import subprocess as sub
import sys
class Color:
RESET = "\033[0m"
GREEN = "\033[1;32m"
RED = "\033[1;31m"
YELLOW = "\033[1;33m"
def printLogo():
print(f"""{Color.GREEN}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @
@ |\\\\ /|| || |\\\\ || || @
@ ||\\\\//|| || ||\\\\ || || @
@ || || ROUTER \\\\ || || @
@ || || || || \\\\ || || @
@ || || || || \\\\|| || @
@ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@{Color.RESET}""")
def getCmd(confDict):
if confDict["change"]:
cmd = input(f"{Color.YELLOW}{confDict['ssid']}{Color.RESET}>!> ")
else:
cmd = input(f"{Color.GREEN}{confDict['ssid']}{Color.RESET}>>> ")
return cmd.strip().lower()
def runCmd(cmd):
if cmd in cmdsDict:
return cmdsDict[cmd]()
else:
print(f"{Color.YELLOW}Unrecognized command '{cmd}'{Color.RESET}")
return False
def save(confDict):
saveIfaceConf(confDict)
dhcpWrite(confDict)
hostapdWrite(confDict)
confDict["change"] = False
def saveIfaceConf(confDict):
with open("/etc/network/interfaces","w") as f:
f.write(f"""source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
allow-hotplug {confDict["iface"]}
iface {confDict["iface"]} inet static
address {confDict["ip"]}
netmask {confDict["mask"]}
""")
def calcSubnet(ip, mask):
subnet = []
for i,j in zip(ip, mask):
subnet.append(int(i)&int(j))
return subnet
def calcBroadcast(ip, mask):
bcast = []
for i,j in zip(ip, mask):
bcast.append(int(i)|(255-int(j)))
return bcast
def formatIp(confDict, asWhat, ipArr):
confDict[asWhat] = f"{ipArr[0]}.{ipArr[1]}.{ipArr[2]}.{ipArr[3]}"
def calcRange(confDict):
formatIp(confDict, "subnet", calcSubnet(confDict["ip"].split("."),\
confDict["mask"].split(".")))
formatIp(confDict, "broadcast", calcBroadcast(confDict["ip"].split("."),\
confDict["mask"].split(".")))
confDict["ip"] = confDict["subnet"][:-1]\
+ str(int(confDict["subnet"][-1]) + 1)
confDict["rangeStart"] = confDict["ip"][:-1]\
+ str(int(confDict["ip"][-1]) + 1)
confDict["rangeEnd"] = confDict["broadcast"][:-1]\
+ str(int(confDict["broadcast"][-1]) - 1)
def dhcpWrite(confDict):
calcRange(confDict)
with open("/etc/dhcp/dhcpd.conf", "w") as f:
f.write(f"""ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-faility local7;
subnet {confDict["subnet"]} netmask {confDict["mask"]} {{
range {confDict["rangeStart"]} {confDict["rangeEnd"]};
option broadcast-address {confDict["broadcast"]}
option routers {confDict["ip"]};
default-lease-time 21600;
max-lease-time 43200;
option domain-name "local";
option domain-name-servers {confDict["dns1"]}, {confDict["dns2"]};
}}""")
def dhcpLoad(confDict):
try:
with open("/etc/dhcp/dhcpd.conf", "r")as file:
contents = file.readlines()
confDict["dns1"], confDict["dns2"] = contents[12]\
.replace("option domain-name-servers ","").replace(";","")\
.strip().split(",")
confDict["subnet"], confDict["mask"] = contents[5]\
.replace("subnet ","").replace("netmask ","")\
.replace("{","").strip().split(" ")
confDict["ip"] = confDict["subnet"][:-1] + str(int(confDict["subnet"][-1]) + 1)
confDict["rangeStart"], confDict["rangeEnd"] = contents[6]\
.replace("range ","").replace(";","").strip().split(" ")
return True
except FileNotFoundError:
print(f"{Color.RED}No configuration found!{Color.RESET}")
except Exception as e:
print(f"{Color.RED}Unexpected error in dhcpLoad.\n{e}{Color.RESET}")
return False
def hostapdWrite(confDict):
with open("/etc/hostapd/hostapd.conf", "w") as f:
f.write(f"""interface={confDict["iface"]}
ssid={confDict["ssid"]}
country_code=US
hw_mode=g
channel={confDict["channel"]}
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase={confDict["passphrase"]}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_group_rekey=86400
ieee80211n=1
wme_enabled=1""")
def hostapdLoad(confDict):
try:
with open("/etc/hostapd/hostapd.conf","r") as file:
contents = file.read()
contents = contents.splitlines()
confDict["iface"] = contents[0].replace("interface=","")
confDict["ssid"] = contents[1].replace("ssid=","")
confDict["passphrase"] = contents[9].replace("wpa_passphrase=","")
confDict["channel"] = contents[4].replace("channel=","")
return True
except FileNotFoundError:
print(f"{Color.RED}No configuration found!{Color.RESET}")
except Exception as e:
print(f"{Color.RED}Unexpected error in hostapdLoad.\n{e}{color.RESET}")
return False
def changeSsid(confDict):
ssid = str(input("Enter Ssid: "))
if len(ssid) < 2 or len(ssid) > 32:
print(f"{Color.RED}SSID has to be at least 3 characters LONG!{Color.RESET}")
return
confDict["ssid"] = ssid
confDict["change"] = True
def isIp(ipStr):
ipStr = ipStr.split(".")
if len(ipStr) != 4:
return False
try:
for i in ipStr:
if int(i)<0 or int(i)>255:
return False
except Exception as e:
print(f"{Color.RED}Input is not a valid IP!{Color.RESET}")
return False
return True
def isMask(ipStr):
if not isIp(ipStr):
return False
maskValues = [0,128,192,224,240,248,252,254,255]
shouldBeZero = False
for i in ipStr.split("."):
if shouldBeZero:
if int(i) != 0:
return False
if int(i) not in maskValues:
return False
if int(i) != 255:
shouldBeZero = True
if int(ipStr.split(".")[3]) in [254,255]:
print(f"{Color.YELLOW}This subnet is too small!{Color.RESET}")
return False
return True
def changeDns(confDict):
dns = str(input("Enter dns1 ip: "))
if isIp(dns):
confDict["dns1"] = dns
confDict["change"] = True
else:
print("{Color.YELLOW}Entered ip is not valid!{Color.RESET}")
dns = str(input(f"Enter dns2 ip, leave blank for {confDict['dns2']}: "))
if dns != "":
if isIp(dns):
confDict["dns2"] = dns
else:
print("{Color.YELLOW}Entered ip is not valid!{Color.RESET}")
def changeSubnet(confDict):
ip = str(input("Enter subnet ip: "))
if not isIp(ip):
print("{Color.YELLOW}Entered ip is not valid!{Color.RESET}")
return;
mask = str(input("Enter mask: "))
if not isMask(ip):
print("{Color.YELLOW}Entered mask is not valid!{Color.RESET}")
return;
confDict["ip"] = ip
confDict["mask"] = mask
confDict["change"] = True
def changeChannel(confDict):
try:
channels = sub.check_output("sudo iwlist wlan0 scan | grep Channel:",shell=True)\
.split("\n").replace("Channel:","").strip()
except Exception as e:
print(f"{Color.RED}{e}{Color.RESET}")
return
channelDict={"1":0, "2":0, "3":0, "4":0, "5":0, "6":0, "7":0, "8":0, "9":0,
"10":0, "11":0, "12":0, "13":0}
for i in channels:
channelDict[i] += 1
print(f"Found APs on channels: {channelDict}")
channel = str(input("Enter channel: "))
try:
confDict["channel"] = channel if int(channel) > 0 and int(channel) < 14 else\
print(f"{Color.YELLOW}Entered channel is not valid!{Color.RESET}")
confDict["change"] = True
except:
print(f"{Color.YELLOW}Entered channel is not valid!{Color.RESET}")
def changePass(confDict):
passwd = str(input("Enter passhprase: "))
if len(passwd) < 8:
print(f"{Color.YELLOW}Entered passphrase is too short. Min. 8 chars!{Color.RESET}")
return
confDict["passphrase"] = passwd
confDict["change"] = True
if __name__=="__main__":
cmdsDict = {
"cls": lambda: sub.call("clear", shell=True),
"help": lambda: print(*cmdsDict),
"exit": lambda: True,
"reboot": lambda: sub.call("reboot",shell=True),
"save": lambda: save(runningConf),
"change-ssid": lambda: changeSsid(runningConf),
"change-dns": lambda: changeDns(runningConf),
"change-subnet": lambda: changeSubnet(runningConf),
"change-channel": lambda: changeChannel(runningConf),
"change-pass": lambda: changePass(runningConf),
"showcfg": lambda: print(runningConf),
"showdhcpls": lambda: sub.call("cat /var/lib/dhcp/dhcpd.leases | more", shell=True)
}
printLogo();
runningConf = {"change": False}
if not hostapdLoad(runningConf):
sys.exit(f"{Color.RED}Failed to load AP config!{Color.RESET}")
if not dhcpLoad(runningConf):
sys.exit(f"{Color.RED}Failed to load DHCP config!{Color.RESET}")
quitFlag = False
while not quitFlag:
cmd = getCmd(runningConf)
if cmd != "":
quitFlag = runCmd(cmd)