-
Notifications
You must be signed in to change notification settings - Fork 5
/
arp_ping.py
55 lines (46 loc) · 1.91 KB
/
arp_ping.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
#!/usr/bin/env python3
# suppress "WARNING: No route found for IPv6 destination :: (no default route?)"
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import sys
import argparse
from scapy.all import *
bcast_mac = 'ff:ff:ff:ff:ff:ff'
def get_mac(ip, interface, broadcast_mac=bcast_mac):
result = ''
arp_request = Ether(src=get_if_hwaddr(interface),dst=broadcast_mac)/ARP(pdst=ip)
ans, unans = srp(arp_request, timeout=2, iface=interface, verbose=False)
if ans:
first_response = ans[0]
req, res = first_response
result = res.getlayer(Ether).src
return result
def get_mac_alt(ip_addr, interface):
# arp_packet creation
interface_mac = get_if_hwaddr(interface)
eth_hdr = Ether(src=interface_mac, dst=bcast_mac, type=0x0806) # Ethernet header (Broadcast ethernet)
arp_hdr = ARP(op=ARP.who_has, pdst=ip_addr) # ARP header with 'who has this IP with MAC?'
padstr = '\x00' * (60 - len(eth_hdr) - len(arp_hdr))
pad = Padding(load=padstr) # a bunch of 0-bytes
arp_packet = eth_hdr/arp_hdr/pad
# Send ARP packets 5 times until it get correct response
for i in range(5):
response = srp1(arp_packet, iface=interface, verbose=0, timeout=5)
if response == None:
print("ARP faild. retransmitting", file=sys.stderr)
continue
else:
break
if response.op == 2:
return response.hwsrc
else:
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('ip', type=str,
help='IP of the host you want to arp ping')
parser.add_argument('-i', '--interface', type=str, dest='interface', default='wlan0',
help='Network interface to use')
args = parser.parse_args()
fetched_mac = get_mac(args.ip, args.interface)
if fetched_mac: print("{} = {}".format(args.ip, fetched_mac ))