Created
August 7, 2019 08:13
-
-
Save newbenhd/547de318a29265c7536fdcb096ef31aa to your computer and use it in GitHub Desktop.
network_scanner2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from optparse import OptionParser | |
from scapy.all import srp, ARP, Ether | |
def get_args(): | |
parser = OptionParser() | |
parser.add_option('-t', '--target', dest='ip', help='IP address range') | |
options = parser.parse_args()[0] | |
if not options.ip: | |
parser.error('IP address range missing.') | |
return options | |
# | |
# When a device sends a packet to the broadcast MAC address (ff:ff:ff:ff:ff:ff) | |
# it is delivered to all stations on the local network. It needs to be used in | |
# order for all devices to receive your packet at the data link layer. | |
# | |
def scan(ip): | |
arp_request = ARP(pdst=ip) | |
broadcast_request = Ether(dst='ff:ff:ff:ff:ff:ff') | |
# arp_request.show() | |
# broadcast_request.show() | |
combined_packet = broadcast_request/arp_request | |
answered, unanswered = srp(combined_packet, timeout=1, verbose=False) | |
client_devices = [] | |
for answer in answered: | |
client_network_dict = {'ip': answer[1].psrc, 'mac': answer[1].hwsrc} | |
client_devices.append(client_network_dict) | |
return client_devices | |
def print_client_devices(client_devices): | |
print('IP\t\t\t\t\tMAC Address\n-----------------------------------------------') | |
for device in client_devices: | |
print(device['ip']+'\t\t'+device['mac']) | |
options = get_args() | |
client_devices = scan(options.ip) | |
print_client_devices(client_devices) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment