我正在尝试使用 paramiko 检查远程文件是否可写。 我当前的代码是
from paramiko.ssh_exception import SSHException, BadHostKeyException
import paramiko
import sys
from optparse import OptionParser
import os
stdin, stdout, stderr = self.__econnection.exec_command('bash');
stdin.write('if [ -w "%s" ];'%(temp_path))
stdin.write("then echo True;");
stdin.write("else echo False;");
stdin.write("fi;");
stdin.flush();
但是一旦我执行这些行,外壳就会卡住,我必须关闭外壳。 请帮忙..
最佳答案
假设 ssh 是您的 paramiko SSHClient 对象,temp_path 是被测试文件的路径,并且连接已设置,请尝试以下操作:
# prepare command
command = 'if [ -w {filename} ]; then echo True; else echo False; fi;'
# add filename
command = command.format(filename=temp_path)
# execute command
stdin, stdout, stderr = ssh.exec_command(command)
# read the result from stdout and remove the trailing newline character
result = stdout.readline().rstrip()
print(result)
关于python - 如何使用 python Paramiko (SSHClient) 检查远程文件是否可写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5102702/