-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathespfinder.py
executable file
·263 lines (233 loc) · 8.22 KB
/
espfinder.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
#!/usr/bin/env python3
#
# Pings for active host on *this* sub network.
# The current sub network address and
# the host numbers 1-254 will be pinged
#
import subprocess
from multiprocessing import Process, Array, Value
from sys import exit, argv
import ctypes
import time
import signal
import os
from ef_net import *
import tkinter# sudo apt-get install python3-tk
from tkinter import ttk
from tkinter import messagebox
PROG_NAME= "ESP Finder"
PROG_VER="0.5"
# ---------- MULTIPROCESSING PART
class PING_SWEEP(object):
def __init__(self,callback,ownip):
self.callback = callback
self.ownip = ownip
self.ping_sweeper.__init__(self)
self.ping_sweeper()
def pinger(self, host_num, resarray, resnum):
"""thread pinger function"""
hostadrr = self.ownip.split('.')[:-1]
hostadrr = '.'.join(hostadrr) + '.' + repr(host_num)
#print('.', end='')
if os.name == "posix":
try:
line = subprocess.getoutput("ping -n -c 1 -W 0.2 %s 2> /dev/null" % hostadrr)
except:
resarray[host_num] = 0
line = ""
if line.find(hostadrr) and line.find("bytes from") > -1: # Host Active
resarray[host_num] = host_num
else:
resarray[host_num] = 0
elif os.name == "nt":
#print(hostadrr, end='') # debug only
proc = subprocess.Popen(['ping', '-n', '1', '-w', '200', hostadrr],stdout=subprocess.DEVNULL)
proc.wait()
if proc.returncode == 0: # success
#print(' ----> is UP') # debug only
resarray[host_num] = host_num
else:
#print(' no response') # debug only
resarray[host_num] = 0
resnum.value +=1
self.hcount = resnum.value
exit(0)
def ping_sweeper(self):
global shared_array, scounter
#shared_array = Array(ctypes.c_ubyte,255) # multiprocessing ping results
#scounter = Value('i',0)
# self.maxnum = 255 # 255
self.hcount = scounter.value
procarr = []
print('000 IPs scanned', end='')
if os.name == "posix":
for host_num in range(1, 255):
print('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b'+('00'+str(host_num))[-3:]+' IPs scanned', end='')
ping = Process(target=self.pinger, args=(host_num,shared_array,scounter))
ping.start()
procarr.append(ping.pid)
time.sleep(3)
for process in procarr: # Cleanup
try:
os.kill(process,signal.SIGTERM)
except:
pass
elif os.name == "nt":
for host_num in range(1, 255):
print('.', end='')
ping = Process(target=self.pinger, args=(host_num,shared_array,scounter))
ping.start()
procarr.append(ping)
for process in procarr: # Cleanup
process.join(1)
print(' ... done')
self.callback(1,255)
def __del__(self):
self.callback(2,self.hcount)
def cb_stationsearch(func, par1):
global UseGUI
if UseGUI==False:
print('.', end='')
# print("Counter",par1)
if par1>=255:
if UseGUI==False:
print("Analyzing online targets...")
analyzerange()
def pingscan():
global ownip
PING_SWEEP(cb_stationsearch,ownip)
def analyzerange():
global shared_array, ownip, UseGUI, tree
hostaddr = ownip.split('.')[:-1]
if UseGUI:
for i in tree.get_children(): # clear tree
tree.delete(i)
for i in range(1,255):
if shared_array[i] > 0 and shared_array[i] < 255:
#hostadr = '.'.join(hostaddr) + '.' + repr(shared_array[i])
hostadr = '.'.join(hostaddr) + '.' + str(i)
if (hostadr == ownip):
tline = "THIS DEVICE"
if UseGUI==False:
print(hostadr.ljust(14)+tline)
else:
tree.insert("","end",text=hostadr, values=(tline))
else:
analyzeip(hostadr)
def analyzeip(par1):
global UseGUI, tree
# 0:mac,1:netname,2:mactype, 3: ptype
attribs = ["","","",""]
infoarr = getMACfromIP(par1)
if (infoarr[0] != ""):
attribs[0] = str(infoarr[0])
attribs[1] = str(infoarr[1])
attribs[2] = checkMACManuf(infoarr[0])
if (check_port(par1,80)):
attribs[3] = check80(par1)
if (str(attribs[2]) != str("Espressif")) and (str(attribs[3]) == str("ESPurna")):
attribs[3] = "" # its only a wild guess, delete if surely not!
if (str(attribs[2]) == str("Espressif")) and ( (attribs[3] == "Unknown") or (attribs[3] == "") ):
if check_tuya(par1):
attribs[3] = "Tuya"
tline = attribs[0] + ", "+ attribs[1] + ", "+ attribs[2] +" "+ attribs[3]
if UseGUI==False:
print(par1.ljust(14) + tline)
else:
tpid= tree.insert("","end",text=par1.ljust(14),values=(tline))
if attribs[3] not in ["Tasmota","RPIEasy","ESPEasy","Shelly","Tuya"]: # rpieasy can run on different ports, check that
tport = 8080
if (check_port(par1,tport)):
attribs[3] = check80(str(par1)+":"+str(tport))
if attribs[3]!="RPIEasy":
tport = 8008
if (check_port(par1,tport)):
attribs[3] = check80(str(par1)+":"+str(tport))
if attribs[3]=="RPIEasy":
tinfos = get_espeasy(str(par1)+":"+str(tport))
tline = tinfos[2] + ", " + tinfos[3] + ", "+ tinfos[4] +", "+ tinfos[5] +", "+ tinfos[6]+", "+ tinfos[7]
if UseGUI==False:
print(" -"+tline)
else:
tree.insert(tpid,"end",values=(tinfos[2],tinfos[3],tinfos[4],tinfos[5],tinfos[6],tinfos[7]))
elif attribs[3] == "Tasmota":
tinfos = get_tasmota(par1)
tline = tinfos[2] + ", " + tinfos[3] + ", "+ tinfos[4] +", "+ tinfos[5] +", "+ tinfos[6]+", "+ tinfos[7]+", "+ tinfos[8]
if UseGUI==False:
print(" -"+tline)
else:
tree.insert(tpid,"end",values=(tinfos[2],tinfos[3],tinfos[4],tinfos[5],tinfos[6],tinfos[7],tinfos[8]))
elif attribs[3] == "ESPEasy" or attribs[3] == "RPIEasy":
tinfos = get_espeasy(par1)
tline = tinfos[2] + ", " + tinfos[3] + ", "+ tinfos[4] +", "+ tinfos[5] +", "+ tinfos[6]+", "+ tinfos[7]
if UseGUI==False:
print(" -"+tline)
else:
tree.insert(tpid,"end",values=(tinfos[2],tinfos[3],tinfos[4],tinfos[5],tinfos[6],tinfos[7]))
elif attribs[3] == "Shelly":
tinfos = get_shelly(par1)
tline = tinfos[2] + ", " + tinfos[3] + ", "+ tinfos[4] +", "+ tinfos[5] +", "+ tinfos[6]+", "+ tinfos[7]
if UseGUI==False:
print(" -"+tline)
else:
tree.insert(tpid,"end",values=(tinfos[2],tinfos[3],tinfos[4],tinfos[5],tinfos[6],tinfos[7]))
def searchdevices():
global UseGUI
if UseGUI==False:
print("Starting pingscan...")
pingscan()
UseGUI = True
if __name__ == '__main__':
shared_array = Array(ctypes.c_ubyte,255) # multiprocessing ping results
scounter = Value('i',0)
if len(argv)>1:
if argv[1] == '-t':
UseGUI = False
if UseGUI:
window = tkinter.Tk()
window.title(PROG_NAME)
window.geometry('1000x700')
ownip = get_ip()
if (ownip == False):
if UseGUI:
window.withdraw()
messagebox.showinfo("Error","I can not find own IP address, please send ifconfig output to forum!")
else:
print("I can not find own IP address, please send ifconfig output to forum!")
# exit(0)
if (len(argv)>3 and argv[2]=='-i'):
ownip = argv[3]
else:
print("To start in Terminal mode use '-t' for window mode '-g'.")
print("As syntax to start with manual defined IP use:")
print(" => 'python3 ", argv[0], " -t -i xxx.xxx.xxx.xxx'")
print("Please try again. Script support English and German.")
exit(0)
print("Your IP is:", ownip)
if UseGUI:
# messagebox.showinfo("Info","Please press 'Refresh' button and wait for results. It will take a while.")
tree = ttk.Treeview(window)
tree["columns"]=("A","B","C","D","E","F","G")
tree.column("A", width=250)
tree.column("B", width=150)
tree.column("C", width=100)
tree.column("D", width=120)
tree.column("E", width=105)
tree.column("F", width=38)
tree.column("G", width=50)
tree.heading("A", text="Version")
tree.heading("B", text="Unit num/name")
tree.heading("C", text="Uptime")
tree.heading("D", text="Prog/Flash size")
tree.heading("E", text="Free RAM")
tree.heading("F", text="Wifi")
tree.heading("G", text="Vcc")
tree.pack(expand=True, fill=tkinter.BOTH, side=tkinter.TOP)
tree.insert("","end",text="Please press 'Refresh' button",values=("and wait for results.","It will take a while."))
refreshbutton = ttk.Button(window, text='Refresh', command=searchdevices)
refreshbutton.pack()
refreshbutton.focus_set()
#draw the window, and start the 'application'
window.mainloop()
else:
searchdevices()