Running a python script over ssh with paramiko not working as expected












0















I have a python script on a raspberry that take a picture, when I run it with putty foo.jpg is indeed created.



However when I run it using paramiko foo.jpg is not created, but the script run as expected (it prints 'foo.jpg captured').



class RemoteServer():
def __init__(self, ip, port, username, password):
self.ip = ip
self.port = port
self.username = username
self.password = password

class RemoteHelper():
def __init__(self, paramiko_ssh_object):
self.ssh = paramiko_ssh_object

def waitForExecCommandEnd(self, channel, command):
"""
Block untill the end of a command executed by Paramiko.ssh.exec_command
-channel : (channel) channel stdout returned by Paramiko.ssh.exec_command
-command : (string) command to run
"""
while not channel.exit_status_ready():
print "Waiting for end of {}".format(command)
time.sleep(1)

def runRemoteCommand(self, command):
"""
Run a command on the remote server via ssh and block until it ends
-command : (string) command to run
"""
print "running {}".format(command)
a, stdout, stderr = self.ssh.exec_command(command)
self.waitForExecCommandEnd(stdout.channel, command)
for line in stdout.readlines():
print line
for line in stderr.readlines():
print li

def authentificate(ssh, rpi):
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "Connection a %s:%s user=%s mdp=XXXXXXXXX" % (rpi.ip, rpi.port, rpi.username)
ssh.connect(rpi.ip, port=rpi.port, username=rpi.username, password=rpi.password)

rpi = RemoteServer("192.168.1.20", 22, "pi", "raspberry")
ssh = paramiko.SSHClient()
authentificate(ssh, rpi)
remoteHelper = RemoteHelper(ssh)
remoteHelper.runRemoteCommand("sudo python /home/pi/camera/pictaker.py")


And here is the script on the RPI:



#!/usr/bin/env python
# --*-- encoding: utf-8 --*--

from time import sleep
from picamera import PiCamera

#camera conf
camera = PiCamera()
camera.resolution = (2592, 1944)
camera.vflip = True
camera.framerate = 5

#camera warmpup
print "preparing camera"
camera.start_preview()
sleep(2)

#taking pic
camera.capture('foo.jpg')
print "foo.jpg captured"
camera.close()


would it be due to some unix permissions?



Thanks.










share|improve this question



























    0















    I have a python script on a raspberry that take a picture, when I run it with putty foo.jpg is indeed created.



    However when I run it using paramiko foo.jpg is not created, but the script run as expected (it prints 'foo.jpg captured').



    class RemoteServer():
    def __init__(self, ip, port, username, password):
    self.ip = ip
    self.port = port
    self.username = username
    self.password = password

    class RemoteHelper():
    def __init__(self, paramiko_ssh_object):
    self.ssh = paramiko_ssh_object

    def waitForExecCommandEnd(self, channel, command):
    """
    Block untill the end of a command executed by Paramiko.ssh.exec_command
    -channel : (channel) channel stdout returned by Paramiko.ssh.exec_command
    -command : (string) command to run
    """
    while not channel.exit_status_ready():
    print "Waiting for end of {}".format(command)
    time.sleep(1)

    def runRemoteCommand(self, command):
    """
    Run a command on the remote server via ssh and block until it ends
    -command : (string) command to run
    """
    print "running {}".format(command)
    a, stdout, stderr = self.ssh.exec_command(command)
    self.waitForExecCommandEnd(stdout.channel, command)
    for line in stdout.readlines():
    print line
    for line in stderr.readlines():
    print li

    def authentificate(ssh, rpi):
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print "Connection a %s:%s user=%s mdp=XXXXXXXXX" % (rpi.ip, rpi.port, rpi.username)
    ssh.connect(rpi.ip, port=rpi.port, username=rpi.username, password=rpi.password)

    rpi = RemoteServer("192.168.1.20", 22, "pi", "raspberry")
    ssh = paramiko.SSHClient()
    authentificate(ssh, rpi)
    remoteHelper = RemoteHelper(ssh)
    remoteHelper.runRemoteCommand("sudo python /home/pi/camera/pictaker.py")


    And here is the script on the RPI:



    #!/usr/bin/env python
    # --*-- encoding: utf-8 --*--

    from time import sleep
    from picamera import PiCamera

    #camera conf
    camera = PiCamera()
    camera.resolution = (2592, 1944)
    camera.vflip = True
    camera.framerate = 5

    #camera warmpup
    print "preparing camera"
    camera.start_preview()
    sleep(2)

    #taking pic
    camera.capture('foo.jpg')
    print "foo.jpg captured"
    camera.close()


    would it be due to some unix permissions?



    Thanks.










    share|improve this question

























      0












      0








      0








      I have a python script on a raspberry that take a picture, when I run it with putty foo.jpg is indeed created.



      However when I run it using paramiko foo.jpg is not created, but the script run as expected (it prints 'foo.jpg captured').



      class RemoteServer():
      def __init__(self, ip, port, username, password):
      self.ip = ip
      self.port = port
      self.username = username
      self.password = password

      class RemoteHelper():
      def __init__(self, paramiko_ssh_object):
      self.ssh = paramiko_ssh_object

      def waitForExecCommandEnd(self, channel, command):
      """
      Block untill the end of a command executed by Paramiko.ssh.exec_command
      -channel : (channel) channel stdout returned by Paramiko.ssh.exec_command
      -command : (string) command to run
      """
      while not channel.exit_status_ready():
      print "Waiting for end of {}".format(command)
      time.sleep(1)

      def runRemoteCommand(self, command):
      """
      Run a command on the remote server via ssh and block until it ends
      -command : (string) command to run
      """
      print "running {}".format(command)
      a, stdout, stderr = self.ssh.exec_command(command)
      self.waitForExecCommandEnd(stdout.channel, command)
      for line in stdout.readlines():
      print line
      for line in stderr.readlines():
      print li

      def authentificate(ssh, rpi):
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      print "Connection a %s:%s user=%s mdp=XXXXXXXXX" % (rpi.ip, rpi.port, rpi.username)
      ssh.connect(rpi.ip, port=rpi.port, username=rpi.username, password=rpi.password)

      rpi = RemoteServer("192.168.1.20", 22, "pi", "raspberry")
      ssh = paramiko.SSHClient()
      authentificate(ssh, rpi)
      remoteHelper = RemoteHelper(ssh)
      remoteHelper.runRemoteCommand("sudo python /home/pi/camera/pictaker.py")


      And here is the script on the RPI:



      #!/usr/bin/env python
      # --*-- encoding: utf-8 --*--

      from time import sleep
      from picamera import PiCamera

      #camera conf
      camera = PiCamera()
      camera.resolution = (2592, 1944)
      camera.vflip = True
      camera.framerate = 5

      #camera warmpup
      print "preparing camera"
      camera.start_preview()
      sleep(2)

      #taking pic
      camera.capture('foo.jpg')
      print "foo.jpg captured"
      camera.close()


      would it be due to some unix permissions?



      Thanks.










      share|improve this question














      I have a python script on a raspberry that take a picture, when I run it with putty foo.jpg is indeed created.



      However when I run it using paramiko foo.jpg is not created, but the script run as expected (it prints 'foo.jpg captured').



      class RemoteServer():
      def __init__(self, ip, port, username, password):
      self.ip = ip
      self.port = port
      self.username = username
      self.password = password

      class RemoteHelper():
      def __init__(self, paramiko_ssh_object):
      self.ssh = paramiko_ssh_object

      def waitForExecCommandEnd(self, channel, command):
      """
      Block untill the end of a command executed by Paramiko.ssh.exec_command
      -channel : (channel) channel stdout returned by Paramiko.ssh.exec_command
      -command : (string) command to run
      """
      while not channel.exit_status_ready():
      print "Waiting for end of {}".format(command)
      time.sleep(1)

      def runRemoteCommand(self, command):
      """
      Run a command on the remote server via ssh and block until it ends
      -command : (string) command to run
      """
      print "running {}".format(command)
      a, stdout, stderr = self.ssh.exec_command(command)
      self.waitForExecCommandEnd(stdout.channel, command)
      for line in stdout.readlines():
      print line
      for line in stderr.readlines():
      print li

      def authentificate(ssh, rpi):
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      print "Connection a %s:%s user=%s mdp=XXXXXXXXX" % (rpi.ip, rpi.port, rpi.username)
      ssh.connect(rpi.ip, port=rpi.port, username=rpi.username, password=rpi.password)

      rpi = RemoteServer("192.168.1.20", 22, "pi", "raspberry")
      ssh = paramiko.SSHClient()
      authentificate(ssh, rpi)
      remoteHelper = RemoteHelper(ssh)
      remoteHelper.runRemoteCommand("sudo python /home/pi/camera/pictaker.py")


      And here is the script on the RPI:



      #!/usr/bin/env python
      # --*-- encoding: utf-8 --*--

      from time import sleep
      from picamera import PiCamera

      #camera conf
      camera = PiCamera()
      camera.resolution = (2592, 1944)
      camera.vflip = True
      camera.framerate = 5

      #camera warmpup
      print "preparing camera"
      camera.start_preview()
      sleep(2)

      #taking pic
      camera.capture('foo.jpg')
      print "foo.jpg captured"
      camera.close()


      would it be due to some unix permissions?



      Thanks.







      python ssh raspberry-pi paramiko






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 11:55









      sliders_alphasliders_alpha

      68031537




      68031537
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Try:

          1) print(camera.capture('foo.jpg') to see if it return 0

          2) try to change 'foo.jpg' to '/tmp/foo.jpg', maybe it capture image but save it to same other path and you don't know where

          Edit:

          3) you can try, but it's not so trivial as above, run sudo strace -f -o /tmp/strace.out . Then you will see is there any 'permission denied' or something else






          share|improve this answer


























          • foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

            – sliders_alpha
            Jan 19 at 12:04











          • yout're welcome :) mark as solved and close topic

            – Marcin Fabrykowski
            Jan 19 at 12:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266808%2frunning-a-python-script-over-ssh-with-paramiko-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Try:

          1) print(camera.capture('foo.jpg') to see if it return 0

          2) try to change 'foo.jpg' to '/tmp/foo.jpg', maybe it capture image but save it to same other path and you don't know where

          Edit:

          3) you can try, but it's not so trivial as above, run sudo strace -f -o /tmp/strace.out . Then you will see is there any 'permission denied' or something else






          share|improve this answer


























          • foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

            – sliders_alpha
            Jan 19 at 12:04











          • yout're welcome :) mark as solved and close topic

            – Marcin Fabrykowski
            Jan 19 at 12:06
















          2














          Try:

          1) print(camera.capture('foo.jpg') to see if it return 0

          2) try to change 'foo.jpg' to '/tmp/foo.jpg', maybe it capture image but save it to same other path and you don't know where

          Edit:

          3) you can try, but it's not so trivial as above, run sudo strace -f -o /tmp/strace.out . Then you will see is there any 'permission denied' or something else






          share|improve this answer


























          • foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

            – sliders_alpha
            Jan 19 at 12:04











          • yout're welcome :) mark as solved and close topic

            – Marcin Fabrykowski
            Jan 19 at 12:06














          2












          2








          2







          Try:

          1) print(camera.capture('foo.jpg') to see if it return 0

          2) try to change 'foo.jpg' to '/tmp/foo.jpg', maybe it capture image but save it to same other path and you don't know where

          Edit:

          3) you can try, but it's not so trivial as above, run sudo strace -f -o /tmp/strace.out . Then you will see is there any 'permission denied' or something else






          share|improve this answer















          Try:

          1) print(camera.capture('foo.jpg') to see if it return 0

          2) try to change 'foo.jpg' to '/tmp/foo.jpg', maybe it capture image but save it to same other path and you don't know where

          Edit:

          3) you can try, but it's not so trivial as above, run sudo strace -f -o /tmp/strace.out . Then you will see is there any 'permission denied' or something else







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 12:05

























          answered Jan 19 at 12:00









          Marcin FabrykowskiMarcin Fabrykowski

          55927




          55927













          • foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

            – sliders_alpha
            Jan 19 at 12:04











          • yout're welcome :) mark as solved and close topic

            – Marcin Fabrykowski
            Jan 19 at 12:06



















          • foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

            – sliders_alpha
            Jan 19 at 12:04











          • yout're welcome :) mark as solved and close topic

            – Marcin Fabrykowski
            Jan 19 at 12:06

















          foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

          – sliders_alpha
          Jan 19 at 12:04





          foo.jpg was indeed saved elsewhere, using /tmp/foo.jpg solved the problem. (print(camera.capture('foo.jpg') always display 'None', even when it's working)

          – sliders_alpha
          Jan 19 at 12:04













          yout're welcome :) mark as solved and close topic

          – Marcin Fabrykowski
          Jan 19 at 12:06





          yout're welcome :) mark as solved and close topic

          – Marcin Fabrykowski
          Jan 19 at 12:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266808%2frunning-a-python-script-over-ssh-with-paramiko-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre