运行 使用 paramiko 的 ssh 上的 python 脚本无法按预期工作
Running a python script over ssh with paramiko not working as expected
我在覆盆子上有一个 python 脚本可以拍照,当我 运行 它用油灰 foo.jpg 确实创建了。
然而,当我 运行 它使用 paramiko foo.jpg 时没有创建,但脚本 运行 符合预期(它打印 '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")
这是 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()
会不会是因为一些unix权限?
谢谢。
尝试:
1) print(camera.capture('foo.jpg') 看看是不是 return 0
2) 尝试将 'foo.jpg' 更改为 '/tmp/foo.jpg',也许它捕获图像但将其保存到相同的其他路径并且你不知道在哪里
编辑:
3)你可以试试,但它不像上面那么简单, 运行 sudo strace -f -o /tmp/strace.out 。然后你会看到有没有 'permission denied' 或其他东西
我在覆盆子上有一个 python 脚本可以拍照,当我 运行 它用油灰 foo.jpg 确实创建了。
然而,当我 运行 它使用 paramiko foo.jpg 时没有创建,但脚本 运行 符合预期(它打印 '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")
这是 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()
会不会是因为一些unix权限?
谢谢。
尝试:
1) print(camera.capture('foo.jpg') 看看是不是 return 0
2) 尝试将 'foo.jpg' 更改为 '/tmp/foo.jpg',也许它捕获图像但将其保存到相同的其他路径并且你不知道在哪里
编辑:
3)你可以试试,但它不像上面那么简单, 运行 sudo strace -f -o /tmp/strace.out 。然后你会看到有没有 'permission denied' 或其他东西