How do I start a shell without terminal emulation in Python Paramiko?

后端 未结 1 1339
北恋
北恋 2020-12-20 08:33

Is there a way to start a shell without terminal emulation in Python Paramiko?

  • I have tried using exec_command but I really need an interactive sh
相关标签:
1条回答
  • 2020-12-20 09:16

    Paramiko SSHClient.invoke_shell opens "shell" SSH channel. What is basically only a shorthand for executing user's default shell. Otherwise it does not differ to what SSH "exec" channel (used by SSHClient.exec_command) does.

    Both "shell" and "exec" SSH channels can be started with or without the terminal emulation. It's only that Paramiko SSHClient.invoke_shell method does not offer that option (while SSHClient.exec_command does – via its get_pty parameter).

    There are two alternatives:

    • Use SSHClient.exec_channel to start the shell explicitly, like

      ssh.exec_command("/bin/bash")
      

      On Linux servers, you may even be able to avoid hard-coding the shell path by using the SHELL environment variable:

      ssh.exec_command("$SHELL")
      

      Similar might be done on Windows using %CMDSPEC% (untested).

    • Or re-implement SSHClient.invoke_shell to support execution without the terminal emulation.

      If you look at SSHClient.invoke_shell implementation, it does:

      chan = self._transport.open_session()
      chan.get_pty(term, width, height, width_pixels, height_pixels)
      chan.invoke_shell()
      

      All you need, is to do the same, just remove the Channel.get_pty call:

      chan = ssh.get_transport().open_session()
      chan.invoke_shell()
      

    Though note that there's a reason, why SSHClient.invoke_shell always uses the terminal emulation. The only purpose of SSH "shell" channel is implementing an interactive SSH terminal client (like PuTTY). A terminal client without the terminal emulation makes no sense.

    That you want to use "shell" channel without the terminal emulation indicates, that you are abusing it for purposes for which it was not designed. Think twice, if there's not a better solution to what you are trying to do!

    0 讨论(0)
提交回复
热议问题