python远程登录并执行命令_在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?

解决方案

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

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: irfan@raspberry.lan

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:

[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

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值