1

我正在尝试检查远程文件是否可写或不使用 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();

但是一旦我执行了这些行,shell就会卡住,我必须关闭shell。请帮忙..

4

1 回答 1

2

假设 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)
于 2011-07-14T10:47:38.503 回答