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:

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.connect('', username='root', password='superlazy')
stdin, stdout, stderr = ssh.exec_command("tail -20 /home/alert.log")
sys.stdout = open('alert.txt', 'w')
print stdout.readlines()