Total Complexity | 2 |
Total Lines | 28 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | '''network related functions, mostly for discovery''' |
||
2 | import socket |
||
3 | import nmap |
||
4 | |||
5 | def localip(dns): |
||
6 | '''todo: why needs dns to find out local ip?''' |
||
7 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
||
8 | sock.connect((dns, 80)) |
||
9 | locip = sock.getsockname()[0] |
||
10 | sock.close() |
||
11 | return locip |
||
12 | |||
13 | def networkmap(dns, api_port, ssh_port): |
||
14 | '''see if scanning for ssh port is really necessary''' |
||
15 | locip = localip(dns) |
||
16 | print('Your ip is {0}'.format(locip)) |
||
17 | #scan can take a minute |
||
18 | #-n/-R: Never do DNS resolution/Always resolve [default: sometimes] |
||
19 | #-sL: List Scan - simply list targets to scan |
||
20 | #-sP <hosts>: only does discovery |
||
21 | #-PS: Port scan during discovery |
||
22 | scanarguments = '-v -sV' #faster but lists hosts that are not miners |
||
23 | print("mapping network with arguments '{0}'".format(scanarguments)) |
||
24 | netmapper = nmap.PortScanner() |
||
25 | netmapper.scan(hosts=locip+'/24', ports='{0},{1}'.format(api_port, ssh_port), arguments=scanarguments, sudo=False) |
||
26 | hosts_list = [(h, netmapper[h]['status']['state'], netmapper[h]['vendor']) for h in netmapper.all_hosts()] |
||
27 | return hosts_list |
||
28 |