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!
#!/usr/bin/env pythonor#!/usr/bin/env python2.7instead 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.