def session(self, isatty=False, new_session=False): return ShellSession( self.popen((), (["-t"] if isatty else ["-T"]), new_session=new_session), self.custom_encoding, isatty, self.connect_timeout, )
def tunnel(self, lport, dport, lhost="localhost", dhost="localhost", connect_timeout=5): r"""Creates an SSH tunnel from the TCP port (``lport``) of the local machine (``lhost``, defaults to ``"localhost"``, but it can be any IP you can ``bind()``) to the remote TCP port (``dport``) of the destination machine (``dhost``, defaults to ``"localhost"``, which means *this remote machine*). The returned :class:`SshTunnel <plumbum.machines.remote.SshTunnel>` object can be used as a *context-manager*. The more conventional use case is the following:: +---------+ +---------+ | Your | | Remote | | Machine | | Machine | +----o----+ +---- ----+ | ^ | | lport dport | | \______SSH TUNNEL____/ (secure) Here, you wish to communicate safely between port ``lport`` of your machine and port ``dport`` of the remote machine. Communication is tunneled over SSH, so the connection is authenticated and encrypted. The more general case is shown below (where ``dport != "localhost"``):: +---------+ +-------------+ +-------------+ | Your | | Remote | | Destination | | Machine | | Machine | | Machine | +----o----+ +---- ----o---+ +---- --------+ | ^ | ^ | | | | lhost:lport | | dhost:dport | | | | \_____SSH TUNNEL_____/ \_____SOCKET____/ (secure) (not secure) Usage:: rem = SshMachine("megazord") with rem.tunnel(1234, 5678): sock = socket.socket() sock.connect(("localhost", 1234)) # sock is now tunneled to megazord:5678 """ ssh_opts = ["-L", "[%s]:%s:[%s]:%s" % (lhost, lport, dhost, dport)] proc = self.popen((), ssh_opts=ssh_opts, new_session=True) return SshTunnel( ShellSession( proc, self.custom_encoding, connect_timeout=self.connect_timeout))
def session(self, isatty = False, term = "vt100", width = 80, height = 24): chan = self._client.get_transport().open_session() if isatty: chan.get_pty(term, width, height) chan.set_combine_stderr() chan.invoke_shell() stdin = chan.makefile('wb', -1) stdout = chan.makefile('rb', -1) stderr = chan.makefile_stderr('rb', -1) proc = ParamikoPopen(["<shell>"], stdin, stdout, stderr, self.encoding) return ShellSession(proc, self.encoding, isatty)
def session(self, isatty = False, term = "vt100", width = 80, height = 24, new_session = False): # new_session is ignored for ParamikoMachine trans = self._client.get_transport() trans.set_keepalive(self._keep_alive) chan = trans.open_session() if isatty: chan.get_pty(term, width, height) chan.set_combine_stderr() chan.invoke_shell() stdin = chan.makefile('wb', -1) stdout = chan.makefile('rb', -1) stderr = chan.makefile_stderr('rb', -1) proc = ParamikoPopen(["<shell>"], stdin, stdout, stderr, self.encoding) return ShellSession(proc, self.encoding, isatty)
def session(self, isatty = False, new_session = False): return ShellSession(self.popen(["/bin/sh"], (["-tt"] if isatty else ["-T"]), new_session = new_session), self.encoding, isatty, self.connect_timeout)
def session(self, new_session = False): """Creates a new :class:`ShellSession <plumbum.session.ShellSession>` object; this invokes ``/bin/sh`` and executes commands on it over stdin/stdout/stderr""" return ShellSession(self["sh"].popen(new_session = new_session))
def tunnel( self, lport, dport, lhost="localhost", dhost="localhost", connect_timeout=5, reverse=False, ): r"""Creates an SSH tunnel from the TCP port (``lport``) of the local machine (``lhost``, defaults to ``"localhost"``, but it can be any IP you can ``bind()``) to the remote TCP port (``dport``) of the destination machine (``dhost``, defaults to ``"localhost"``, which means *this remote machine*). This function also supports Unix sockets, in which case the local socket should be passed in as ``lport`` and the local bind address should be ``None``. The same can be done for a remote socket, by following the same pattern with ``dport`` and ``dhost``. The returned :class:`SshTunnel <plumbum.machines.remote.SshTunnel>` object can be used as a *context-manager*. The more conventional use case is the following:: +---------+ +---------+ | Your | | Remote | | Machine | | Machine | +----o----+ +---- ----+ | ^ | | lport dport | | \______SSH TUNNEL____/ (secure) Here, you wish to communicate safely between port ``lport`` of your machine and port ``dport`` of the remote machine. Communication is tunneled over SSH, so the connection is authenticated and encrypted. The more general case is shown below (where ``dport != "localhost"``):: +---------+ +-------------+ +-------------+ | Your | | Remote | | Destination | | Machine | | Machine | | Machine | +----o----+ +---- ----o---+ +---- --------+ | ^ | ^ | | | | lhost:lport | | dhost:dport | | | | \_____SSH TUNNEL_____/ \_____SOCKET____/ (secure) (not secure) Usage:: rem = SshMachine("megazord") with rem.tunnel(1234, "/var/lib/mysql/mysql.sock", dhost=None): sock = socket.socket() sock.connect(("localhost", 1234)) # sock is now tunneled to the MySQL socket on megazord """ formatted_lhost = "" if lhost is None else f"[{lhost}]:" formatted_dhost = "" if dhost is None else f"[{dhost}]:" ssh_opts = ( [ "-L", f"{formatted_lhost}{lport}:{formatted_dhost}{dport}", ] if not reverse else [ "-R", f"{formatted_dhost}{dport}:{formatted_lhost}{lport}", ] ) proc = self.popen((), ssh_opts=ssh_opts, new_session=True) return SshTunnel( ShellSession( proc, self.custom_encoding, connect_timeout=self.connect_timeout ) )
def session(self, isatty=False): return ShellSession(self.popen((), ["-t"] if isatty else ["-T"]), self.encoding, isatty)