Attribute errors in basic Python script
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 err开发者_开发百科or:
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!
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
.
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
.
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.
精彩评论