-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArpScanner.py
89 lines (74 loc) · 2.42 KB
/
ArpScanner.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scapy.all import * #srp,srp1,Ether,ARP,conf
import sys, getopt, os
import socket
import uuid
def get_mac_address(): #get mac
mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
return ":".join([mac[e:e+2] for e in range(0,11,2)])
def usage():
print "Usage: sudo python ArpScanner.py "
def cutIP(ip):
#print ip
iplist = ip.split('.')
#print iplist
del iplist[3]
#print iplist
ipscan = '.'.join(iplist)
#print ipscan
return ipscan
def scan(ip):
try:
#ipscan='192.168.1.1/24'
#print '\nCurrent Network Segment:'+ip
#ipscan = sys.argv[1] + ".0/24"
ipscan = cutIP(ip) + ".0/24"
arpPkt = Ether(dst="FF:FF:FF:FF:FF:FF")/ARP(pdst=ipscan)
#arpPkt = Ethernet_head(dst="FF:FF:FF:FF:FF:FF")/ARP_head(pdst=ipscan)
#print Ethernet_head(dst="FF:FF:FF:FF:FF:FF")
ans,unans = srp(arpPkt,timeout=2,verbose=False)
#print ans
except Exception,e:
print str(e)
else:
for snd,rcv in ans:
#print ARP.psrc
list_ip = rcv.sprintf('%ARP.psrc%')
#print list_ip
try:
(hostname, aliaslist, ipaddrlist) = socket.gethostbyaddr(list_ip)
except:
hostname = 'Unknow Device'
list_mac=rcv.sprintf("IP:%ARP.psrc% - MAC:%Ether.src%")
print list_mac + ' - HostName: '+hostname
'''
#return list_mac
for ipFix in range(1, 254):
ipscan = "192.168.1."+str(ipFix)
arpPkt = Ether(dst="FF:FF:FF:FF:FF:FF")/ARP(pdst=ipscan)
ans = srp1(arpPkt, timeout = 1, verbose = False)
if ans:
print "IP:"+ans.psrc + " MAC:"+ans.hwsrc
'''
def main(argv):
if os.geteuid() != 0:
print "This program must be run as root. Aborting."
sys.exit()
print '\n----Local Network Information----'
localIP = socket.gethostbyname(socket.gethostname())#得到本地ip
print "\nlocal IP: "+localIP
ipList = socket.gethostbyname_ex(socket.gethostname())
print 'local PC: '+ipList[0]
mac = get_mac_address()
print 'local MAC: '+mac +'\n'
print '----All of active PC in LAN----\n'
try:
opts, args = getopt.getopt(argv, "")
except getopt.GetoptError:
usage()
sys.exit(2)
#start scan LAN
scan(localIP)
if __name__ == "__main__":
main(sys.argv[1:])