import sys, os, select, socket
try:
import paramiko
except Exception, e:
raise ImportError, "paramiko SSH library import failed !!!"
class remoteCmdExecuter:
def __init__( self ):
pass
def execCmd( self, machineDetails ):
print "hostname -- " + machineDetails['hostname']
print "username -- " + machineDetails['username']
print "passwd -- " + machineDetails['passwd']
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((machineDetails['hostname'], 22))
pc = paramiko.Transport(sock)
pc.start_client()
if not pc.is_active():
pc.close()
raise Exception, 'client negotiation failed'
pc.auth_password(machineDetails['username'], machineDetails['passwd'])
if not pc.is_authenticated():
print 'Authentication failed'
pc.close()
raise Exception, 'invalid password'
print self.__runCmd(pc, "touch i-was-here.grr")
pc.close()
return True
def __runCmd( self, t, cmd ):
out = ''
print 'Running cmd:', cmd
chan = t.open_session()
chan.setblocking(0)
chan.exec_command(cmd)
if chan.recv_exit_status():
print 'cmd not successful !!!'
chan.close()
return False
chan.close()
return True
if __name__ == "__main__":
aD = remoteCmdExecuter()
machineDetails = { 'hostname': 'cs3.csshyamsundar.com',
'ipaddr': '192.168.0.1', 'username': 'cs3', 'passwd': 'passwd' }
aD.execCmd( machineDetails )