In Paramiko, how to pass a list or dict to exec_command
and save results to a list or dict?
I need
sleep
between exec_command.Commands are not executed sequentially, but in the order of 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()
If there are no two problems mentioned above, I have tried map()
, it works fine.
cmd = ['xxx', 'xxx']
def func(cmd):
stdin, stdout, stderr= ssh.exec_command(cmd)
result = stdout.read()
return result
list(map(func, cmd))
My problem is that I need to SSH a remote Linux, replace a string in a file.
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()
I don't want to repeat coding stdin, stdout, stderr = ssh.exec_command()
.