How to check if a remote file is writable using python Paramiko (SSHClient)? -
i trying check if remote file writable or not using paramiko. current code
from paramiko.ssh_exception import sshexception, badhostkeyexception import paramiko import sys 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();
but execute these lines, shell gets stuck , have close shell. please help..
assuming ssh paramiko sshclient object, temp_path path file under test, , connection setup try following:
# prepare command command = 'if [ -w {filename} ]; echo true; else echo false; fi;' # add filename command = command.format(filename=temp_path) # execute command stdin, stdout, stderr = ssh.exec_command(command) # read result stdout , remove trailing newline character result = stdout.readline().rstrip() print(result)
Comments
Post a Comment