1

我正在使用 nmap 进行端口扫描功能。但是,我得到这种类型的字符串索引必须是整数的错误。能帮忙指点我哪里出错了吗?

        self.txtarea.Clear()    
        targetServerinput = self.targethost_input.GetValue()
        if len(targetServerinput) == 0:
            empty_error = wx.MessageDialog(None,"There is no input.\nPlease Try Again.","",wx.OK|wx.ICON_ERROR)
            empty_error.ShowModal()
        else:
            try:        
                targetServerIP = socket.gethostbyname(targetServerinput)    
                nm = nmap.PortScanner()
                nm.scan(targetServerIP,'1-1024')
                nm[targetServerIP]['tcp'].keys()
                for host in nm.all_hosts():
                    print('----------------------------------------------------')
                    print('Host : %s (%s)' % (host, nm[host].hostname()))
                        print('State : %s' % nm[host].state())
                        for proto in nm[host].all_protocols():
                         print('----------')
                             lport = nm[host][proto].keys()
                             lport.sort()
                             for port in lport:
                                print('port : %s\tstate : %s' %(port,nm[host][proto][port]['state']))

                #targetServerResult = str(targetServerIP)
            except socket.gaierror:
                name_error = wx.MessageDialog(None,"Name or service not known\nPlease Try Again.","",wx.OK|wx.ICON_ERROR)
                name_error.ShowModal()

我收到了这个错误-

Traceback (most recent call last):
  File "wxloginv1.py", line 168, in scan_btnClick
    print('port : %s\tstate : %s' %(port,nm[host][proto][port]['state']))
TypeError: string indices must be integers
4

2 回答 2

0

我有同样的问题,我发现将原型设置为静态 ['tcp'] 有帮助。(我的问题:Python Nmap Parser)。

    import nmap                         # import nmap.py module

    nm = nmap.PortScanner()

    host = '127.0.0.1'

    nm.scan(host, '1-1024')
    nm.command_line()
    nm.scaninfo()

    for host in nm.all_hosts():
        print('----------------------------------------------------')
        print('Host : %s (%s)' % (host, nm[host].hostname()))
        print('State : %s' % nm[host].state())
        print('----------------------------------------------------')

    for proto in nm[host].all_protocols():
            print('----------')
            print('Protocol : %s' % proto)

    lport = nm[host]['tcp'].keys()   #<------ This 'proto' was changed from the [proto] to the ['tcp'].
    lport.sort()
    for port in lport:
                    print('----------------------------------------------------')
                    print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
                    print('----------------------------------------------------')
于 2014-02-12T07:29:59.360 回答
0

在第 168 行 -

print('port : %s\tstate : %s' %(port,nm[host][proto][port]['state']))

编辑 - 这是真正发生的事情。

Python nmap 使用网站上详细介绍的示例实际上是您收到此错误的原因。密钥'state'并不总是在nm[host][proto][port]. 您需要将 for 循环更改为仅通过特定协议,例如 TCP 或 UDP。

于 2013-09-26T15:49:50.710 回答