[英]How to run a python script remotly and caputre the output
I have a shell script in my server and I want it to run a python script in raspberry pi and get the result to my server's terminal. 我的服务器中有一个Shell脚本,我希望它在raspberry pi中运行python脚本并将结果发送到服务器的终端。
To do it, I tried with expect
package. 为此,我尝试使用
expect
软件包。 This is my shell script. 这是我的shell脚本。
#!/usr/bin/expect -f
spawn ssh pi@192.168.123.123
expect "password:"
send "pass\r"
interact
sudo python pi@192.168.123.123 TemparatureSensor/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4
From this, I can access to the raspberry pi, but not executing the python script. 由此,我可以访问树莓派,但不执行python脚本。
What am I missing here? 我在这里想念什么? How to get this into working?
如何使它起作用?
Thanks in advance. 提前致谢。
You want to do it the other way around. 您想反过来做。 Make a python script that executes a command (or you python script) and write the output to your terminal.
制作一个执行命令的python脚本(或您的python脚本),然后将输出写入终端。
Here's an example of such python script, that in my case executes a command, adjusting the sound of my laptop. 这是此类python脚本的示例,在我的情况下,该命令执行命令,调整笔记本电脑的声音。
#!/usr/bin/python
import paramiko
import sys
def sshConnect():
HOST = "ip"
USER = "user"
KEYF = "/home/pi/.ssh/id_rsa"
ssh = paramiko.SSHClient()
key = paramiko.RSAKey.from_private_key_file(KEYF)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "[*] Connecting..."
ssh.connect(hostname=HOST, username=USER, pkey=key)
print "[+] Connected!"
return ssh
def setVolume(ssh, volume):
command = 'osascript -e "set Volume %s" ' % (volume)
print "Executing %s" % (command)
stdin,stdout,stderr = ssh.exec_command(command)
print stdout.read() #this prints the result in the terminal
errors = stderr.read()
if errors:
print errors
ssh.close()
print "[+] Disconnected"
def main(volume):
setVolume(sshConnect(), volume)
if __name__ == "__main__":
main(sys.argv[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.