[英]Execute local script on remote server using non-default shell with Python Paramiko
I am trying to run my local bash
script on remote server without copying it into remote server.我正在尝试在远程服务器上运行我的本地
bash
脚本而不将其复制到远程服务器中。 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.它运行完美的服务器不止几台,但在某些运行
tcsh
的服务器中,存在问题。 How do I invoke bash
, if following does not work.如果以下不起作用,我该如何调用
bash
。 Below is dummy test.sh
下面是虚拟
test.sh
#!/bin/bash
a=test
echo $a
echo $SHELL
I am using Python Paramiko exec_command
for remote execution as following:我正在使用 Python Paramiko
exec_command
进行远程执行,如下所示:
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.鉴于此,该连接有效,并且相同的脚本适用于其他具有
bash
默认 shell 的服务器。
This is the output that i get:这是我得到的 output:
/bin/tcsh
printing from errors
a=test: Command not found.
a: Undefined variable.
The #!/bin/bash
is a comment. #!/bin/bash
是注释。 Sending it to a remote shell as a command has no effect.将其作为命令发送到远程 shell 无效。
You have to execute /bin/bash
on the server and send your script to it:您必须在服务器上执行
/bin/bash
并将您的脚本发送给它:
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.此外,您必须在脚本结束时
exit
shell,否则它将永远不会结束。
Related question:相关问题:
Pass arguments to a bash script stored locally and needs to be executed on a remote machine using Python Paramiko将 arguments 传递给本地存储的 bash 脚本,需要使用 Python Paramiko 在远程机器上执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.