python - How to get bash output over ssh via Paramiko -
i'm trying test file permissions of user prior performing other commands via ssh. have:
ssh = paramiko.sshclient() channel = ssh.get_transport().open_session() # check permissions channel.send("if [ -w %s ]; echo \"true\"; else echo \"false\"; fi\n" % self.dest_path) if (channel.recv(1024) == "false"): exit(priv_err) however, never response remote machine. other .recv() calls work fine, response, i'm thinking there's issue bash script? works fine locally. timeout exception when try receive on ssh channel though.
be careful sending paths and/or running small scripts -- it's easy not quote things correctly. show program running correctly of time, failing handle files spaces, or apostrophes, or unicode characters.
this code runs "test -f (mypath)" remotely, signaling if mypath file or not. consider using more sophisticated library fabric kind of thing -- it's more reliable. (fabric link)
source
import paramiko client = paramiko.sshclient() client.set_missing_host_key_policy(paramiko.autoaddpolicy()) client.connect('localhost') # check permissions # todo: proper quoting handle filenames apostrophes _stdin,stdout,_stderr = client.exec_command( "test -f '{}' && echo isfile".format('/etc/issue') ) isfile = stdout.read().strip() == 'isfile' print 'isfile:', isfile output
isfile: true
Comments
Post a Comment