基本 Python 脚本中的属性错误
这是一个非常基本的 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'
关于我哪里出错了有什么想法吗?多谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您调用
portscan
时,您使用args.ip
来调用它,而不是args
。您可以通过这样做来修复它:
或者,如果您只想传递
ip
,您需要记住您为函数提供了 IP,而不是参数对象。pingsweep
也是如此。When you call
portscan
, you call it withargs.ip
, notargs
.You could fix it by doing this:
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
.您将
args.ip
传递给 portscan,然后 portscan 使用它的ip
属性 (args.ip.ip
)。显然,args.ip
与args
不同(对于某些对象的某些属性可以为真,但通常情况并非如此并且这里肯定是不合逻辑的)。将整个args
传递给函数,或者(首选)让函数接受参数ip
并使用它(而不是ip.ip
)。类似于pingsweep
。You're passing
args.ip
to portscan, which then uses theip
attribute of that (args.ip.ip
). Obviously,args.ip
is not the same thing asargs
(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 wholeargs
to the function or (preferred) make the function take an argumentip
and just use that (instead ofip.ip
). Analogous forpingsweep
.您使用以下代码将参数传递给
portscan
和pingsweep
:在这些函数中,您应该通过直接引用
args
来使用它们。使用 args.ip 和 args.ip3octets 是不正确的。You passed the arguments to
portscan
andpingsweep
with this code:In these functions, you should use them by referencing directly
args
. Usingargs.ip
andargs.ip3octets
is not correct.