在 Paramiko 中,执行列表或字典中的命令并将结果保存到列表或字典中
In Paramiko, execute commands from a list or dict and save the result to a list or dict
在 Paramiko 中,如何将列表或字典传递给 exec_command
并将结果保存到列表或字典?
我需要 sleep
在 exec_command 之间。
命令不是按顺序执行的,而是按照1、2、1的顺序执行的。
stdin, stdout, stderr = ssh.exec_command(d.values()[0])
reuslt1 = stdout.read()
stdin, stdout, stderr = ssh.exec_command(d.values()[1])
reuslt2 = stdout.read()
stdin, stdout, stderr = ssh.exec_command(d.values()[0])
reuslt3 = stdout.read()
如果没有上述两个问题,我试过map()
,没问题
cmd = ['xxx', 'xxx']
def func(cmd):
stdin, stdout, stderr= ssh.exec_command(cmd)
result = stdout.read()
return result
list(map(func, cmd))
我的问题是我需要 SSH 远程 Linux,替换文件中的字符串。
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
command = {
"search" : "grep$img_name ='string' file",
"modify" : "sed -i 's/$img_name = $now/$img_name = $word/g' file",
}
stdin, stdout, stderr = ssh.exec_command(command.values()[0])
before = stdout.read()
sleep(1) ##If I don't add the wait, I will grep the string twice before the modification.
ssh.exec_command(command.values()[1])
sleep(1)
stdin, stdout, stderr = ssh.exec_command(command.values()[0])
after = stdout.read() ##Confirm that my modification was successful
ssh.close()
我不想重复编码stdin, stdout, stderr = ssh.exec_command()
。
我相信您正在寻找这个:Iterating over dictionaries using 'for' loops。
所以在 Python 3:
for key, value in command.items():
stdin, stdout, stderr = ssh.exec_command(value)
results[key] = stdout.read()
关于sleep
: stdout.read()
不仅读取命令输出。作为读取输出的副作用,它会等待命令完成。因为您没有为 sed
调用 stdout.read()
,所以您不需要等待它完成。所以实际上,上面的 for
循环也应该解决这个问题,因为它等待所有命令(包括 sed
)完成。
在 Paramiko 中,如何将列表或字典传递给 exec_command
并将结果保存到列表或字典?
我需要
sleep
在 exec_command 之间。命令不是按顺序执行的,而是按照1、2、1的顺序执行的。
stdin, stdout, stderr = ssh.exec_command(d.values()[0])
reuslt1 = stdout.read()
stdin, stdout, stderr = ssh.exec_command(d.values()[1])
reuslt2 = stdout.read()
stdin, stdout, stderr = ssh.exec_command(d.values()[0])
reuslt3 = stdout.read()
如果没有上述两个问题,我试过map()
,没问题
cmd = ['xxx', 'xxx']
def func(cmd):
stdin, stdout, stderr= ssh.exec_command(cmd)
result = stdout.read()
return result
list(map(func, cmd))
我的问题是我需要 SSH 远程 Linux,替换文件中的字符串。
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
command = {
"search" : "grep$img_name ='string' file",
"modify" : "sed -i 's/$img_name = $now/$img_name = $word/g' file",
}
stdin, stdout, stderr = ssh.exec_command(command.values()[0])
before = stdout.read()
sleep(1) ##If I don't add the wait, I will grep the string twice before the modification.
ssh.exec_command(command.values()[1])
sleep(1)
stdin, stdout, stderr = ssh.exec_command(command.values()[0])
after = stdout.read() ##Confirm that my modification was successful
ssh.close()
我不想重复编码stdin, stdout, stderr = ssh.exec_command()
。
我相信您正在寻找这个:Iterating over dictionaries using 'for' loops。
所以在 Python 3:
for key, value in command.items():
stdin, stdout, stderr = ssh.exec_command(value)
results[key] = stdout.read()
关于sleep
: stdout.read()
不仅读取命令输出。作为读取输出的副作用,它会等待命令完成。因为您没有为 sed
调用 stdout.read()
,所以您不需要等待它完成。所以实际上,上面的 for
循环也应该解决这个问题,因为它等待所有命令(包括 sed
)完成。