Exemplo n.º 1
0
class PlugGeneric:
    def __init__(self, ssh=None, **kwargs):
        if not ssh:
            self._ssh_owner = True
            host = kwargs.get('host',None)
            port = kwargs.get('port',22)
            user = kwargs.get('user', '')
            secret = kwargs.get('secret', '')
            
            self.ssh = SSHChannel(host=host, port=port, user=user, secret=secret)
            self.ssh.connect()
        else:
            self._ssh_owner = False
            self.ssh = ssh

        self.shell = False
        self.channel = None

    def start_shell(self, cols=80, rows=24):
        if not self.shell:
            self.channel = self.ssh.get_shell()
            if cols!=80 or rows!=24:
                self.channel.resize_pty(width=cols, height=rows)

    def close(self):
        if self._ssh_owner:
            self.ssh.close()       
        else:
            self.channel.close() 

    def flush(self):
        if self.shell:
            while self.channel.recv_ready():
                self.channel.recv(256)

    def fileno(self):
        return self.channel.fileno()

    def check_response(self):
        """ Used to process Plug data received from remote side. 
            Must be overriden in child

            @return True if there are some result
            @return False - no data available
        """
        assert False, "Override!"

    def get_result(self):
        """ Used to read result of last command.

            @return data
        """
        assert False, "Override!"

    def put_request(self):
        """ Used to feed command to remote side.

            May raise exception if the request cannot be completed.
        """
        assert False, "Override!"
Exemplo n.º 2
0
 def _connect(self, **kwargs):
     host = kwargs.get('host', None)
     
     port = kwargs.get('port', 22)
     user = kwargs.get('user', None)
     secret = kwargs.get('secret',None)
     try:
         ssh_conn = SSHChannel(host=host, port=port, user=user, secret=secret)
         ssh_conn.connect()
     except Exception as e:
         logger.warning('Unable to start ssh session: %s'%str(e))
         res = {'cmd':kwargs['cmd'], 'res':'error'}
     else:
         conn_id = str(uuid.uuid4())
         self._sessions[conn_id] = [ssh_conn, time.time()]
         logger.info(self.name+'New ssh session was registered, conn_id = %s' % conn_id)
         res = {'cmd':kwargs['cmd'], 'res':'ok', 'conn_id':conn_id}
     
     self._put_answer_in_queue(res)