-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
108 lines (93 loc) · 3.17 KB
/
main.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
import utils
import arping
import logging
from fileStorage import fileStorage
import sys
def parseArgs(args):
import getopt
parsedArgs = getopt.getopt(args,'i:')
argsDict = {}
for i in parsedArgs[0]:
if(len(i) > 0):
argsDict[i[0]] = i[1]
return argsDict
def handleDifferent(fs, mac, ip):
print('Note: the "{}" machine is now having a new IP address:'.format(mac))
print('\tOld IP address : {}'.format(fs.getIP(mac)))
print('\tNew IP address : {}'.format(ip))
resp = input('Update IP ? (yes/no)').lower()
if resp == 'yes':
fs.add(mac, ip)
def getManuf(mac):
try:
from manuf import manuf
parser = manuf.MacParser(manuf_name='manuf/manuf')
print('\n')
m = parser.get_all(mac)
if m.manuf:
manufacture = '{} {}'.format(
m.manuf,
'('+m.comment+')' if m.comment else ''
)
else:
manufacture = '-'
return manufacture
except:
return '-'
def handleMissed(fs, mac, ip):
manufacture = getManuf(mac)
print('ATTENTION: a new machine had been detected with the following details')
print('\tMAC Address : {}'.format(mac))
print('\tIP Address : {}'.format(ip))
print('\tManufacture : {}'.format(manufacture))
resp = input('Do you confirm that you are arware of this device' +
', by confirming that it\'ll be added to the whitelist'+
' and you\'ll not be notified about it anymore? (yes/no):')
resp = resp.lower()
if resp == 'yes':
fs.add(mac, ip)
return resp == 'yes'
def main():
logging.basicConfig(level=logging.CRITICAL)
argsDic = parseArgs(sys.argv[1:])
if '-i' in argsDic and argsDic['-i']:
iface = argsDic['-i']
else:
iface = utils.getDefInterface()
fs = fileStorage(iface)
loadingRes = fs.load()
if(iface != None):
netInfo = utils.getInterfaceConnectionInfo(iface)
toArping = netInfo['inet'] + '/' + str(netInfo['nm'])
print('Pinging....')
addresses = arping.arping(toArping)
_match = _different = _missed = _added = _ignored = 0
if loadingRes:
for mac,ip in addresses:
checkRes = fs.check(mac, ip)
if checkRes == 'DIFFERENT':
handleDifferent(fs, mac, ip)
_different += 1
if checkRes == 'MISSED':
added = handleMissed(fs, mac, ip)
_missed += 1
if added:
_added += 1
else:
_ignored += 1
if checkRes == 'MATCH':
_match += 1
else:
print('first time')
for mac,ip in addresses:
fs.add(mac,ip)
print('{} machines detected.'.format(len(addresses)))
if loadingRes:
print('\t{} Matched.'.format(_match))
print('\t{} Missed ({} added, {} ignored).'.format(_missed, _added, _ignored))
print('\t{} Different IP.'.format( _different))
fs.dump()
else:
logging.error('No up interfase')
if __name__ == '__main__':
main()