Nakita
Nakita

Reputation: 33

How to timeout the tail function in paramiko-expect?

I am using paramikoe's(paramiko-expect) tail functionality to tail a remote log file via ssh in python2.7. But I want it to stop after a specified time(like 12 hours or so). I do not want to use Ctrl+C. Is there a way to Add a timeout to it?

The purpose is to monitor a log file containing the errors occurring in the remote system. A mail will be sent having the error line from the log file in it. It should monitor only for the specified given time.

Any other alternative method of doing this is highly welcomed!

Thanks in advance!

Here is the code for it:

import paramiko

from paramikoe import SSHClientInteraction


def print_me(line_prefix, current_line):

  if not current_line == '':
      a = current_line
      print a
      #SEND MAIL CODE HERE
      return current_line

hostname = '1.1.1.1'
username = 'root'
password = 'abcd'
prompt = '.*=# '

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname, username=username, password=password)

interact = SSHClientInteraction(client,timeout=10,display=False)

interact.send('tail -f /usr/elog/elog | grep -i --line-buffered severity')
interact.tail(line_prefix = hostname+': ' , callback = print_me)

Upvotes: 3

Views: 852

Answers (1)

Fotis Gimian
Fotis Gimian

Reputation: 537

I'm the author of paramiko-expect :)

Unfortunately, there is no elegant solution currently except using the callback functionality to raise an exception which will interrupt the endless loop in the tail function. Unfortunately, this has some drawbacks as the connection may not be closed gracefully.

I encourage you to create an issue on GitHub under the repo and I'll be sure to consider a good solution to this in the future as it's come up before.

See Paramiko Expect - Tailing

All the best
Fotis

Upvotes: 1

Related Questions