helpers.networkhelper   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A localip() 0 7 1
A networkmap() 0 15 1
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