-
Notifications
You must be signed in to change notification settings - Fork 1
/
osdetectioncron.py
129 lines (102 loc) · 3.82 KB
/
osdetectioncron.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
"""
Author: Sujayyendhiren Ramarao
Description: Run this program from cron on a periodic basis and cache the data.
This cached data maybe read by the UI. This program needs sudo permissions
and thus isolating this program helps us not run the flask app with sudo
permissions
"""
import os
import json
import traceback
from glob import glob
from collections import defaultdict
from multiprocessing import Pool
import nmap
import pysftp
from highlander import one
from library.network import HomeNetwork
def oshandler(iphost):
""" Scan device(s) OS"""
osinfo = {}
ipaddr, hostname = iphost
pscan = nmap.PortScanner()
pscan.scan(hosts=ipaddr, arguments='-O')
pscan.all_hosts()
print('-'*70)
print("Hostname: {}".format(hostname))
print('-'*70)
try:
if 'osclass' in pscan[ipaddr]:
for osclass in pscan[ipaddr]['osclass']:
print('OsClass.type : {0}'.format(osclass['type']))
print('OsClass.vendor : {0}'.format(osclass['vendor']))
print('OsClass.osfamily : {0}'.format(osclass['osfamily']))
print('OsClass.osgen : {0}'.format(osclass['osgen']))
print('OsClass.accuracy : {0}'.format(osclass['accuracy']))
print('-'*70)
osinfo = osclass
break
if 'osmatch' in pscan[ipaddr]:
for osmatch in pscan[ipaddr]['osmatch']:
print('osmatch.name : {0}'.format(osmatch['name']))
print('osmatch.accuracy : {0}'.format(osmatch['accuracy']))
print('osmatch.line : {0}'.format(osmatch['line']))
print('-'*70)
osinfo = osmatch
break
if 'fingerprint' in pscan[ipaddr]:
print('Fingerprint : {0}'.format(pscan[ipaddr]['fingerprint']))
print('-'*70)
osinfo = pscan[ipaddr]['fingerprint']
except:
osinfo["exception"] = traceback.format_exc()
finally:
osinfo["ip"] = ipaddr
osinfo["hostname"] = hostname
if 'osclass' in osinfo:
osinfo.update(osinfo['osclass'][0])
del osinfo['osclass']
return osinfo
def osdetection():
""" Detect OS of the network devices """
ipaddr = []
for hostname in HomeNetwork.get_allhosts():
ipaddr.append((HomeNetwork.get_hostname_specificdata(hostname, 'ip'), hostname))
if not ipaddr:
return
pool = Pool(processes=len(ipaddr))
results = pool.map(oshandler, ipaddr)
pool.close()
pool.join()
newstruct = defaultdict(dict)
osfailure = []
for aresult in results:
if 'exception' not in aresult:
hostname = aresult['hostname']
newstruct[hostname]['cpe'] = aresult['cpe'][0].replace(":", "_")
newstruct[hostname]['ip'] = aresult['ip']
newstruct[hostname]['hostname'] = hostname
newstruct[hostname]['osvendor'] = aresult['vendor']
newstruct[hostname]['osname'] = aresult['name']
newstruct[hostname]['ostype'] = aresult['type']
newstruct[hostname]['osgen'] = aresult['osgen']
newstruct[hostname]['osfamily'] = aresult['osfamily']
newstruct[hostname]['osaccuracy'] = aresult['accuracy']
else:
print(json.dumps(aresult, indent=4))
osfailure.append(aresult)
HomeNetwork.add_update_rows(newstruct)
def complete_setup():
""" Cleanup previously network topology temp data """
for afile in glob("static/data/networkdata-*.json"):
os.remove(afile)
HomeNetwork.hasync()
@one()
def entrypoint():
""" Runs only a single instance of a process """
HomeNetwork.initializetable(perms=True)
HomeNetwork.create_tree()
osdetection()
complete_setup()
if __name__ == '__main__':
entrypoint()