Skip to content

Commit 8675659

Browse files
committed
A simple python script that will ping a range of a subnet when you supply the first three octets
1 parent 8d43958 commit 8675659

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

ping_subnet.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Script Name : ping_subnet.py
2+
# Author : Craig Richards
3+
# Created : 12th January 2012
4+
# Last Modified :
5+
# Version : 1.0
6+
7+
# Modifications :
8+
9+
# Description : After supplying the first 3 octets it will scan the final range for available addresses
10+
11+
import os # Load the Library Module
12+
import subprocess # Load the Library Module
13+
import sys # Load the Library Module
14+
15+
filename = sys.argv[0] # Sets a variable for the script name
16+
17+
if '-h' in sys.argv or '--h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: # Help Menu if called
18+
print '''
19+
You need to supply the first octets of the address Usage : ''' + filename + ''' 111.111.111 '''
20+
sys.exit(0)
21+
else:
22+
23+
if (len(sys.argv) < 2): # If no arguments are passed then display the help and instructions on how to run the script
24+
sys.exit (' You need to supply the first octets of the address Usage : ' + filename + ' 111.111.111')
25+
26+
subnet = sys.argv[1] # Set the variable subnet as the three octets you pass it
27+
28+
if os.name == "posix": # Check the os, if it's linux then
29+
myping = "ping -c 2 " # This is the ping command
30+
elif os.name in ("nt", "dos", "ce"): # Check the os, if it's windows then
31+
myping = "ping -n 2 " # This is the ping command
32+
33+
f = open('ping_'+subnet+'.log', 'w') # Open a logfile
34+
for ip in range(2,255): # Set the ip variable for the range of numbers
35+
ret = subprocess.call(myping + str(subnet)+"."+str(ip) , shell=True,stdout=f,stderr=subprocess.STDOUT) # Run the command pinging the servers
36+
if ret == 0: # Depending on the response
37+
f.write (subnet+"."+str(ip) + " is alive" + "\n") # Write out that you can receive a reponse
38+
else:
39+
f.write (subnet+"."+str(ip) + " did not respond" + "\n") # Write out you can't reach the box

0 commit comments

Comments
 (0)