示例#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!"