Python: Log on to a server, run a command, and store the output in a text file.

Do you need to go to a server everytime to see something, like a log file, or to create daily report ( Like me !! ). This is the end of all your miseries. 

1. You need to install paramiko for this:

https://pypi.python.org/pypi/paramiko

2. alert.txt is the name of the file where the output will be saved. This will be create in same folder as the script. 

--------------------------------------------------------------------------------------


import os
import sys
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.123.128', username='root', password='superlazy')
stdin, stdout, stderr = ssh.exec_command("tail -20 /home/alert.log")
sys.stdout = open('alert.txt', 'w')
type(stdin)
print stdout.readlines()
sys.stdout.close()

ssh.close()

Comments