I'd like to use paramiko and an sftp client to poll a log file for the last line. I'm aware of sshtail module in python, but using it goes against the coding standards where I am right now.
I previously used that and now I'm wondering how to go about reading the last line of a log file?
Thanks,
Parth
EDIT2:
try:
self.logger.info("SSH Log: trying to connect to: " + self.ssh_server_ip + "," + str(self.ssh_port) + "," + self.ssh_username + "," + self.ssh_password)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(self.ssh_server_ip, self.log_server_port, self.ssh_username, self.ssh_password)
self.logger.info("SSH LOG: Deleting files from HTTP Upload Server")
sftp = client.open_sftp()
remote_command = "tail -n1 /var/log/apache2/access.log"
stdin, stdout, stderr = client.exec_command(remote_command)
last_line = stdout.read()
old_line = last_line
while 1:
remote_command = "tail -n1 /var/log/apache2/access.log"
stdin, stdout, stderr = client.exec_command(remote_command)
last_line = stdout.read()
if last_line != old_line:
finish_line = last_line
break
self.logger.info("SSH Log: closing connection")
sftp.close()
client.close()
except Exception, e:
self.logger.error(str(e))
self.logger.error("Failed to delete file on HTTP server: " + str(e))
except:
self.logger.error("Failed to delete file on HTTP server")