mercredi 6 juillet 2016

If found keywords print line else print python

i am trying to write a code where i can search certain keywords from different files ; if found print line and hostname ; else not found print "Test Passed" and host name

Hostsfile.txt router1 router2

router1.txt

not ready is ready reset me if required blah blahhh blahh

router2.txt

blah blahhh blahh

            hosts = open((hostsfile) , "r")
            keys = ['Not Ready','RESET']
            hosts = [hosts for hosts  in (hosts.strip() for hosts in open(hostsfile)) if hosts]
            for host2 in hosts:
              f = [f for f in (f.strip() for f in open(router1.txt)) if f]                  
                for line in f:
                    for keywords in keys:
                        if keywords in line:
                            print (line)
                            file2.write (line)
                        elif:
                            file2.write("Test Passed")

it return the correct search results but for all lines where it doesnot find keywords print "Test Passed", the output i am looking for is

router 1 not ready is ready reset me if required

router 2 test passed

Aucun commentaire:

Enregistrer un commentaire