What is Ping Scan?
Ping Scan is a process of sending ICMP ECHO request packet to any live host. If a Host wants to mention his availability, it will definitely reply with an ICMP ECHO response packet.
Now, you will think why I said, "want to mention” in previous lines.
This Is Because, To Prevent System From Hackings Attacks, Users Use Firewalls And Other Security Features That's Not Allow Host Machine to Response back at ICMP Packet Request But Don't Worry, Almost All Host System Likes To Respond On ICMP ECHO Requests.
To Send Ping ICMP Request, All Types Of Operating System’s Provides Built-in Facility That Can Help User To Ping Any Host And Verify That, The Host Is Live Or Not.
In Another word, Ping Scans Also Known As Ping Sweeping. Ping Sweeping is the procedure to Find More Than One Machine Availability in Specific Network Range.
Why Create Ping Sweeping Script
Let Assume If You Want To Scan Big List Of IP Addresses To Find Live Host Systems But this Procedure is very time consuming And You don’t have that much time. So, here what we can do? we can use our automatic ping sweeping python script that can handle all our works automatically.
In Simple Words, This Script also plays very important role in the various type of penetrating testing and hacking.
Find Live Systems
To Send A Simple ICMP Echo Request, you can use many types of utilities in different situations like
Windows (built-in)
Here, -n argument is selecting a number of time to send ICMP ECHO request.
$ ping -n 5 192.168.1.101/24
Linux (built-in)
Here, -n argument is selecting a number of time to send ICMP ECHO request.
$ ping -c 5 192.168.1.101/24
ICMP IP Network Scanning with Nmap tool
You can use regular open source tool called Nmap. Best For Scanning Because Nmap has also the ability to guess host even after ICMP filter and Firewall. Type the following command to run ICMP IP Scan:
$ nmap -sP -PI 192.168.1.101/24
Create Ping Sweep Script
readers, Belive Me This Script is very also very good example of multi-processing because here in this script, to increase the speed of ping sweeping process, we will use multiprocessing module.
now, let's Talk about the basic structure of ping script.
1. This Function is for selecting commands for ping sweeping according to the operating system.
# Command Selecting Function
def set_os_command(self):
oper = platform.system()
if (oper=="Windows"):
ping = "ping -n {} {}"
elif (oper== "Linux"):
ping= "ping -c {} {}"
else :
ping= "ping -c {} {}"
self.commad=ping
return
2. This Function is for sending ICMP ECHO request and also for verifying response status.
# Function for Checking IP Status
def checkping(self, ip):
ping=self.commad
recv=os.popen(ping.format(self.timeout, ip)).read()
recv=recv.upper()
if recv.count('TTL'):
print "[+]\t {} \t==> Live ".format(ip)
self.live_ip_collector.put(ip)
return
3. This Function Is Using Special Technique For Selecting Range Of IP Address To Ping Sweep Scan
# Extracting Number format
def extraction(port):
storeport=[]
# Verifiying Port Value
if port:
# Verifying Port is in Range
if "-" in port and "," not in port:
x1,x2=port.split('-')
storeport=range(int(x1),int(x2))
# Verifying Port is in Commas
elif "," in port and "-" not in port:
storeport=port.split(',')
elif "," in port and "-" in port:
x2=[]
for i in port.split(','):
if '-' in i:
y1,y2=i.split('-')
x2=x2+range(int(y1),int(y2))
else:
x2.append(i)
storeport=x2
else:
storeport.append(port)
else:
pass
return storeport
# Extracting Ip Address
def IP_extractor(ip):
storeobj=[]
ip=ip.split(':')
x1=extraction(ip[0])
x2=extraction(ip[1])
x3=extraction(ip[2])
x4=extraction(ip[3])
for i1 in x1:
for i2 in x2:
for i3 in x3:
for i4 in x4:
storeobj.append("{}.{}.{}.{}".format(i1,i2,i3,i4))
return storeobj
4. This Function is For Using Multi-Processing In Scanning.
# Function For Multi_processing
def scanning_boosters(self):
proces=[]
for ip in self.target:
k=len(multiprocessing.active_children())
if k==self.thread:
time.sleep(3)
self.thread=self.thread+30
mythread=multiprocessing.Process(target=self.checkping, args=(ip,))
mythread.start()
proces.append(mythread)
for mythread in proces:
mythread.join()
self.timeclose=time.time()
self.showing_results()
return
Features Of This Script:
- High-Speed Ping Sweep.
- Stable Script
- Cross-platform Supported
- Result Save as txt
- Unique feature of input
Now, I hope you got the basic idea about the concept of python pinger script. so, it's time to assemble all above previewed function in one script to make a useful program. hence Here, it's my codes of python pinger.
Now, In Each Section, Choose Your Ip Address Digits Separated by Commas or Provide Range.
Ans. 192:168:10:1-7
Ans. 112-114,155,196-199:168:10:1
Specify IP Addresses Range For Scan. Eg:- 192:168:10:1-7
Specify Path For Saving Output in Txt.
Specify No. Of Request Per IP
Let's See, How Our Code Running.
hmm, our script running well.
If You Want Practical Usages Then Watch This Video.