---
title: "Subprocess on remote server"  
description: "Subprocess on remote server"  
author: "Anonymous User"  
published: 2015-11-19  
updated: 2015-11-19  
canonical: https://www.mindstick.com/forum/33612/subprocess-on-remote-server  
category: "python"  
tags: ["python"]  
reading_time: 1 minute  

---

# Subprocess on remote server

I am using this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) for executing [command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) on [remote](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) server.\

```
import subprocessimport sysCOMMAND="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" % errorelse:    print result
```

When I try to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) this [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line), I get prompt for [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net). Is there any way I could [avoid](https://yourviews.mindstick.com/story/1518/tips-to-avoid-dengue-at-home) it, for example, can I enter password in script somehow? Also, password should be encrypted somehow so that [people](https://www.mindstick.com/news/2295/more-people-need-to-monitor-this-crucial-metric-for-heart-health) who have [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) to the script cannot see it.

## Replies

### Reply by Anonymous User

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 paramikoimport getpassssh = 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()
```


---

Original Source: https://www.mindstick.com/forum/33612/subprocess-on-remote-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
