Pass Input/Variables to Command/Script Over Ssh Using Python Paramiko

Pass input/variables to command/script over SSH using Python Paramiko

Write the input that your command needs to the stdin:

stdin, stdout, stderr = self.client.exec_command(command)
stdin.write(name + '\n')
stdin.flush()

(You will of course need to propagate the name variable from module to sendCommand, but I assume you know how to do that part).

Pass arguments to a bash script stored locally and needs to be executed on a remote machine using Python Paramiko

Do the same, what you are doing in the bash:

command = "/bin/bash -s {v1} {v2}".format(v1=var1, v2=var2)
stdin, stdout, stderr = c.exec_command(command)
stdin.write(mymodule)
stdin.close()

If you prefer the heredoc syntax, you need to use the single quotes, if you want the argument to be expanded:

command = "/bin/bash -s {v1} {v2} <<'EOF'\n{s}\nEOF".format(v1=var1,v2=var1,s=mymodule)
stdin, stdout, stderr = c.exec_command(command)

The same way as you would have to use the quotes in the bash:

ssh -i key user@IP bash -s "$var1" "$var2" <<'EOF'
echo $1
echo $2
EOF

Though as you have the script in a variable in your Python code, why don't you just modify the script itself? That would be way more straightforward, imo.


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

Execute command/script using different shell in SSH/Paramiko

Your question has nothing to do with Paramiko. Try to paste your command in SSH terminal - It won't work either.


The syntax aaa ; bbb executes the commands one after another. bbb won't be executed until aaa finishes. Similarly, /bin/bash ; echo $shell executes bash and the echo won't be executed until bash finishes, what it never does, hence the hang.

You actually do not want to execute echo after bash - you want to execute echo within bash.

If you want to execute a script/commands within a different shell, you have three options:

  • Specify the shell that the script needs in the script itself using shebang - This the the right way for scripts.

    #!/bin/bash
  • Execute the script/commands using shell command-line:

    /bin/bash script.sh

    or

    /bin/bash -c "command1 ; command2 ; ..."
  • Write the script/command to be executed to a shell input, like I've shown you in your previous question:

    Pass input/variables to bash script over SSH using Python Paramiko

Perform commands over ssh with Python

I will refer you to paramiko

see this question

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

If you are using ssh keys, do:

k = paramiko.RSAKey.from_private_key_file(keyfilename)
# OR k = paramiko.DSSKey.from_private_key_file(keyfilename)

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)

Pass input/variables to command/script over SSH using Python Paramiko

Write the input that your command needs to the stdin:

stdin, stdout, stderr = self.client.exec_command(command)
stdin.write(name + '\n')
stdin.flush()

(You will of course need to propagate the name variable from module to sendCommand, but I assume you know how to do that part).

Get past user/pass prompt in a shell script using PySFTP

after searching for Paramiko rather than pysftp, I think this thread holds the answer.

Running interactive commands in Paramiko



Related Topics



Leave a reply



Submit