Krcn U
Krcn U

Reputation: 421

searching a string and printing the complete line containing that string

I am trying to find out a MAC address in radius.log in the remote server with a small script in python. I want to search for one specific mac and print the complete line which contains that mac address. I can only do confirm the mac is found by search().

a part of my code is :

prog=re.compile(self.MAC_ADDR)
sess.exec_command('tail -f /usr/local/var/log/radius/radius.log')
rl, wl, xl = select.select([sess],[],[],0.0)
if len(rl) > 0:   #stdout
    block= sess.recv(1024)
    macfound=prog.search(block)
    if macfound:
        print "##############################################################################"
        print  self.MAC_ADDR,"found in tail"
        time.sleep (1)

Upvotes: 0

Views: 256

Answers (1)

User
User

Reputation: 14873

Only using strings could be faster:

for line in all_blocks.splitlines():
    if MAC in line:
         print(line)

This gives you all the lines that the mac address is in, using regular expresssions:

prog = re.compile('^.*' + re.escape(MAC) + '.*$', re.MULTILINE)
lines = prog.findall(all_blocks)

Upvotes: 1

Related Questions