无法使用 paramiko ssh 客户端执行带有参数的远程放置的 ssh 脚本

Not able to execute remotely placed ssh script with parameter using paramiko ssh client

我的代码是这样读的-

import paramiko
import time
import sys

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

def runScripts():
    parameter1 = 1000
    client.connect('mysite.com', username='user', password='pass')
    print ("Logged in into Site server")
    stdin, stdout, stderr = client.exec_command("cd parent_folder/Sub_folder/script_folder; ./script1.sh %s" % parameter1)
    print ("Script executed perfectly")
    client.close()

runScripts()

我在控制台上得到如下输出-

Logged in into Site server
Script executed perfectly 

但是检查了由于script.sh 1000而将受到影响的文件没有变化。问题不在于 script.sh,因为我可以手动 运行 并且它的行为符合预期。

客户端是否无法检测到命令结束?

我在 youtube 的视频中找到了对交互式命令的查询的答案。

  1. 一些命令终止的问题,客户端无法找到它的结尾,因为它不是 unix/system 定义的命令 ("system commands like ls -lrt, cd, grep works perfectly fine with client")
  2. 所以我尝试了如下代码的通道来执行位于需要参数-

    的服务器上的shell脚本

    def runScripts():

    parameter1 = 1000
    client.connect('mysite.com', username='user', password='pass')
    channel = client.invoke_shell()
    print ("Logged in into Site server")
    channel.send("cd parent_folder/Sub_folder/script_folder; ./script1.sh %s" % parameter1)
    print ("Script executed perfectly")
    client.close()
    

    runScripts()

  3. 在上面的 runScripts 函数中,我引入了调用 shell 的新通道,然后一旦发送命令完成执行并关闭通道,我们就可以通过该通道发送任何命令或数据。

回答对你们有用就采纳too.Thank你们