-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
48 lines (42 loc) · 1.85 KB
/
scan.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
import manuf
from scapy.all import ARP, Ether, srp
from typing import Dict, Tuple, List
class Scan:
@staticmethod
def scan() -> Tuple[List[str], Dict[str, str]]:
target_ip: str = "192.168.1.0/24"
devices: List[Dict[str, str]] = []
arp = ARP(pdst=target_ip)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether / arp
result = srp(packet, timeout=3, verbose=0)[0]
for sent, received in result:
devices.append({'ip': received.psrc, 'mac': received.hwsrc})
print("\n\tIP Address\t\t\tMAC Address\t\tInfo")
print("----------------------------------------------------------------------------")
vendor = manuf.MacParser()
ret_mac: List[str] = []
ret_arp: Dict[str, str] = dict()
for k, device in enumerate(devices):
ret_mac.append(device['mac'])
ret_arp[device['mac']] = device['ip']
print(f"{k} -> {device['ip']} \t\t {device['mac']} \t\t {vendor.get_manuf(device['mac'])}")
return ret_mac, ret_arp
@staticmethod
def arp_cache() -> Tuple[List[str], Dict[str, str]]:
import subprocess
result: str = subprocess.check_output("arp -a", shell=True, text=True)
arp_cache: List[str] = result.strip().split('\n')
print("\n\tIP Address\t\t\tMAC Address\t\tInfo")
print("----------------------------------------------------------------------------")
vendor = manuf.MacParser()
ret_mac: List[str] = []
ret_arp: Dict[str, str] = dict()
for k, entry in enumerate(arp_cache):
spl: str = entry.split(' ')
ret_mac.append(spl[3])
ret_arp[spl[3]] = spl[1][1:-1]
print(f"{k} -> {spl[1][1:-1]} \t\t {spl[3]} \t\t {vendor.get_manuf(spl[3])}")
return ret_mac, ret_arp
if __name__ == "__main__":
Scan.arp_cache()