Python nmap:TypeError:列表索引必须是整数或片,而不是s

2024-10-03 09:16:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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("The host is: " + nm_scanner['scan']['192.168.0.1']['portused']['portid']) TypeError: list indices must be integers or slices, not 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'
              ]
            }
          ]
        }
      ]
    }
  }
}

Tags: ornamescantypelinknmaptcpscanner
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'])

您使用的是nm\u扫描器的字符串索引,而不是数字。nm\U扫描仪是字典还是列表?你可以这样说。你知道吗

type(nm_scanner)

如果它是一个列表,那么您必须使用数字作为索引(在方括号中)。你可以用字符串作为字典。 您可以通过打印nm\U扫描仪并粘贴在此处来帮助我们。你知道吗

相关问题 更多 >