Hey guys, this is my first project since learning the python basics so please bear with me!
I am trying to either portscan a host or ping sweep a class c network based on what the user specifies. The code is very basic I know but what's giving me issues is using the argparse module.
#!/usr/bin/python2.7
import argparse
import socket
import sys
def main():
parser = argparse.ArgumentParser(description="Do you wish to scan for live hosts or conduct a port scan?")
parser.add_argument("-s", dest='ip3octets', action='store', help='Enter the first three octets of the class C network to scan for live hosts')
parser.add_argument("-p", dest='ip', action='store',help='conduct a portscan of specified host')
args = parser.parse_args()
portscan(args.ip)
pingsweep(args.ip3octets)
def portscan():
for port in range(20, 1025):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
portinfo = s.connect_ex((args.ip, port))
if (portinfo == 0):
print port, " is open"
s.close()
def pingsweep():
for ip in range(1, 255):
host = args.ip3octets+"."+str(ip)
data = "ping -n 1 " +host
process = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE)
#give it time to respond
process.wait()
result_str = process.stdout.read()
if 'Destination host unreachable' not in result_str:
print host, ' is up'
if __name__ == "__main__": main()
What I want is for argparse to store the values in ip and ip3octets and then call the appropiate function. I'm not sure however if it's the way i'm calling the functions, or the way argparse is setup that is causing the problems
Any help is much appreciated!
Cheers