rogueakula
rogueakula

Reputation: 79

Paramiko equivalent of ssh host -t bash

In my Python3 script, I am trying to use Paramiko to ssh into remote devices. I can do that just fine. The issue is that ssh dumps me into a proprietary shell. I need to add -t bash to the .connect command to dump me into a bash shell. Here is what I have that is working:

ssh_tranfer = paramiko.SSHClient()
ssh_transfer.set_missing_host_key_policy(paramiko.AutoAddPolicy)

ssh.connect(hostname=device, port=22, username=username, key_filename=private_key_name)

With this I can connect just fine, but like I said, I am in the proprietary shell and can't pass it commands. I am not even sure that I can do it this way.

I define all the parameters that I am using (ie - device, username, and the private key) prior to the ssh.connect shown above.

If I was to ssh into the device directly from my computer to go straight to the bash shell, I would use:

ssh [email protected] -t bash

I would like to find a way to do this using paramiko.

Thanks for the help!

Upvotes: 1

Views: 658

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202098

The -t + bash in ssh do two things:

  • Starts command bash in "exec" channel (instead of starting "shell" channel, what ssh does by default).
    For that, see Python Paramiko - Run command
  • The -t forces an interactive session, what would be the default for "shell", but is by default disabled for "exec". For that, pass get_pty=True to SSHClient.exec_command.

Obligatory warning: Do not use AutoAddPolicy this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Upvotes: 2

Related Questions