2017-03-23 9 views
0

私は、ネットワークに接続されているすべてのアクティブホストの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機能を使用します。

答えて

0

Pythonは私の最も強い言語ではありませんが、outputの行を、output.splitlines()(これは文字列の配列を返します)を使って分割することができます。そこから照会者choicesのパラメータに渡すことができます。

関連する問題