查找并打印 - 正则表达式

find and print - regular expression

这个程序是登录到我的交换机并将输出复制到一个文件;第二部分用于查找关键字并打印整行。 当我 运行 代码的第一部分工作正常但代码的第二部分不打印包含我正在寻找的关键字的行.. 但是,当我单独 运行 代码的第二部分时,我能够打印包含 key_word 的行。 这里有什么问题?请帮帮我?

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

您在第 17 行将标准输出重定向到一个文件:

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

之后的所有打印语句都不会写入控制台,而是写入文件。

其次,您打开同一个文件两次,一次用于写入,一次用于读取,但在第二种情况下 f.readlines() returns 一个空列表。


示例说明为什么打开文件两次会出现问题。

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) 也是写入文件而不是显示匹配行。此外,您从未在第一部分中关闭文件,因此缓冲输出不会写入文件。

不要在第一部分使用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:有没有其他方法可以存储输出并在其中搜索字符串并打印输出?我想将数据存储在一个文件中并通过它进行搜索是最好的?

问题 3:我想添加一个 GUI,要求用户输入“wwn”并打印输出。有什么建议吗?

再次感谢:)

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