0% found this document useful (1 vote)
2K views

Create High Speed Ping Scanning Script With Python

FInd Live Host With The Help Of Automatic Ping Scanning Script. This Paper is Very Good Resource To Increase Your Python Networking Knowledge And Also, THis Tutorial will show you how to use multi-processing efficiently.

Uploaded by

Suraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views

Create High Speed Ping Scanning Script With Python

FInd Live Host With The Help Of Automatic Ping Scanning Script. This Paper is Very Good Resource To Increase Your Python Networking Knowledge And Also, THis Tutorial will show you how to use multi-processing efficiently.

Uploaded by

Suraj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Hacking With Python

Part I

Create High-Speed Ping Sweeping Script

By Suraj Singh Bisht

[email protected]
www.bitforestinfo.com
Index

1. Overview ......................................... 03

2. What is Ping Scan ................................. 04

3. Why Create Ping Sweep Script ...................... 05

4. Find Live System .................................. 06

Windows ................................. 06
Linux .................................. 06
Nmap .................................... 06

5. Create Ping Sweep Script .......................... 07

Function 1 .............................. 07
Function 2 .............................. 07
Function 3 .............................. 08
Function 4 .............................. 09
Features Of Script ...................... 09

6. Ping Sweep Codes .................................. 10

7. How To Use It ..................................... 14

8. Conclusion ........................................ 15

9. About Me .......................................... 16

10. You Can Follow Us ................................ 17


Overview

This Paper is created for providing a free tutorial on


Python About How We Can Create Python Script That Can
Ping Various Systems Automatically. In Simple Words, Ping
Sweeping is a set of procedure to Find Online Available
Machine in Specific Network. Now, Let Assume If You Want
To Scan Big List Of IP Addresses To Find Live Host
Systems But this Procedure is very time-consuming. So,
here we can use our automatic ping sweeping python script
that can handle all our works automatically.

So, In This Paper, I am going to explore below points.

1. What is Ping Scan?


2. How to Find Live Systems?
3. How to Create Ping Sweep Script?
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


Systems 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 dont 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

Friends, 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

Now, let me share with you my complete codes.

Features Of This Script:

High-Speed Ping Sweep.

Stable Script

Cross-platform Supported

Result Save as txt

Unique feature of input:

Here, it's my codes of python pinger.


pypinger.py

#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------


# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://bitforestinfo.blogspot.com
# This Script is Written By
__author__='''

######################################################
By S.S.B Group
######################################################

Suraj Singh
Admin
S.S.B Group
[email protected]
http://bitforestinfo.blogspot.in/

Note: We Feel Proud To Be Indian


######################################################
'''
# =================Other Configuration================
# Usages :
usage = "usage: %prog [options] "
# Version
Version="%prog 0.0.1"
# ====================================================

# Importing Modules
import os, multiprocessing, time, optparse, platform

# Main Engine
class Pinger:
def __init__(self, target, thread, output, timeout):
self.timestarted=time.time()
self.live_ip_collector=multiprocessing.Queue()
self.target=target
self.thread=thread
self.output=output
self.timeout=timeout
self.set_os_command()
#self.checkping()
self.scanning_boosters()

# Saving OUtput
def save_output(self):
f=open(self.output,'a')
for i in self.live_ip_collector:
f.write(i+'\n')
f.close()
return
# 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

# Printing Function
def showing_results(self):
storeip=[]
x=1
while x==1:
try:
storeip.append(self.live_ip_collector.get_nowait())
except:
x=x+1
self.live_ip_collector=storeip

print "\n"*3,"#"*80
print "[+] Scan Started On \t\t:\t",time.ctime(self.timestarted)
print "[+] Scan Closed On \t\t:\t",time.ctime(self.timeclose)
print "[+] Scan Total Duration \t:\t",self.timeclose-self.timestarted
print "[+] Total Live System Answered\t:\t",len(self.live_ip_collector)
if self.output:
self.save_output()
print "\n[+] Thanks For Using My Program. By S.S.B"
return

# 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

# 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
# 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
def main():
print __author__
parser=optparse.OptionParser(usage=usage,version=Version)
parser.add_option('-i','--target',type='string',dest='target',help="Specify IP
Addresses Range For Scan", default=None)
parser.add_option('-t',"--thread",type='string', dest="thread", help="Specify
Number of Thread For Scanning ", default='100')
parser.add_option('-o',"--output",type='string', dest="output", help="Specify
Path For Saving Output in Txt.", default="live_ip.txt")
parser.add_option('-c','--timeout',type='string', dest="timeout", help="Specify
No. Of Request Per IP",default='1')
(options, args)= parser.parse_args()
if not options.target:
print "[+] Please Provide IP Range. e.g: 192-192:128:1:4-70, For More, Check
Readme "
exit(0)
target=options.target
thread=options.thread
output=options.output
timeout=options.timeout
target=IP_extractor(target)
Pinger(target,thread,output,timeout)
return

# Trigger
if __name__ == '__main__':
main()
How To Use It
For Usages, Raw Script And More Info:

Follow my blog post link : http://www.bitforestinfo.com/2017/02/how-to-create-ping-


sweeping-script.html

Usages Preview
Conclusion
Ping Sweeping is a very useful technique to find live hosts. With the help of multi-
processing module in python, we can boost various processes in the python script.
And Also, You Can Follow And Share My Blog And Github Account To Connect
With Our Bitforestinfo Audience And Also With Me.
About Me

Blog Introduction

Bitforestinfo A blog Based On technical knowledge, tutorials,


how-to guides, hacking and programming solutions. In this blog you will find articles
related to python programming, penetrating testing, ethical hacking, Linux, Linux
and many more interesting topics.

About Me (Blog Admin)

My Name is Suraj Singh Bisht, A Friendly And


Lazy Boy Who Want To Learn And Share About Every Thing Like Hacking,
Cracking, Programming, Blogging And Much More.
I also like to write articles on a wide range of topics like Hacking, Cracking,
Penetration testing, Programming etc.

"Python Is My Love,
Linux Is My Habit,
Web Scraping is my hobby,
C/C++ is my Interest, And,
Technology is my life "

I live in India,
You Can Follow Me On

Email [email protected]

Blog www.bitforestinfo.com
RSS Feed www.bitforestinfo.com/feeds/posts/default
Github github.com/surajsinghbisht054
Google+ plus.google.com//111795052270500977970
Facebook facebook.com/bitforestinfo/
Twitter twitter.com/bitforestinfo
Forum www.bitforestinfo.com/p/forum.html

Thanks For Reading,

Nice Day.

You might also like