简体   繁体   中英

find and print - regular expression

This program is to login to my switch and copies output to a file; 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:

    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.


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. Also, you never closed the file in the first part, so buffered output won't be written to the file.

Don't use sys.stdout in the first part, use an ordinary variable.

Another problem is that you're overwriting the file for each command in 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() . Just use if wwn in line: . And you don't need to use f.readlines() , just loop through the file itself.

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.

This code asks the user to enter the "wwn" to search for in the host and print the line which contains the "wwn" number.

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. So I opened the file in 'w' mode -- SO this clears each time right? Any other suggestion?

Question2: IS there any other way to store the output and search it for a string and print the 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM