
def lanmap():
    '''
    @description: uses Scapy to enumerate live hosts and gather IP addresses
    @author: ohdae [bindshell@live.com]
    @short: find live hosts on LAN
    '''
    log_msg("\n\n [ LANMap Module ]")
    log_msg("\n Start Time: %s" % logtime)

    print("[+] Searching for live hosts...")
    maketemp("hosts")
    os.chdir(Temp_Dir+"/hosts")

    try:
        localIP = [x[4] for x in scapy.all.conf.route.routes if x[2] != '0.0.0.0'][0]
    except OSError:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("google.com",80))
        localIP = (s.getsockname()[0])
        s.close()
    else:
        pass
    ipBin = reduce(lambda x, y: (int(x) << 8)+int(y), localIP.split('.'))
    #route = [ network_addr, netmask, gateway, interface, address ]
    for route in scapy.all.conf.route.routes:
        if (route[4] == localIP #If it's the address we're talking to
            and route[0] != 0 #and it's not the route to the gateway itself
            and route[0] == (route[1] & ipBin)): #And localIP is in this subnet (fixes 169.254/16 oddness)
                #Calculate the CIDR from the base-2 logarithm of the netmask
                IPRange = '/'.join((localIP, str(int(32-log(0xffffffff-route[1]+1,2)))))
    
    conf.verb=0
    ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=IPRange),timeout=2)
    write2file("livehosts.txt", "LAN IP Range: " + IPRange +"\n")
    for snd,rcv in ans:
        mac_address=rcv.sprintf("%Ether.src%")
        ip_address=rcv.sprintf("%ARP.psrc%")
        write2file("livehosts.txt", "\n[+] Live Host\nIP: "+ip_address + " MAC"+ mac_address + "\n")

    externalIP = urllib2.urlopen("http://myip.ozymo.com/").read()
    results = ("External IP Address: " + externalIP + "\nInternal IP Address: " + localIP + "\nInternal IP Range: " + IPRange +"\n")
    writenew("external.txt", results)
    
 
