简体   繁体   English

查找并打印 - 正则表达式

[英]find and print - regular expression

This program is to login to my switch and copies output to a file;这个程序是登录到我的交换机并将output复制到一个文件中; and the second part is for finding a keyword and print the entire line.第二部分用于查找关键字并打印整行。 When I run the code the first part works fine but second part of the code does not print the line containing the key word i am looking for.. However, when i run the second part of the code separately i am able to print the line containing the key_word.当我运行代码时,第一部分工作正常,但代码的第二部分不打印包含我正在寻找的关键字的行。但是,当我单独运行代码的第二部分时,我能够打印该行包含关键字。 What is wrong here?这里有什么问题? Pease help me out?佩斯帮帮我?

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

You're redicting stdout to a file in line 17:您将 stdout 改写为第 17 行中的文件:

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

All print statements afterwards don't write to the console, but to the file.之后的所有打印语句都不会写入控制台,而是写入文件。

Secondly you open the same file twice, once for writing and once for reading, but in the second case f.readlines() returns an empty list.其次,您打开同一个文件两次,一次用于写入,一次用于读取,但在第二种情况下 f.readlines() 返回一个空列表。


Example to show why it's problematic to open the file twice.示例说明为什么打开文件两次会出现问题。

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')

The first part of the script redirected stdout to the file.脚本的第一部分将标准输出重定向到文件。 So print(line) in the second part is also writing to the file instead of displaying the matching line.所以第二部分中的print(line)也是写入文件而不是显示匹配的行。 Also, you never closed the file in the first part, so buffered output won't be written to the file.此外,您从未在第一部分关闭文件,因此缓冲的 output 不会被写入文件。

Don't use sys.stdout in the first part, use an ordinary variable.不要在第一部分使用sys.stdout ,使用普通变量。

Another problem is that you're overwriting the file for each command in commands .另一个问题是您正在覆盖commands中每个命令的文件。 You should open the file once before the loop, not each time through the loop.您应该在循环之前打开文件一次,而不是每次都通过循环。

wwn isn't a regular expression, there's no need to use re.search() . wwn不是正则表达式,不需要使用re.search() Just use if wwn in line: .只需使用if wwn in line: And you don't need to use f.readlines() , just loop through the file itself.而且您不需要使用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

got the code straight.直接得到代码。 And couple of questions to bug you and Maddi.还有几个问题要困扰你和 Maddi。

This code asks the user to enter the "wwn" to search for in the host and print the line which contains the "wwn" number.此代码要求用户输入“wwn”以在主机中搜索并打印包含“wwn”数字的行。

Question1: I would run this code multiple times whenever I would like to search for "wwn"... And here I would like to have a clear "zones_list.txt" file each time I start.问题1:每当我想搜索“wwn”时,我都会多次运行此代码......在这里我希望每次启动时都有一个清晰的“zones_list.txt”文件。 So I opened the file in 'w' mode -- SO this clears each time right?所以我以“w”模式打开文件——所以每次都会清除,对吗? Any other suggestion?还有什么建议吗?

Question2: IS there any other way to store the output and search it for a string and print the output?问题2:还有其他方法可以存储output并搜索字符串并打印output吗? I guess storing the data in a file and searching through it is the best?我想将数据存储在文件中并通过它进行搜索是最好的?

Question3: I would like to add a GUI where user is asked to enter the "wwn" and print the output.问题3:我想添加一个GUI,要求用户输入“wwn”并打印output。 Any suggestion?有什么建议吗?

Thanks again:)再次感谢:)

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