用 Python 在远程机器上执行命令 [英] Execute a command on Remote Machine in Python

查看:24
本文介绍了用 Python 在远程机器上执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Ubuntu 上用 python 编写程序,在树莓派上执行命令 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 在他的回答中涵盖了这一点.如果您不想使用任何第三方库,您绝对应该这样做.

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('
')

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':

然后命令就会运行成功.

And then the command will be ran successfully.

[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
...

这篇关于用 Python 在远程机器上执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆