使用非默认 shell 和 Python Paramiko 在远程服务器上执行本地脚本
Execute local script on remote server using non-default shell with Python Paramiko
我正在尝试 运行 我在远程服务器上的本地 bash
脚本,而不是将其复制到远程服务器。出于测试目的,它就像以下一样简单。有很多服务器 运行 完美,但在某些服务器 运行 宁 tcsh
中存在问题。如果以下内容不起作用,我该如何调用 bash
。下面是虚拟 test.sh
#!/bin/bash
a=test
echo $a
echo $SHELL
我正在使用 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)
假定该连接有效,并且相同的脚本适用于具有 bash
默认值 shell 的其他服务器。
这是我得到的输出:
/bin/tcsh
printing from errors
a=test: Command not found.
a: Undefined variable.
#!/bin/bash
是评论。将其作为命令发送到远程 shell 无效。
您必须在服务器上执行 /bin/bash
并将您的脚本发送给它:
stdin, stdout, stderr = ssh.exec_command("/bin/bash", timeout=15)
stdin.write(my_script)
此外,你必须exit
在你的脚本末尾shell,否则它永远不会结束。
相关问题:
我正在尝试 运行 我在远程服务器上的本地 bash
脚本,而不是将其复制到远程服务器。出于测试目的,它就像以下一样简单。有很多服务器 运行 完美,但在某些服务器 运行 宁 tcsh
中存在问题。如果以下内容不起作用,我该如何调用 bash
。下面是虚拟 test.sh
#!/bin/bash
a=test
echo $a
echo $SHELL
我正在使用 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)
假定该连接有效,并且相同的脚本适用于具有 bash
默认值 shell 的其他服务器。
这是我得到的输出:
/bin/tcsh
printing from errors
a=test: Command not found.
a: Undefined variable.
#!/bin/bash
是评论。将其作为命令发送到远程 shell 无效。
您必须在服务器上执行 /bin/bash
并将您的脚本发送给它:
stdin, stdout, stderr = ssh.exec_command("/bin/bash", timeout=15)
stdin.write(my_script)
此外,你必须exit
在你的脚本末尾shell,否则它永远不会结束。
相关问题: