Skip to content

Instantly share code, notes, and snippets.

@AlexLynd
Last active September 24, 2021 13:15
Show Gist options
  • Save AlexLynd/6b42f41fed69cb27943e5fd9db862f38 to your computer and use it in GitHub Desktop.
Save AlexLynd/6b42f41fed69cb27943e5fd9db862f38 to your computer and use it in GitHub Desktop.
Python network scanning utility [GCI 2019-20]
import subprocess, ipaddress, socket, concurrent.futures , re
from subprocess import Popen, PIPE
def myIP(): # get my IP addy
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
def pingy_the_pinger(ip_addr): # ping them IP's
try:
subprocess.check_output(["ping", "-c", "1", ip_addr])
ip_list.append(ip_addr)
except:
pass
def get_MAC(ip_addr): # get MAC from IP address
pid = Popen(["arp", "-n", ip_addr], stdout=PIPE)
s = pid.communicate()[0]
mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", str(s)).groups()[0]
return mac
header = myIP().split('.') # strip IP to get base for search
network = ipaddress.ip_network(header[0] + '.' + header[1] + '.' + header[2] + '.0/24')
hosts = network.hosts()
ip_list = []
mac_list = []
# multithreaddddddddddddddddddddddd uwu
executor = concurrent.futures.ThreadPoolExecutor(254)
ping_hosts = [executor.submit(pingy_the_pinger, str(ip)) for ip in hosts]
print("There are "+str(len(ip_list))+" devices on the network.")
print("-----------------------------------------------------------------")
try:
for ip in ip_list:
MAC = get_MAC(str(ip))
print(ip+ "\t" + MAC)
except :
ip_list.remove(ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment