0

I have issue about executing commands in python. Problem is: In our company we have bought commercial software that can be used either GUI or Command line interface. I have been assigned a task that automize it as possible as. First I thought about using CLI instead of GUI. But then i have encountered a problem about executing multiple commands. Now, I want to execute CLI version of that soft with arguments and continue executing commands in its menu(I dont mean execute script with args again.I want , once initial commands executed , it will open menu and i want to execute soft's commands inside Soft's menu at background). Then redirect output to variable. I know, I must use subprocess with PIPE , but I didn't manage it.

import subprocess
proc=subprocess.Popen('./Goldbackup -s -I -U', shell=True, stdout=subprocess.PIPE)
output=proc.communicate()[0]
proc_2 = subprocess.Popen('yes\r\n/dir/blabla/\r\nyes', shell=True, stdout=subprocess.PIPE) 
# This one i want to execute inside first subprocess
Abdulla
  • 39
  • 1
  • 7

1 Answers1

0

Set stdin=PIPE if you want to pass commands to a subprocess via its stdin:

#!/usr/bin/env python
from subprocess import Popen, PIPE

proc = Popen('./Goldbackup -s -I -U'.split(), stdin=PIPE, stdout=PIPE,
             universal_newlines=True)
output = proc.communicate('yes\n/dir/blabla/\nyes')[0]

See Python - How do I pass a string into subprocess.Popen (using the stdin argument)?

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670