Thursday, October 14, 2010

ssh client with python



1. Install paramiko & paracrypto
- paramiko : http://www.lag.net/paramiko/download/paramiko-1.7.6.tar.gz
- paracrypto : http://www.amk.ca/files/python/crypto/pycrypto-2.0.1.tar.gz
NOTE : they all work on a python version >= 2.4




2. sample code for using paramiko
- the following python code checks if sshd is alive at a remote server.

import paramiko
import sys
import socket
import os

addr = sys.argv[1]

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=addr, username="root", password="rootpw")

stdin, stdout, stderr = client.exec_command("ps -ef | grep sshd")

found = False
for line in stdout.read().splitlines():
if line.find("/usr/bin/sshd"):
found = True

if found:
print "sshd is alive!"
else:
print "sshd is dead!"

client.close()

No comments: