35

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?

Irfan Ghaffar7
  • 1,143
  • 4
  • 11
  • 30

4 Answers4

70

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
...
Community
  • 1
  • 1
Igor Hatarist
  • 5,234
  • 2
  • 32
  • 45
  • 2
    @IrfanGhaffar7 you can install third-party Python library using `pip` or `easy_install`. So it would be `pip install fabric`. Check the documentation (I linked to both fabric and paramiko docs), it has quickstarts and tutorials! – Igor Hatarist Feb 09 '15 at 14:31
  • my remote machine hostname is `Pi@192.168.2.34` and password is `raspberrypi`. How do I mention these information on Fabric – Fahadkalis Feb 09 '15 at 14:40
  • 1
    @Fahadkalis when it prompts you for a hostname, you can put `Pi@192.168.2.34` into it. Also, you can run a `fab` command like that: `fab list_files -u Pi -H 192.168.2.34` – Igor Hatarist Feb 09 '15 at 14:44
  • @Fahadkalis you can also assign an `env.hosts` variable in a fabfile. Here's a tutorial: http://docs.fabfile.org/en/1.10/tutorial.html#defining-connections-beforehand – Igor Hatarist Feb 09 '15 at 14:45
  • @IgorHatarist When run above `fabfile.py` program, it just show me a black screen – Irfan Ghaffar7 Feb 09 '15 at 14:52
  • @IrfanGhaffar7 You don't run the actual `fabfile.py`, you should run the fabric itself, while being in the same directory where the `fabfile` is located. It's the `fab list_files` you should run, not `python fabfile.py` :) – Igor Hatarist Feb 09 '15 at 14:53
  • U mean I have to save above program(fabfile.py) in `/home/tansen/Documents/python2`, then i went there and run command `fab list_files`? – Irfan Ghaffar7 Feb 09 '15 at 15:00
  • @IrfanGhaffar7 yup, just like that. If you're uncomfortable with that, you can still use the `paramiko` library, I have updated my answer so it'd be easier to dive into `paramiko`. – Igor Hatarist Feb 09 '15 at 15:01
  • 2
    @IrfanGhaffar7 and with `paramiko` you can run the actual python script itself. – Igor Hatarist Feb 09 '15 at 15:01
  • @IgorHatarist I am getting this error `Traceback (most recent call last): File "Network2_02Feb.py", line 5, in client.connect('raspberry.lan', username='pi@192.168.2.40', password='raspberry') File "build/bdist.linux-i686/egg/paramiko/client.py", line 237, in connect socket.gaierror: [Errno -2] Name or service not known` and I also didn't understand what is this `raspberry.lan` – Irfan Ghaffar7 Feb 09 '15 at 15:07
  • 2
    @IrfanGhaffar7 if you use paramiko, you should connect like that then: `client.connect('192.168.2.40', username='pi', password='raspberry')` – Igor Hatarist Feb 09 '15 at 15:24
  • @IgorHatarist Thank you very much... is that possible in future that I can post my question to you directly? – Irfan Ghaffar7 Feb 09 '15 at 15:30
  • @IrfanGhaffar7 You're welcome! Sure, you can mail me at igor@hatari.st, just have in mind that there are thousands of users on StackOverflow who are able to provide you with an answer! I could reply relatively slow :) – Igor Hatarist Feb 09 '15 at 15:31
  • @IgorHatarist do you think that the Paramico can be used with AWS EC2? (since It uses a .pem file) is it possible? or what is the best library to do that if not paramiko? – Dami Apr 18 '20 at 06:58
  • @Dami sorry, I've no idea. Quick google for the "paramiko pem" has yielded me this gist: https://gist.github.com/batok/2352501, check it out – Igor Hatarist Apr 18 '20 at 18:22
30

Simple example from here:

import subprocess
import sys

HOST="www.example.org"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                       shell=False,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

It does exactly what you want: connects over ssh, executes command, returns output. No third party library needed.

sashab
  • 1,534
  • 2
  • 19
  • 36
  • when I use this COMMAND= "ls -l" , its result is in one line, means non- readable but it is working – Irfan Ghaffar7 Feb 09 '15 at 15:57
  • 2
    @IrfanGhaffar7 it's because the `result` is a list, not a string. You can do `print ''.join(result)` instead to make it look readable. – Igor Hatarist Feb 09 '15 at 16:09
  • What if we want to run a command like `tailf /some/file/name`. In that case it is starting the process and then killing it or it goes into waiting state. But i have some more commands to enter while tailf is still running. – manoj prashant k Oct 04 '16 at 07:22
  • @manojprashantk - you could try something like `other command &; tail -f /some/file` – Adam Van Prooyen Feb 13 '19 at 18:00
2

You may use below method with linux/ Unix 's built in ssh command.

   import os
   os.system('ssh username@ip  bash < local_script.sh >> /local/path/output.txt 2>&1')
   os.system('ssh username@ip  python < local_program.py >> /local/path/output.txt 2>&1')
1

Paramiko module can be used to run multiple commands by invoking shell. Here I created class to invoke ssh shell

class ShellHandler:

def __init__(self, host, user, psw):
    logger.debug("Initialising instance of ShellHandler host:{0}".format(host))
    try:
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(host, username=user, password=psw, port=22)
        self.channel = self.ssh.invoke_shell()
    except:
        logger.error("Error Creating ssh connection to {0}".format(host))
        logger.error("Exiting ShellHandler")
        return
    self.psw=psw
    self.stdin = self.channel.makefile('wb')
    self.stdout = self.channel.makefile('r')
    self.host=host
    time.sleep(2)

    while not self.channel.recv_ready():
        time.sleep(2)
    self.initialprompt=""
    while self.channel.recv_ready():

        rl, wl, xl = select.select([ self.stdout.channel ], [ ], [ ], 0.0)
        if len(rl) > 0:
            tmp = self.stdout.channel.recv(24)
            self.initialprompt=self.initialprompt+str(tmp.decode())



def __del__(self):
    self.ssh.close()
    logger.info("closed connection to {0}".format(self.host))

def execute(self, cmd):
    cmd = cmd.strip('\n')
    self.stdin.write(cmd + '\n')
    #self.stdin.write(self.psw +'\n')
    self.stdin.flush()
    time.sleep(1)
    while not self.stdout.channel.recv_ready():
        time.sleep(2)
        logger.debug("Waiting for recv_ready")

    output=""
    while self.channel.recv_ready():
        rl, wl, xl = select.select([ self.stdout.channel ], [ ], [ ], 0.0)
        if len(rl) > 0:
            tmp = self.stdout.channel.recv(24)
            output=output+str(tmp.decode())
    return output

If creating different shell each time does not matter to you then you can use method as below.

def run_cmd(self,cmd):
    try:
        cmd=cmd+'\n'
        #self.ssh.settimeout(60)
        stdin,stdout,stderr=self.ssh.exec_command(cmd)
        while not stdout.channel.eof_received:
           time.sleep(3)
           logger.debug("Waiting for eof_received")
        out=""
        while stdout.channel.recv_ready():
            err=stderr.read()
            if err:
                print("Error: ",my_hostname, str(err))
                return False 

            out=out+stdout.read()
        if out:
               return out 

    except:
        error=sys.exc_info()
        logger.error(error)
        return False