-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathph.py
executable file
·205 lines (173 loc) · 5.24 KB
/
ph.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
import os
import subprocess
import os.path
import csv
import sys
import time
import config
from pushetta import Pushetta
try:
import configuser as config
except ImportError:
import config
ipr = config.ipr
nmap = "nmap -sn " + ipr + " | awk '/Address:/ {print $3}' "
def nmac():
try:
x = subprocess.check_output(nmap,shell=True).decode(sys.stdout.encoding).split("\n")
# print("NMAC X POP")
# print(x.pop())
except subprocess.CalledProcessError as e:
cfl( [str(e.output), "retrying inhttps://www.reddit.com/r/AyyMD/top/?sort=top&t=month 60 seconds"])
# print(e.output)
# print("retrying in 60 seconds")
time.sleep(60)
x = nmac()
# x.pop()
return (x)
def push(x):
# API_KEY="YOU API KEY"
# CHANNEL_NAME="YOUR CHANNEL NAME"
try:
ps = Pushetta(config.API_KEY)
ps.pushMessage(config.CHANNEL_NAME, x)
except:
cfl( x)
# sys.exit()
def addb(dic, key, val):
dic.setdefault(key, [])
dic[key].append(val)
# def remb(dic,key):
# del (dic[key])[0]
def ctb(kid, vb):
mac = nmac()
# print("NMAC")
# print(mac)
matchv = list(set(kid).intersection(mac))
matchf = list(set(kid).difference(matchv))
for key in matchv:
addb(vb, key, True)
for key in matchf:
addb(vb, key, False)
return (vb)
def opcsv(nmbr):
# ids csv file of MACADDRESS,DEVICENAME
ids = {}
for key, val in csv.reader(open(nmbr)):
ids[key] = val
return(list(ids.keys()), ids)
def crvbr(vbx, vbrx):
for key in vbx:
if True in vbx[key]:
vbrx[key] = True
else:
vbrx[key] = False
def earlnot(vbrx, vbx, arlnotx):
tme = tmee()
for key in vbrx:
if ((vbrx[key] is False) and
(True in vbx[key]) and (key not in arlnotx)):
push(ids[key] + config.HASEN + " " + tme)
cfl( [ids[key] + config.HASEN + " " + tme])
arlnotx.append(key)
return(arlnotx)
def tmee():
return(time.strftime("%H:%M:%S"))
def cfl(ln):
dia = time.strftime("%d %m %Y")
with open(dia, 'a') as f:
f.write("\n".join(ln))
print("\n".join(ln))
def lstvb(idsx, vbx):
lst = []
for key in vbx:
lst.append(str(idsx[key]))
lst.append(str(vbx[key]))
return(lst)
def createuserconf():
if not os.path.isfile("configuser.py"):
conu = open("configuser.py",'x')
cons = open("config.py",'r')
conss = cons.read()
conu.write(conss)
conu.close()
print("NO CONFIG USER FILE DETECTED CREATING ONE FROM TEMPLATE MODIFY ACCORDINLY config.py --> configuser.py")
extt = 1
if not os.path.isfile("idsuser.csv"):
idsu = open("idsuser.csv",'x')
idss = open("ids.csv",'r')
idsss = idss.read()
idsu.write(idsss)
idsu.close()
print("NO IDS USER FILE DETECTED CREATING ONE FROM TEMPLATE MODIFY ACCORDINLY ids.csv --> idsuser.csv")
extt = 1
# check if variable exists
try:
extt
sys.exit()
except NameError:
print("User files exist")
if __name__ == '__main__':
createuserconf()
(kid, ids) = opcsv("idsuser.csv")
# print("KID")
# print(kid)
vb = {}
vb1 = {}
count = 1
while True:
# print(count)
if not vb1:
vb = ctb(kid, vb)
# print("VB")
# print(vb)
else:
vb = dict(vb1)
vb1 = {}
count = 10
if count == 10:
vbr = {}
crvbr(vb, vbr)
# print("VBR")
# print(vbr)
arlnot = []
while count != 1:
vb1 = ctb(kid, vb1)
# print("TABLA DE VERDAD 1")
arlnot = earlnot(vbr, vb1, arlnot)
# print("vb1")
# print(vb1)
# print(time.strftime("%H:%M:%S"))
# for key in vb1:
# print(ids[key])
# print(vb1[key])
vbl1 = lstvb(ids, vb1)
cfl( ["vb1", tmee(), "#cicle " + str(count)] + vbl1)
count -= 1
time.sleep(60)
if count == 1:
vbr1 = {}
for key in vb1:
if True in vb1[key]:
vbr1[key] = True
else:
vbr1[key] = False
chan = [k for k in vbr if vbr[k] != vbr1[k]]
# print("chan")
# print(chan)
tme = tmee()
for key in chan:
# print(ids[key])
if not vbr1[key]:
push(ids[key] + config.HASLE + " " + tme)
else:
ntf = [ids[key] + config.HASEN + " NOT NOF " + tme]
cfl( ntf)
# print(ids[key] + config.HASEN + " NOT NOF"+tme)
# push(ids[key] + config.HASEN + " " + tme)
vbl = lstvb(ids, vb)
cfl( ["vb", tmee(), "#cicle " + str(count)] + vbl)
count += 1
# print("vb")
# print(time.strftime("%H:%M:%S"))
time.sleep(60)