3

I'm trying to do something like this:

from subprocess import Popen
p = Popen(["vagrant", "ssh", "vmname", "-c", '"pvcreate -ff /dev/sdb"'])

But it requires user input. Also, that didn't work anyway. They give the error: bash: pvcreate -ff /dev/sdb: command not found, because it's looking for a program pvcreate -ff /dev/sdb, instead of pvcreate with arguments. I also tried this first:

p = Popen(["vagrant", "ssh", "vmname", "-c", "pvcreate", "-ff", "/dev/sdb"])

And have resorted to using this:

os.system("vagrant ssh vmname -c 'pvcreate -ff /dev/sdb'")

However I need to say yes when it prompts me. I've already tried these options as well:

os.system("yes | vagrant ssh vmname -c 'pvcreate -ff /dev/sdb'")
os.system("echo y | vagrant ssh vmname -c 'pvcreate -ff /dev/sdb'")

Is it possible to respond to a prompt using os.system?

rofls
  • 4,993
  • 3
  • 27
  • 37
  • Possible duplicate of [Give response yes/no in python when a command is executed os.system() in python linux](http://stackoverflow.com/questions/28662107/give-response-yes-no-in-python-when-a-command-is-executed-os-system-in-python) – zondo Jun 21 '16 at 00:56
  • I feel like neither of you read the question. What am I doing wrong with `Popen`? I get this error `bash: pvcreate -ff /dev/sdb: No such file or directory`. Also not sure why I'm getting downvoted, I tried numerous solutions before asking and detailed them in my question. – rofls Jun 21 '16 at 01:07
  • The questions marked as duplicates do not help me, I have tried using `Popen` that way. @JoachimPileborg – rofls Jun 21 '16 at 01:11
  • You put two questions in one. The first is how to respond to a prompt using `os.system()`; the second is asking why the code doesn't work. I admit that I did not read the question completely. I skimmed the question, took a look at the end where the question was, and assumed that the middle was like the title and the end. – zondo Jun 21 '16 at 02:03
  • Note: instead of `os.system` you can always use `subprocess.call("vagrant ssh .... " , shell=True)`. Also, if you have trouble lexing commands use `shlex`: `subprocess.Popen(shlex.split("vagrant ssh vmname -c 'pvcreate -ff /dev/sdb'"))` this should perform the proper splitting for you. – Bakuriu Jun 22 '16 at 10:34
  • The problem with your code is that you double-quoted the command: `'""'`. I believe the actual real error message you got was `"pvcreate -ff /dev/sdb: No such file or directory` which included the double-quotes `"`. When using the list-form you should not quote anything, because the values are not interpreted by the shell at all. – Bakuriu Jun 22 '16 at 11:12
  • Thank you @Bakuriu. Honestly a big part of the problem was that I needed `"echo y | pvcreate -ff /dev/sdb"` or `"yes | pvcreate -ff /dev/sdb"`. I guess I was really asking "why doesn't my code work?" so I'll try to narrow it down more in the future :) – rofls Jun 22 '16 at 18:33
  • the `yes` wasn't being fed to the remote server – rofls Jun 22 '16 at 18:34

1 Answers1

4

I'd suggest using the list form of invocation.

import subprocess
command = ["vagrant", "ssh", "vmname", "-c", "pvcreate -ff /db/sdb"]
output,error  = subprocess.Popen(
                command, universal_newlines=True,
                stdout=subprocess.PIPE,  
                stderr=subprocess.PIPE).communicate()

This represents the set of parameters that are going to be passed and eliminates the need to mess around with shell quoting.

Jay
  • 123
  • 1
  • 1
  • 5