我的代码是这样写的-
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
但是检查了由于脚本.sh 1000 将受到影响的文件没有变化。问题不在于 script.sh,因为我可以手动运行它并且它的行为符合预期。
客户端是否无法检测到命令结束?
请您参考如下方法:
我在 youtube 上的视频中找到了我自己查询的答案用于交互式命令。
- 一些命令终止的问题,客户端无法找到它的结尾,因为它不是 unix/系统定义的命令(“系统命令,如 ls -lrt、cd、grep 与客户端完美配合”)<
所以我尝试了如下代码的 channel 来执行位于需要参数的服务器上的 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()
在上面的 runScripts 函数中,我引入了调用 shell 的新 channel ,然后我们可以通过该 channel 发送任何命令或数据,一旦发送命令完成,执行发生并且 channel 关闭。
如果对你们也有用,请采纳。谢谢。
