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

Ok, I think the above was a bit confusing so I'll try and simplify it:

This works and will print whatever value i give -s or -p:

import argparse

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()

    print args.ip3octets
    print args.ip

But then this, which to me is logically identical, gives me errors:

#!/usr/bin/python2.7 

import argparse

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()

    printip3octets()
    printip()

def printip3octets():

    print args.ip3octets

def printip():

    print args.ip

if __name__ == "__main__":main()

That produces these errors:

Traceback (most recent call last):
File "./argparser.py", line 24, in <module>
if __name__ == "__main__":main()
File "./argparser.py", line 13, in main
printip3octets()
File "./argparser.py", line 18, in printip3octets
print args.ip3octets
NameError: global name 'args' is not defined

Thanks for any help =)

I managed to get an answer on another forum, I needed to pass the arg variable to the functions as a parameter, like so:

printip(args.ip)

def printip(args):

    print args.ip3

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.