-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_finder
executable file
·77 lines (67 loc) · 2.36 KB
/
device_finder
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
#!/usr/bin/env python3
import os
import sys
import socket
from scapy.all import *
import threading
import json
import psutil
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
# Get the base IP by taking the first three octets of the IP address
ip_address = get_local_ip()
base_ip = ".".join(ip_address.split(".")[:-1])
ip_range = base_ip + ".0/24"
print(f"Scanning network range: {ip_range}")
# Filter for only active interfaces
interfaces = [iface for iface, stats in psutil.net_if_stats().items() if stats.isup]
print(f"Active interfaces: {interfaces}")
broadcastMac = "ff:ff:ff:ff:ff:ff"
results = []
# Function to perform ARP scan on an interface
def scan_interface(interface):
print(f"Scanning on interface: {interface}")
try:
packet = scapy.layers.l2.Ether(dst=broadcastMac)/scapy.layers.l2.ARP(pdst=ip_range)
ans, unans = scapy.sendrecv.srp(packet, timeout=2, iface=interface, inter=0.1, verbose=False)
for send, receive in ans:
mac_addr = receive.sprintf(r'%Ether.src%')
ip_addr = receive.sprintf(r'%ARP.psrc%')
try:
hostname = socket.gethostbyaddr(ip_addr)[0]
except socket.herror:
hostname = "Unknown"
print(f"Interface: {interface} - {mac_addr} - {ip_addr} - Hostname: {hostname}")
results.append({
"interface": interface,
"mac_address": mac_addr,
"ip_address": ip_addr,
"hostname": hostname
})
except Exception as e:
print(f"Error on interface {interface}: {str(e)}")
try:
# Using threading to scan interfaces simultaneously
threads = []
for interface in interfaces:
thread = threading.Thread(target=scan_interface, args=(interface,))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
# Save results to a JSON file
with open("scan_results.json", "w") as f:
json.dump(results, f, indent=4)
print("Scan results saved to scan_results.json")
except KeyboardInterrupt:
print("\nScript interrupted by user. Exiting...")
sys.exit(0)