Building Tools is an important aspect of learning for myself. Expanding on this , I built out some tools to do some quick subnetting and to increase the speed of the pingsweeper tool.
Subnetter tool will take in a prefix inside of quotes with the included mask. Then taking the difference of subnetting you want to output. In the example below 10.0.0.0/24 is subnetted into /26 networks.
#Louis DeVictoria
#Script takes in a prefix and the amount of subnets you want to carve out
#Libraries
import ipaddress
def Subnetter(prefix,change):
subnets = list(ipaddress.ip_network(prefix).subnets(prefixlen_diff=change))
for i in subnets:
print(i)
##Example Output
Subnetter("10.0.0.0/24",2)
10.0.0.0/26
10.0.0.64/26
10.0.0.128/26
10.0.0.192/26
Pingsweeper but Faster!
Something made quickly clear is the need to do things faster. The subprocess module helps run multiple new processes in parallel. This allows operations to be completed faster.
#Louis DeVictoria
#Slow Pingsweeper Script
def pingsweepold(prefix):
start = timer()
prefix = (list(ipaddress.ip_network(prefix).hosts()))
#The format function pulls the ip to be used in the function
format(prefix)
for i in prefix:
ip = (format(i))
result = (os.system("ping -c 1 -n -W 2 " + ip ))
if result:
print (ip, 'inactive')
else:
print (ip, 'active')
stop = timer()
time = (stop - start)
print(f"{time} seconds" )
pingsweepold("10.245.0.0/26")
.....truncated output......
62.710192931001075 seconds
pingsweepold("public/26")
5.113516895999055 seconds
pingsweepold("public/24")
156.3443550189986 seconds
#Louis DeVictoria
def pingsweep(prefix):
#Start a timer
start = timer()
#This line uses a "special file" which redirects to discard
with open(os.devnull, 'wb') as limbo:
#This line iterates over a prefix and returns all the hosts
prefix = (list(ipaddress.ip_network(prefix).hosts()))
#Format returns the host ip address without the method attached
format(prefix)
for i in prefix:
ip = (format(i))
#Ping , Count 1 , Numeric Output Only , Wait 2 Seconds
result=subprocess.Popen(['ping', '-c', '1', '-n', '-W', '2', ip], stdout=limbo, stderr=limbo).wait()
if result:
print (ip, 'inactive')
else:
print (ip, 'active')
stop = timer()
time = (stop - start)
print(f"{time} seconds" )
pingsweep("10.245.0.0/26")
.....truncated output......
62.58549390799817 seconds
pingsweep("public/26")
5.2381483190001745 seconds
pingsweep("public/24")
155.7946709909993 seconds
The performance gains were made from the using the switches in the ping command limit the wait time in the pingsweepold but there is still a performance gain with the script with the subprocess method. The times are not a massive difference for this tool but it was fun to measure the time and performance of this tool set.
pingsweepold(“public/24”)
156.3443550189986 seconds
pingsweep(“public/24”)
155.7946709909993 seconds
We gained a full second with the difference . I hope you gained something from reading this and I am thankful to share my journey with you.
Resources:
https://docs.python.org/3/library/subprocess.html
https://docs.python.org/3/library/ipaddress.html
