Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 451
Irfan Ghaffar7 Avatar asked Feb 09 '15 14:02

Irfan Ghaffar7


People also ask

How do I run a Python command on a remote computer?

Running commands remotely on another host from your local machinelink. Using the Paramiko module in Python, you can create an SSH connection to another host from within your application, with this connection you can send your commands to the host and retrieve the output.

How do I automate remote desktop connection in Python?

If you need GUI automation inside RDP window, you have to install pywinauto on remote machine and run the script there. This is true for any GUI automation tool because RDP or any other remote client doesn't provide GUI elements info to local machine.


1 Answers

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: [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 ... 
like image 77
Igor Hatarist Avatar answered Sep 21 '22 23:09

Igor Hatarist