Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas-Mimi committed Nov 25, 2024
1 parent d7339b7 commit 0995cdd
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@
3.
'''

"""
Note:
For the first to do list. In python 3, scapy has changed the way it handles packets. It handles them as objects.
So, using str(pkt) will not work. You used srp() which is already correct. So i don't understand what you mean by
"Fix the calling pkt(str) python 3 makes no sense Suggestion -> Read the scapy documentation" ?
====================================================================================================================
Change i added:
- I renamed the variable "results" to "results" in the ScanSpecificSubnet class. As it's not a constant, it should be in lower case.
- I renamed the variable "clients" to "clients" in the ScanSpecificSubnet class. As it's not a constant, it should be in lower case.
- I added a try except block in the ScanSpecificAdd class to improve error handling.
- I added a try except block in the ScanSpecificSubnet class to improve error handling.(Maybe add a specific exception ??)
- I added a check to see if the ip address is in the correct format or not.
- I added a help message for the ip argument.
- I added a print of the IP and MAC addresses of the devices found on the network in the ScanSpecificAdd class.
- I'm working on README.md file to explain how to use the script. Is it okay for you??
=====================================================================================================================
"""




Expand All @@ -27,25 +48,37 @@
# ===== Scan Specific IP Address =====
class ScanSpecificAdd:
def __init__(self, ip):
arp = ARP(pdst=ip) # Creating an ARP Packet
ether = Ether(dst="ff:ff:ff:ff:ff:ff") # Creating a broadcast Packet
packet = ether/arp # Stacking the arp and ether Packets.
result = srp(packet, timeout=3)[1]
# Add try except to improve the error handling
try:
arp = ARP(pdst=ip) # Creating an ARP Packet
ether = Ether(dst="ff:ff:ff:ff:ff:ff") # Creating a broadcast Packet
packet = ether/arp # Stacking the arp and ether Packets.
result = srp(packet, timeout=3)[1]
except Exception as e:
print(f"Error scanning subnet {ip}: Network timeout or no response")
return
for sent, received in result:
print(f"IP: {received.psrc} MAC: {received.hwsrc}")


# ===== Scan for IP Address range =====
class ScanSpecificSubnet:
def __init__(self, ip):
arp = ARP(pdst=ip) # Creating an ARP Packet
ether = Ether(dst="ff:ff:ff:ff:ff:ff") # Creating a broadcast Packet
packet = ether/arp # Stacking the arp and ether Packets.

RESULTS = srp(packet, timeout=3)[0]
CLIENTS = []
for sent, received in RESULTS:
CLIENTS.append({'ip': received.psrc, 'mac': received.hwsrc})
try :
arp = ARP(pdst=ip) # Creating an ARP Packet
ether = Ether(dst="ff:ff:ff:ff:ff:ff") # Creating a broadcast Packet
packet = ether/arp # Stacking the arp and ether Packets.

results = srp(packet, timeout=3)[0] # change from upper case to lower case as it's not a constant! (python naming convention)
except Exception as e:
print(f"Error scanning subnet {ip}: Network timeout or no response")
return
clients = [] # change from upper case to lower case as it's not a constant!(python naming convention)
for sent, received in results:
clients.append({'ip': received.psrc, 'mac': received.hwsrc})

# Print the hosts discovered in the network
print(CLIENTS)
print(clients)


if __name__ == "__main__":
Expand All @@ -57,7 +90,7 @@ def __init__(self, ip):
if int(address) in range(0,256) and len(ListedAddress) == 4 or len(ListedAddress) == 5:
continue
else:
print("Follow the IP Address format")
print("Follow the IP Address format ; 192.168.1.1 for single IP\n or 192.168.1.0/24 for subnet")
sleep(3)
exit()

Expand Down

0 comments on commit 0995cdd

Please sign in to comment.