这是一个非常基本的 portscan/ping 扫描脚本。当我在另一个脚本中单独使用它们时,这两个函数工作正常,但是一旦我在这个脚本中尝试它们,我就会得到属性错误
#!/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()
如果我使用 portscan (-p) 函数,我会收到此错误:
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'
虽然使用 pingsweep (-s) 函数会产生此错误:
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'
关于我哪里出错的任何想法?非常感谢!