2011-02-24 93 views
1

我想检查一个远程文件是否可写或使用paramiko。 我当前的代码是如何使用python Paramiko(SSHClient)检查远程文件是否可写?

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

但是一旦我执行这些方针,外壳只是卡住,我不得不关闭外壳。 请帮助..

+0

做一个简单的'ls'工作? – 2011-02-24 09:33:19

+0

yes ls正常工作 – tejzpr 2011-02-24 09:38:26

+0

请写下你的'import',以便我们查看它的文档。 – 2011-02-24 10:31:23

回答

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) 
相关问题