0

This is a very basic portscan/ping sweep script. The two functions work fine when I just use them individually in another script, but as soon as I try them in this script I get attribute errors

#!/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()

    if args.ip != None:
        portscan(args.ip)

    if args.ip3octets != None:
        pingsweep(args.ip3octets)

def portscan(args):
    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(args):
    for ips in range(1, 255):

                host = args.ip3octets+"."+str(ip)
                data = "ping -c 1 " +host
                process = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE)
                #give it time to respond
                process.wait()
                result_str = process.stdout.read()

                if '64 bytes from' in result_str:
                        print host, ' is up'

if __name__ == "__main__":main()

If I use the portscan (-p) function I get this error:

Traceback (most recent call last):
  File "./portscannertest.py", line 42, in <module>
    if __name__ == "__main__":main()
  File "./portscannertest.py", line 16, in main
    portscan(args.ip)
  File "./portscannertest.py", line 24, in portscan
    portinfo = s.connect_ex((args.ip, port))
AttributeError: 'str' object has no attribute 'ip'

Whilst using the pingsweep (-s) function produces this error:

Traceback (most recent call last):
  File "./portscannertest.py", line 42, in <module>
    if __name__ == "__main__":main()
  File "./portscannertest.py", line 19, in main
    pingsweep(args.ip3octets)
  File "./portscannertest.py", line 32, in pingsweep
    host = args.ip3octets+"."+str(ip)
AttributeError: 'str' object has no attribute 'ip3octets'

Any ideas as to where I'm going wrong? Thanks a lot!

1
  • 1
    Typically you want to use #!/usr/bin/env python or #!/usr/bin/env python2.7 instead of #!/usr/bin/python2.7. The former are more accomodating than the latter if other systems have python installed in slightly different paths. Of course if you know you definitely want exactly that python, it's reasonable. Commented Feb 19, 2011 at 16:22

3 Answers 3

1

When you call portscan, you call it with args.ip, not args.

You could fix it by doing this:

if args.ip != None:
    portscan(args)

Alternatively, if you want to only pass in the ip, you need to remember that you're giving the function the IP, and not the arguments object.

The same goes for pingsweep.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, i think understand - thanks very much. However if I do as you say I recieve a global error: NameError: global name 'ip' is not defined So I assume fixing this will require a rework of the code?
@Eol: That is probably because you mean str(ips) instead of str(ip) in pingsweep.
1

You're passing args.ip to portscan, which then uses the ip attribute of that (args.ip.ip). Obviously, args.ip is not the same thing as args (can be true for some attributes of some objects, but generally it's not the case and would certainly be illogical here). Either pass the whole args to the function or (preferred) make the function take an argument ip and just use that (instead of ip.ip). Analogous for pingsweep.

Comments

0

You passed the arguments to portscan and pingsweep with this code:

if args.ip != None:
    portscan(args.ip)

if args.ip3octets != None:
    pingsweep(args.ip3octets)  

In these functions, you should use them by referencing directly args. Using args.ip and args.ip3octets is not correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.