在Python的远程计算机上执行命令
我正在Ubuntu上用python编写程序,以在RaspberryPi上执行命令ls -l
,并连接到网络.
I am writing a program in python on Ubuntu, to execute a command ls -l
on RaspberryPi, connect with Network.
有人可以指导我该怎么做吗?
Can anybody guide me on how do I do that?
当然,有几种方法可以做到!
Sure, there are several ways to do it!
假设您在raspberry.lan
主机上安装了Raspberry Pi,并且用户名是irfan
.
Let's say you've got a Raspberry Pi on a raspberry.lan
host and your username is irfan
.
这是运行命令的默认Python库.
您可以使其运行ssh
并在远程服务器上执行所需的任何操作.
It's the default Python library that runs commands.
You can make it run ssh
and do whatever you need on a remote server.
从头开始已在他的答案中覆盖.如果您不想使用任何第三方库,则绝对应该这样做.
scrat has it covered in his answer. You definitely should do this if you don't want to use any third-party libraries.
您还可以使用 pexpect
自动输入密码/密码.
You can also automate the password/passphrase entering using pexpect
.
paramiko
是添加了SSH协议支持的第三方库,因此它可以正常工作就像SSH客户端一样.
paramiko
is a third-party library that adds SSH-protocol support, so it can work like an SSH-client.
将连接到服务器,执行并获取ls -l
命令结果的示例代码如下所示:
The example code that would connect to the server, execute and grab the results of the ls -l
command would look like that:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('raspberry.lan', username='irfan', password='my_strong_password')
stdin, stdout, stderr = client.exec_command('ls -l')
for line in stdout:
print line.strip('\n')
client.close()
织物
您还可以使用 fabric
来实现.
Fabric是一种部署工具,可以在远程服务器上执行各种命令.
fabric
You can also achieve it using fabric
.
Fabric is a deployment tool which executes various commands on remote servers.
它通常用于在远程服务器上运行内容,因此您可以使用单个命令轻松地将Web应用程序的最新版本,重新启动Web服务器等等.实际上,您可以在多个服务器上运行相同的命令,太棒了!
It's often used to run stuff on a remote server, so you could easily put your latest version of the web application, restart a web-server and whatnot with a single command. Actually, you can run the same command on multiple servers, which is awesome!
尽管它是作为部署和远程管理工具而制成的,但是您仍然可以使用它来执行基本命令.
Though it was made as a deploying and remote management tool, you still can use it to execute basic commands.
# fabfile.py
from fabric.api import *
def list_files():
with cd('/'): # change the directory to '/'
result = run('ls -l') # run a 'ls -l' command
# you can do something with the result here,
# though it will still be displayed in fabric itself.
这就像在远程服务器中键入cd /
和ls -l
一样,因此您将在根文件夹中获得目录列表.
It's like typing cd /
and ls -l
in the remote server, so you'll get the list of directories in your root folder.
然后在shell中运行:
Then run in the shell:
fab list_files
它将提示输入服务器地址:
It will prompt for an server address:
No hosts found. Please specify (single) host string for connection: irfan@raspberry.lan
快速注释:您还可以直接在fab
命令中分配用户名和主机:
A quick note: You can also assign a username and a host right in a fab
command:
fab list_files -U irfan -H raspberry.lan
或者您可以将主机放入fabfile中的env.hosts
变量. 此处是操作方法.
Or you could put a host into the env.hosts
variable in your fabfile. Here's how to do it.
然后将提示您输入SSH密码:
Then you'll be prompted for a SSH password:
[irfan@raspberry.lan] run: ls -l
[irfan@raspberry.lan] Login password for 'irfan':
然后命令将成功运行.
[irfan@raspberry.lan] out: total 84
[irfan@raspberry.lan] out: drwxr-xr-x 2 root root 4096 Feb 9 05:54 bin
[irfan@raspberry.lan] out: drwxr-xr-x 3 root root 4096 Dec 19 08:19 boot
...