forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple python script that will ping a range of a subnet when you su…
…pply the first three octets
- Loading branch information
1 parent
8d43958
commit 8675659
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Script Name : ping_subnet.py | ||
# Author : Craig Richards | ||
# Created : 12th January 2012 | ||
# Last Modified : | ||
# Version : 1.0 | ||
|
||
# Modifications : | ||
|
||
# Description : After supplying the first 3 octets it will scan the final range for available addresses | ||
|
||
import os # Load the Library Module | ||
import subprocess # Load the Library Module | ||
import sys # Load the Library Module | ||
|
||
filename = sys.argv[0] # Sets a variable for the script name | ||
|
||
if '-h' in sys.argv or '--h' in sys.argv or '-help' in sys.argv or '--help' in sys.argv: # Help Menu if called | ||
print ''' | ||
You need to supply the first octets of the address Usage : ''' + filename + ''' 111.111.111 ''' | ||
sys.exit(0) | ||
else: | ||
|
||
if (len(sys.argv) < 2): # If no arguments are passed then display the help and instructions on how to run the script | ||
sys.exit (' You need to supply the first octets of the address Usage : ' + filename + ' 111.111.111') | ||
|
||
subnet = sys.argv[1] # Set the variable subnet as the three octets you pass it | ||
|
||
if os.name == "posix": # Check the os, if it's linux then | ||
myping = "ping -c 2 " # This is the ping command | ||
elif os.name in ("nt", "dos", "ce"): # Check the os, if it's windows then | ||
myping = "ping -n 2 " # This is the ping command | ||
|
||
f = open('ping_'+subnet+'.log', 'w') # Open a logfile | ||
for ip in range(2,255): # Set the ip variable for the range of numbers | ||
ret = subprocess.call(myping + str(subnet)+"."+str(ip) , shell=True,stdout=f,stderr=subprocess.STDOUT) # Run the command pinging the servers | ||
if ret == 0: # Depending on the response | ||
f.write (subnet+"."+str(ip) + " is alive" + "\n") # Write out that you can receive a reponse | ||
else: | ||
f.write (subnet+"."+str(ip) + " did not respond" + "\n") # Write out you can't reach the box |