Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess on remote server

I am using this code for executing command on remote server.

import subprocess
import sys
COMMAND="ls"

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

When I try to execute this script, I get prompt for password. Is there any way I could avoid it, for example, can I enter password in script somehow? Also, password should be encrypted somehow so that people who have access to the script cannot see it.

like image 470
Zvonimir Peran Avatar asked Jul 17 '15 13:07

Zvonimir Peran


People also ask

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

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.

What is the role of subprocess?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

What does subprocess Check_output do?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.


2 Answers

Why make it so complicated? Here's what I suggest:

1) Create a ssh config section in your ~/.ssh/config file:

Host myserver
  HostName 50.50.50.12 (fill in with your server's ip)
  Port xxxx (optional)
  User me (your username for server)

2) If you have generated your ssh keypair do it now (with ssh-keygen). Then upload with:

$ ssh-copy-id myserver

3) Now you can use subprocess with ssh. For example, to capture output, I call:

result = subprocess.check_output(['ssh', 'myserver', 'cat', 'somefile'])

Simple, robust, and the only time a password is needed is when you copy the public key to the server.

BTW, you code will probably work just fine as well using these steps.

like image 173
Scott Avatar answered Oct 22 '22 06:10

Scott


One way is to create a public key, put it on the server, and do ssh -i /path/to/pub/key user@host or use paramiko like this:

import paramiko
import getpass

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

p = getpass.getpass()

ssh.connect('hostname', username='user', password=p)

stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
ssh.close()
like image 4
heinst Avatar answered Oct 22 '22 06:10

heinst