How to check if a remote file is writable using python Paramiko (SSHClient)?
I am trying to check if a remote file is writable or not using paramiko. My current code is
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;开发者_JAVA百科");
stdin.flush();
But as soon I execute these lines, the shell just gets stuck and I have to close the shell. Please help..
assuming ssh is your paramiko SSHClient object, temp_path the path to the file under test, and the connection is already setup try the following:
# 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)
精彩评论