bYt34byt3
bYt34byt3

Reputation: 11

Cannot execute shell command with redirected multiline input using Python Paramiko

I am using Paramiko to do the standard SSH into a box, run commands, and display the STDOUT to my terminal. Due to sudo rules, I SSH into a machine with my username and run sudo /bin/su -s /bin/bash - <diff user account>. In my script, I am passing the following command into Paramiko but the STDOUT does not show on my screen. I believe this is because the sudo command is opening a new shell and Paramiko is watching the STDOUT on the new shell.

The commands DO run as I have logged onto the box and see the command history. How do I get the STDOUT of the commands I am running to show on my terminal?

import paramiko

def sshCommand(hostname, port, username, command, key_filename='/home/<my username>/.ssh/id_rsa'):
        sshClient = paramiko.SSHClient()
        sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        sshClient.connect(hostname=hostname, port=port, username=username, key_filename=key_filename)
        stdin, stdout, stderr = sshClient.exec_command(command, get_pty=False)
        output = stdout.readlines()
        print(output)

sshCommand('<servername>', 22, '<my username>', """sudo /bin/su -s /bin/bash - <diff username> << \'EOF\'
echo "Hello World"
EOF
""")

Upvotes: 1

Views: 239

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202534

I do not think that OpenSSH SSH server can accept the << 'EOF' shell construct on the "exec" channel.

But this should work:

echo echo "Hello World" | sudo /bin/su -s /bin/bash - <diff username>

Upvotes: 0

Related Questions