簡體   English   中英

查找並打印 - 正則表達式

[英]find and print - regular expression

這個程序是登錄到我的交換機並將output復制到一個文件中; 第二部分用於查找關鍵字並打印整行。 當我運行代碼時,第一部分工作正常,但代碼的第二部分不打印包含我正在尋找的關鍵字的行。但是,當我單獨運行代碼的第二部分時,我能夠打印該行包含關鍵字。 這里有什么問題? 佩斯幫幫我?

import paramiko
import sys
import re

host = "15.112.34.36"
port = 22
username = "admin"
password = "ssmssm99"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)

commands = ["switchshow"]
for command in commands:
    print(command)
    sys.stdout = open('zones_list.txt', 'w')
    stdin, stdout, stderr = ssh.exec_command(command)
    lines = stdout.readlines()
    lines = "".join(lines)

    print(lines)
    ssh.close()

#SECOND PART
#open the zone_list file and search for a keyword
#search for wwn and print the entire line --> doesnt print why?
wwn = "10:00:00:90:fa:73:df:c9"
with open('zones_list.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        if re.search(r'10:00:00:90:fa:73:df:c9', line):
            print (line)
            break

您將 stdout 改寫為第 17 行中的文件:

    sys.stdout = open('zones_list.txt', 'w')

之后的所有打印語句都不會寫入控制台,而是寫入文件。

其次,您打開同一個文件兩次,一次用於寫入,一次用於讀取,但在第二種情況下 f.readlines() 返回一個空列表。


示例說明為什么打開文件兩次會出現問題。

import sys

# 1. Opening the file without closing it => will be closed at the end of the program
# 2. stdout now writes into the file
sys.stdout = open('text3', 'w')

# Will be writen into the file when the program finishes
print ('line1')
print ('line2')

# We open the file a second time
with open('text3', 'r') as f:
    # lines will always be an empty list, no matter if there was something in the file before
    lines = f.readlines()
    # Writing to stderr so we see the output in  the console (stdout still points at the file)
    sys.stderr.write('length: {}'.format(len(lines)))
    for line in lines:
        # We should never be here
        sys.stderr.write (line)

# write some more stuff the the file
for i in range(1, 6):
    print ('i + {}'.format(i))
print('line3')

腳本的第一部分將標准輸出重定向到文件。 所以第二部分中的print(line)也是寫入文件而不是顯示匹配的行。 此外,您從未在第一部分關閉文件,因此緩沖的 output 不會被寫入文件。

不要在第一部分使用sys.stdout ,使用普通變量。

另一個問題是您正在覆蓋commands中每個命令的文件。 您應該在循環之前打開文件一次,而不是每次都通過循環。

wwn不是正則表達式,不需要使用re.search() 只需使用if wwn in line: 而且您不需要使用f.readlines() ,只需遍歷文件本身即可。

import paramiko
import sys

host = "15.112.34.36"
port = 22
username = "admin"
password = "ssmssm99"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)

commands = ["switchshow"]
with open('zones_list.txt', 'w') as f:
    for command in commands:
        print(command)
        stdin, stdout, stderr = ssh.exec_command(command)
        lines = stdout.readlines()
        lines = "".join(lines)

        print(lines, file=f)
        ssh.close()

#SECOND PART
#open the zone_list file and search for a keyword
#search for wwn and print the entire line --> doesnt print why?
wwn = "10:00:00:90:fa:73:df:c9"
with open('zones_list.txt', 'r') as f:
    for line in f:
        if wwn in line:
            print (line)
            break

直接得到代碼。 還有幾個問題要困擾你和 Maddi。

此代碼要求用戶輸入“wwn”以在主機中搜索並打印包含“wwn”數字的行。

問題1:每當我想搜索“wwn”時,我都會多次運行此代碼......在這里我希望每次啟動時都有一個清晰的“zones_list.txt”文件。 所以我以“w”模式打開文件——所以每次都會清除,對嗎? 還有什么建議嗎?

問題2:還有其他方法可以存儲output並搜索字符串並打印output嗎? 我想將數據存儲在文件中並通過它進行搜索是最好的?

問題3:我想添加一個GUI,要求用戶輸入“wwn”並打印output。 有什么建議嗎?

再次感謝:)

import paramiko
import sys
import re

#host details to fetch data - nodefind
host = "15.112.34.36"
port = 22
username = "admin"
password = "ssmssm99"

#shell into the client host
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)

#asking user to enter the wwn to search in the host
wwn = input('Enter the wwn to be searched >')
#for example command is: nodefind 20:34:00:02:ac:07:e9:d5
commands = ["nodefind " + wwn, "switchshow"]
f =open('zones_list.txt', 'w')
for command in commands:
    print(command)
    stdin, stdout, stderr = ssh.exec_command(command)
    lines = stdout.readlines()
    lines = "".join(lines)
    print(lines, file=open('zones_list.txt', 'a'))
ssh.close()

#print a particular line in console to the user
f =open('zones_list.txt', 'r')
for line in f:
    if wwn in line:
        print(line)
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM