Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

ubuntu - Execute a command on Remote Machine in Python

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure, there are several ways to do it!

Let's say you've got a Raspberry Pi on a raspberry.lan host and your username is irfan.

subprocess

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.

You can also automate the password/passphrase entering using pexpect.

paramiko

paramiko is a third-party library that adds SSH-protocol support, so it can work like an SSH-client.

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

You can also achieve it using fabric.
Fabric is a deployment tool which executes various commands on remote servers.

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.

It's like typing cd / and ls -l in the remote server, so you'll get the list of directories in your root folder.

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: [email protected]

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

Or you could put a host into the env.hosts variable in your fabfile. Here's how to do it.


Then you'll be prompted for a SSH password:

[[email protected]] run: ls -l
[[email protected]] Login password for 'irfan':

And then the command will be ran successfully.

[[email protected]] out: total 84
[[email protected]] out: drwxr-xr-x   2 root root  4096 Feb  9 05:54 bin
[[email protected]] out: drwxr-xr-x   3 root root  4096 Dec 19 08:19 boot
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...