【问题标题】:Parsing Console Output to Individual Strings将控制台输出解析为单个字符串
【发布时间】:2017-03-23 19:49:12
【问题描述】:

我正在编写一个自动渗透测试工具,它可以获取连接到网络的所有活动主机的 IP。它将一个列表输出到控制台中,我想使用这些并创建一个inquirer 问题,用户可以使用箭头键选择一个问题。

import nmap
import subprocess
rhostcommand = "nmap -n -sn " + lhost.rstrip()[:-3] + "-255 -oG - | awk '/Up$/{print $2}'"
pst = subprocess.Popen(rhostcommand, shell=True, stdout=subprocess.PIPE)
output = pst.stdout.read()
if output == None:
    print "Couldn't resolve Remote Host IPs.\n[ref. 0000003]"
else:
    print "Remote Host IPs:\n"
    print output.rstrip()

这是我创建 IP 列表的代码。这是一个示例输出:

Remote Host IPs:

172.16.96.1
172.16.96.2
172.16.96.113
172.16.96.116
172.16.96.117
172.16.96.212
172.16.96.218
172.16.96.219
172.16.96.225

这是我当前的查询代码,我会解释错误:

import inquirer
questions = [
        inquirer.List('rhostips',
            message="Choose target IP"
            choices=int(output),
        ),
]
answers = inquirer.prompt(questions)
print answers ['rhostips']

我遇到的问题是choices=int(output) 行。我需要将output 格式化,以便inquirer 可以识别它。接受的格式如下:

import inquirer
questions = [
  inquirer.List('size',
                message="What size do you need?",
                choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
            ),
]
answers = inquirer.prompt(questions)

感谢您提供的任何答案。

已解决,正确代码如下:

outputsplit = output.splitlines()

import inquirer
questions = [
        inquirer.List('rhostips',
            message="Choose target IP",
            choices=outputsplit,
        ),
]
answers = inquirer.prompt(questions)
print answers ['rhostips']

使用了splitlines 函数。

【问题讨论】:

    标签: python console output


    【解决方案1】:

    Python 不是我最擅长的语言,但您应该能够使用 output.splitlines() 拆分 output 中的行(这将返回一个字符串数组)。从那里您可以将其传递给查询器choices 参数。

    【讨论】:

      猜你喜欢
      • 2011-05-16
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 2019-05-02
      • 2015-03-09
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      相关资源
      最近更新 更多