class RemoteHost: """Examples of use: remotehost = RemoteHost('gkollias@isabella', '/home/giorgos/.ssh/id_rsa') #establishes ssh connection """ def __init__(self, address = None, keypath=" "): """An established ssh connection to address. address is of the form user@host:port where the :port part is optional. keypath is the path to the local private key. """ user, host, port = self.parse(address) self.connection = Connection(host, port) self.connection.connect() print "Loging in to ssh server " + host try: if os.path.isfile(keypath): keyfile = File(keypath) self.connection.authenticateWithPublicKey(user, keyfile, None) else: psw = readPassword("UI Password:"******"Authentication Failed" return None self.user = user self.host = host self.keypath = keypath self.port = port #self.connection = connection #TODO:Make env a dictionary self.env = getSSHEnv(self.connection) self.keepAlive = 1 self.startKeepAlive() def parse(self, address): """address (sth of the form user@host:port) is converted to [user, host, port]. """ if address is None: hostport = raw_input("UI Host:") user = raw_input("UI Username:"******"/bin/date" rhost = self class NestedRunnable(lang.Runnable): def run(self): while (rhost.keepAlive == 1): lang.Thread.sleep(interval) sess = rhost.connection.openSession() sess.execCommand(command) # execute command inputreader = BufferedReader(InputStreamReader(sess.getStdout())) inputreader.readLine() sess.close() self.keepAliveThread = lang.Thread(NestedRunnable()) self.keepAliveThread.start() def stopKeepAlive(self): self.keepAlive = 0 try: self.keepAliveThread.interrupt() except: pass def destroy(self): """ Close Connection Sessions and Streams """ self.connection.close() self.stopKeepAlive()