我正在编写一个自动笔测试工具,用于获取连接到网络的所有活动主机的IP。它将一个列表输出到控制台,我想采取这些并创建一个inquirer问题,用户可以使用箭头键选择一个。{/ p>
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
功能。
答案 0 :(得分:0)
Python不是我最强的语言,但您应该能够使用output
(它将返回一个字符串数组)在output.splitlines()
中拆分行。从那里你可以将它传递给查询者choices
参数。