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();

しかし、これらの行を実行するとすぐに、シェルが動かなくなり、シェルを閉じる必要があります。助けてください..

4

1 に答える 1

2

sshがparamikoSSHClientオブジェクトであり、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 に答える