简体   繁体   English

使用非默认 shell 和 Python Paramiko 在远程服务器上执行本地脚本

[英]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.

相关问题 在远程机器上使用 Paramiko 通过 sshClient() 执行本地 python 脚本 - execute local python script over sshClient() with Paramiko in remote machine 使用 Python 在远程服务器上执行的脚本 Paramiko 无法读取/访问本地文件 - Script executed on remote server using Python Paramiko cannot read/access local files 使用Python和paramiko以root身份执行远程命令 - Execute remote commands as root using Python and paramiko 在ssh上执行受限的远程脚本,通过python paramiko与本地参数连接 - Execute restricted remote script on ssh connect with local arguments via python paramiko 在 Python Paramiko 中的 SSH 服务器上的辅助 shell/命令中执行(子)命令 - Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko 非默认Shell命令的子进程 - subprocess for non-default shell command Paramiko:尝试在远程服务器上执行 python 时抛出错误 - Paramiko : Throwing error when trying to execute python on remote server 无法使用 Python Paramiko 执行重定向多行输入的 shell 命令 - Cannot execute shell command with redirected multiline input using Python Paramiko 在python中使用类变量作为非默认参数 - Using class variable as non-default argument in python 使用python打印(非默认联网打印机) - Using python to print (non-default networked printer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM