Issue
I am trying to run my local bash
script on remote server without copying it into remote server. It is as simple as following for test purpose. There are more than a few servers where it runs perfectly, but in some server running tcsh
, there is an issue. How do I invoke bash
, if following does not work. Below is dummy test.sh
#!/bin/bash
a=test
echo $a
echo $SHELL
I am using Python Paramiko exec_command
for remote execution as following:
my_script = open("test.sh").read()
stdin, stdout, stderr = ssh.exec_command(my_script, timeout=15)
print(stdout.read().decode())
err = stderr.read().decode()
if err:
print(err)
Given, that connection works and same script works for other servers with bash
default shell.
This is the output that i get:
/bin/tcsh
printing from errors
a=test: Command not found.
a: Undefined variable.
Solution
The #!/bin/bash
is a comment. Sending it to a remote shell as a command has no effect.
You have to execute /bin/bash
on the server and send your script to it:
stdin, stdout, stderr = ssh.exec_command("/bin/bash", timeout=15)
stdin.write(my_script)
Also, you have to exit
the shell at the end of your script, otherwise it will never end.
Related question:
Pass arguments to a bash script stored locally and needs to be executed on a remote machine using Python Paramiko
Answered By - Martin Prikryl