【问题标题】:Python nmap: TypeError: list indices must be integers or slices, not strPython nmap:TypeError:列表索引必须是整数或切片,而不是str
【发布时间】:2019-12-04 10:27:27
【问题描述】:

我正在尝试使用 nmap 模块获取目标系统端口 ID,但出现列表索引错误

import nmap
nm_scan = nmap.PortScanner()
nm_scanner = nm_scan.scan('192.168.0.1', '80', arguments='-O')
print("Portid: " + nm_scanner['scan']['192.168.0.1']['portused']['portid'])

print("主机是:" + nm_scanner['scan']['192.168.0.1']['portused']['portid']) TypeError: 列表索引必须是整数或切片,而不是 str

print(nm_scanner)

{
  'nmap': {
    'command_line': 'nmap -oX - -p 80 -O 192.168.0.1',
    'scaninfo': {
      'tcp': {
        'method': 'syn',
        'services': '80'
      }
    },
    'scanstats': {
      'timestr': 'Fri Jul 26 11:31:30 2019',
      'elapsed': '6.36',
      'uphosts': '1',
      'downhosts': '0',
      'totalhosts': '1'
    }
  },
  'scan': {
    '192.168.0.1': {
      'hostnames': [
        {
          'name': '',
          'type': ''
        }
      ],
      'addresses': {
        'ipv4': '192.168.0.1',
        'mac': '1C:5F:2B:53:45:4F'
      },
      'vendor': {
        '1C:5F:2B:53:45:4F': 'D-Link International'
      },
      'status': {
        'state': 'up',
        'reason': 'arp-response'
      },
      'uptime': {
        'seconds': '87161',
        'lastboot': 'Thu Jul 25 11:18:49 2019'
      },
      'tcp': {
        80: {
          'state': 'open',
          'reason': 'syn-ack',
          'name': 'http',
          'product': '',
          'version': '',
          'extrainfo': '',
          'conf': '3',
          'cpe': ''
        }
      },
      'portused': [
        {
          'state': 'open',
          'proto': 'tcp',
          'portid': '80'
        },
        {
          'state': 'closed',
          'proto': 'udp',
          'portid': '42514'
        }
      ],
      'osmatch': [
        {
          'name': 'Allied Telesyn AT-GS950 or D-Link DES-3226L switch or D-Link DSL-2750U router',
          'accuracy': '100',
          'line': '2603',
          'osclass': [
            {
              'type': 'switch',
              'vendor': 'Allied Telesyn',
              'osfamily': 'embedded',
              'osgen': None,
              'accuracy': '100',
              'cpe': [
                'cpe:/h:alliedtelesyn:at-gs950'
              ]
            },
            {
              'type': 'switch',
              'vendor': 'D-Link',
              'osfamily': 'embedded',
              'osgen': None,
              'accuracy': '100',
              'cpe': [
                'cpe:/h:dlink:des-3226l',
                'cpe:/h:dlink:dsl-2750u'
              ]
            }
          ]
        }
      ]
    }
  }
}

【问题讨论】:

  • print(nm_scanner) 得到什么?
  • @Selcuk 类似这样的东西 {'portused': [{'state': 'open', 'proto': 'tcp', 'portid': '80'}}
  • 如果您发布实际结果而不是假设输出,我们可以为您提供帮助。
  • @Selcuk 我添加了 nm_scanner 的输出
  • @DhruvikMevada 有两个 portid,你需要哪一个?

标签: python python-3.x


【解决方案1】:

您正在为 nm_scanner 而不是数字使用字符串索引。 nm_scanner 是字典还是列表?您可以通过以下方式知道。

type(nm_scanner)

如果它是一个列表,那么您必须使用数字作为索引(在方括号中)。您可以使用字符串作为字典。 您可以通过打印 nm_scanner 并在此处粘贴来为我们提供更多帮助。

【讨论】:

  • 虽然技术上是正确的,但这篇文章并没有回答这个问题。
【解决方案2】:

从输出中可以看出,有多个端口,所以必须使用:

nm_scanner['scan']['192.168.0.1']['portused'][0]['portid']

对于第一个,或者遍历所有使用:

for port in nm_scanner['scan']['192.168.0.1']['portused']:
    print(port['portid'])

【讨论】:

    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2020-05-14
    • 2020-11-21
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多