2017-01-10 4 views
-1

レポートの目的でいくつかのSSL検出スキャンの結果をテーブルとして出力しようとしていますが、そのデータを必要な方法で解析する際に問題があります。各ループを格納し、ループ完了時にテーブルに出力する

私は、出力は次のようになりたい:

IP Address Common Name    Valid To 
------------ ---------------------- ---------- 
10.0.255.250 ex.example.com 2017/02/09 
10.0.255.251 localhost  2009/07/04 
10.0.255.252 ex.example2.com  2016/05/24 

ではなく、それはこのようになります。

IP Address Common Name    Valid To 
------------ ---------------------- ---------- 
10.0.255.250 ex.example.com 2017/02/09 
IP Address Common Name Valid To 
------------ ------------- ---------- 
10.0.255.251 localhost  2009/07/04 
None 
IP Address Common Name Valid To 
------------ ------------- ---------- 
10.0.255.252 ex.example2.com  2016/05/2 

私はこれを整理するためにwhileループを使用しようとしており、運がなかった。 "while"ループと "for each"ループのほうが少ないようです。

def tabulateText(): 
    loop = True 
    while loop == True: 
     with open("testinput.txt", "r") as text_file: 
      for line in text_file: 
       if "end" in line: 
        loop = False 
       elif "IP Address =" in line: 
        start = line.find('IP Address = ') 
        endline = line.find('\n', start) 
        ip = line[start+13:endline] 
        cert = SSLmon(ip) 
        Col1 = ip 
        FCS2 = cert.find('commonName') 
        FCE2 = cert.find('/', FCS2) 
        FCS2b = cert.find('commonName') 
        FCE2b = cert.find('\n', FCS2b) 
        Colopt2 = cert[FCS2+11:FCE2] 
        Colopt2b = cert[FCS2b+11:FCE2b] 
        Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b] 
        FCS3 = cert.find('Not valid after:') 
        FCE3 = cert.find('T', FCS3) 
        Col3 = cert[FCS3+18:FCE3].replace('-', '/') 
        column = Col1[n], Col2[n], Col3[n] 
        print(tabulate([column], headers=['IP Address', 'Common Name', 'Valid To'])) 
       else: 
        pass 

print(tabulateText()) 
print(tabulateText()) 
print(tabulateText()) 
+1

それぞれの 'line in text_file'を別々に表にしています。 'while'ループ*内に' for'(each)ループ*があります。外側のループは1回だけ実行されます! 'for'ループ*の外側で' tabulate' *を動かして、 'while'を一掃してください。 – jonrsharpe

+0

また、次の点に注意してください。1.あなたはあなたのタイトルで説明したことをやろうとはしません。 2. 'endline'が冗長であることを見つける - ' \ n'は(必然的に!)行の最後の文字です。 – jonrsharpe

+0

@jonrsharpe Endは入力として使用しているテキストファイルの最後の単語です。私は各ループに3つの列を格納させ、入力文書をループした後、それらの値を適切な列の順序で行に出力します。 – Ryan

答えて

1

プリント(([列]、ヘッダーを表..)コマンドは、ヘッダーとデータの1行のみ見る理由である「IPアドレス」の行毎に、読み出した後に実行される。

の後にprint(tabulate())コマンドを実行して、すべての行を読み込んだ後に、結果の各行を配列に追加します。

まず、テキストファイルを読み込む前に空の配列を作成します - テーブル:

table = [] 
    with open("testinput.txt", "r") as text_file: 

テーブル配列(table.append [column])にカラム配列を追加し、現在の位置にあるprint(tabulate())コマンドを削除します。

column = Col1[n], Col2[n], Col3[n] 
table.append[column] 
印刷を移動

ファイルを通して読んだ後に非常に終わりにコマンドを、(()集計)と、新しい変数「テーブル」を参照。

print (tabulate(table), headers=['...']) 

ループは必要ありません。 "for line"コマンドは、テキストファイルをループします。また、行われたときに関数は次のようなものになります(text_file.closed)

をファイルを閉じます:表の配列自体はこのようになります

table = [] 
with open("testinput.txt", "r") as text_file: 
    for line in text_file: 
     if "IP Address =" in line: 
      start = line.find('IP Address = ') 
      endline = line.find('\n', start) 
      ip = line[start+13:endline] 
      cert = SSLmon(ip) 
      Col1 = ip 
      FCS2 = cert.find('commonName') 
      FCE2 = cert.find('/', FCS2) 
      FCS2b = cert.find('commonName') 
      FCE2b = cert.find('\n', FCS2b) 
      Colopt2 = cert[FCS2+11:FCE2] 
      Colopt2b = cert[FCS2b+11:FCE2b] 
      Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b] 
      FCS3 = cert.find('Not valid after:') 
      FCE3 = cert.find('T', FCS3) 
      Col3 = cert[FCS3+18:FCE3].replace('-', '/') 
      column = Col1[n], Col2[n], Col3[n] 
      table.append[column] 
    text_file.closed 
    print(tabulate([table], headers=['IP Address', 'Common Name', 'Valid To'])) 

することで、印刷(表)コマンドを実行します:

['10 .0.255.250 '、' ex.example.com '、' 2017/02/09 ']、[' 10.0.255.251 '、' localhost '、' 2009/07/04 ']、[' '10 .0.255.252 '、' ex.example2.com '、' 2016/05/24 ']]

0

私が望むデータを得るために別のアプローチをとった。私は最終的に、このように.ljust()によって制御されたパディングと一緒に各列を印刷することになった。

with open("testinput.txt", "r") as text_file: 
    for line in text_file: 
     if "IP Address =" in line: 
      start = line.find('IP Address = ') 
      endline = line.find('\n', start) 
      ip = line[start+13:endline] 
      cert = SSLmon(ip) 
      Col1 = ip 
      FCS2 = cert.find('commonName') 
      FCE2 = cert.find('/', FCS2) 
      FCS2b = cert.find('commonName') 
      FCE2b = cert.find('\n', FCS2b) 
      Colopt2 = cert[FCS2+11:FCE2] 
      Colopt2b = cert[FCS2b+11:FCE2b] 
      Col2 = cert[FCS2+11:FCE2] if len(Colopt2) < len(Colopt2b) else cert[FCS2b+11:FCE2b] 
      FCS3 = cert.find('Not valid after:') 
      FCE3 = cert.find('T', FCS3) 
      Col3 = cert[FCS3+18:FCE3].replace('-', '/') 
      with open(filename1, 'a') as f: 
       line = '%s %s %s %s' % (Col1.ljust(20), Col2.ljust(35), Col3.ljust(15), '\n') 
       f.write(line) 
関連する問題