Example #1
0
    def _excmd(self, sshcmd):
        """
        return (connected_ok, response_array)
        """
        connected_ok = True
        resp = []
        try:
            conn = Connection(self.hostname)
            conn.connect()
            self.logger.info("ssh connection created.")
            isAuthenticated = conn.authenticateWithPassword(self.user, self.password)
            if not isAuthenticated:
                connected_ok = False
                self.logger.error("ssh failed to authenticatd.")
            else:
                self.logger.info("ssh authenticated.")
                sess = conn.openSession()

                self.logger.info("ssh session created.")
                sess.execCommand(sshcmd)
                self.logger.info("ssh command issued. cmd is %s" % sshcmd)

                stdout = StreamGobbler(sess.getStdout())
                br = BufferedReader(InputStreamReader(stdout))
                while True:
                    line = br.readLine()
                    if line is None:
                        break
                    else:
                        resp.append(line)
                self.logger.warning("ssh command output: " % resp)
        except IOException, ex:
            connected_ok = False
            # print "oops..error,", ex
            self.logger.error("ssh exception: %s" % ex)
Example #2
0
    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()
Example #3
0
    def _excmd(self, sshcmd):
        '''
        return (connected_ok, response_array)
        '''
        connected_ok = True
        resp = []
        try:
            conn = Connection(self.hostname)
            conn.connect()
            self.logger.info('ssh connection created.')
            isAuthenticated = conn.authenticateWithPassword(
                self.user, self.password)
            if not isAuthenticated:
                connected_ok = False
                self.logger.error('ssh failed to authenticatd.')
            else:
                self.logger.info('ssh authenticated.')
                sess = conn.openSession()

                self.logger.info('ssh session created.')
                sess.execCommand(sshcmd)
                self.logger.info('ssh command issued. cmd is %s' % sshcmd)

                stdout = StreamGobbler(sess.getStdout())
                br = BufferedReader(InputStreamReader(stdout))
                while True:
                    line = br.readLine()
                    if line is None:
                        break
                    else:
                        resp.append(line)
                self.logger.warning('ssh command output: ' % resp)
        except IOException, ex:
            connected_ok = False
            #print "oops..error,", ex
            self.logger.error('ssh exception: %s' % ex)
Example #4
0
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()