基本 Python 脚本中的属性错误

发布于 2024-10-18 02:48:43 字数 2311 浏览 7 评论 0原文

这是一个非常基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-10-25 02:48:43

当您调用 portscan 时,您使用 args.ip 来调用它,而不是 args

您可以通过这样做来修复它:

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

或者,如果您只想传递 ip,您需要记住您为函数提供了 IP,而不是参数对象。

pingsweep 也是如此。

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.

我的影子我的梦 2024-10-25 02:48:43

您将 args.ip 传递给 portscan,然后 portscan 使用它的 ip 属性 (args.ip.ip)。显然,args.ipargs不同(对于某些对象的某些属性可以为真,但通常情况并非如此并且这里肯定是不合逻辑的)。将整个 args 传递给函数,或者(首选)让函数接受参数 ip 并使用它(而不是 ip.ip )。类似于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.

山川志 2024-10-25 02:48:43

您使用以下代码将参数传递给 portscanpingsweep

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

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

在这些函数中,您应该通过直接引用 args 来使用它们。使用 args.ip 和 args.ip3octets 是不正确的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文