How to execute bunch of commands in one pipe using python? - shell

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

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)?

Related

How execute a pipeline in bash using a python wrapper?

Scenario:
I have a pipeline in a bash script and a list of some process along with their arguments. I want to run a python script after the execution of each process (executable) in the pipeline if the process is in my list.
(I use Python 2.7)
My proposed solution:
Using a python wrapper script. I have replaced all executable in the pipeline with my custom python script which:
1) checks the process if is in the list then FLAG=True
2) execute the process by the original executable using subprocess.Popen(process.command, shell=True).communicate()
3) if FLAG==True then do something.
Problem:
Using the current solution when I run the process using
subprocess.Popen().communicate(), the processes will execute separately
and they cannot get the output of inner process (child process) to the outer process (parent).
For example:
#!/bin/bash
Mean=`P1 $Image1 -M`
P2 "$Image2" $Mean -F
We have not output value of Mean in the second line execution.
Second line will execute like:
subprocess.Popen("P2 $Image2 \nP1 $Image1 -M -F" , shell=True).communicate()
Therefore, it returns an error!
Is there a better way in python to execute process like this?
Please let me know if there is any other suggestion for this scenario (I'm a very beginner in bash).
There's no need to use bash at all.
Assuming modern Python 3.x:
#!/usr/bin/env python
import subprocess
image1 = sys.argv[1]
image2 = sys.argv[2]
p1 = subprocess.run(['P1', image1, '-M'], check=True, capture_output=True)
p2 = subprocess.run(['P2', image2, p1.stdout.strip(), '-F'], check=True, capture_output=True)
print(p2_result.stdout)
See here that we refer to p1.stdout.strip() where we need the mean value in p2's arguments.

Execute commands in Python without closing the CMD shell

I'm trying to execute commands sequentially through Python.
My aim is to do something like this:
command1
calculate something
command2
...
I want the CMD to remain opened after executing 'command1' , since 'command2' is dependent on 'command1'.
I've tried these answers with no results:
This one gives me the error:
ValueError: write to closed file
while executing two communicate commands.
Python Popen - how to execute commands in nested sub shell using python
Execute Commands Sequentially in Python
Thanks in advance!
I think you want this:
import os
os.system("start cmd /k command1 & command2")
unless you want it to quit after execting both of the commands:
import os
os.system("start cmd /c command1 & command2")
If you want to add more commands add & after command 2 and write your commands.
the first example will run both of the commands in the same window, but won't close it.
the second example will run both of the commands in the same window, and will close it afterwards

How to start a shell script within "expect script"?

In this expect script there will be no ssh server connected, I just want to execute a ".sh" file locally, is that possible?
For instance:
#!/bin/expect
command "xxx.sh" # a command which starts a certain shell script
Also, is it possible to execute a expect script within a expect script?
For instance:
#!/bin/expect
command "xxx.exp" # a command which starts a certain expect script
Any help?
If you need to interact with this script, use spawn xxx.sh
If you just need to run it and capture the output, use set output [exec xxx.sh]
Expect is an extension of the Tcl language, so you would be well served to go through the Tcl tutorial.
The command in Expect to run a shell command is spawn.
#!/bin/expect
spawn command arg1 arg2 ...
command can be any program -- a binary executable, a shell script, another expect script, etc. So you can do:
spawn xxx.sh
or:
spawn xxx.exp

converting bash scripts to Python - how to handle heredocs?

I'm learning Python while converting some bash scripts to Python shell scripts. One thing I don't yet understand is how to deal with the heredocs used in these scripts. Here are two examples of how the bash scripts use heredocs:
The most important thing I need to know how to do in Python is this first case where the heredoc is used to provide standard responses to commands so the command can run non-interactively:
sudo command << 'EOF'
prompt_response1
prompt_response2
EOF
Second, tee is used like to this to create a file for which sudo permissions are required:
sudo tee /etc/xdg/autostart/updateNotificationChecker.desktop > /dev/null << 'EOF'
[Desktop Entry]
Name=Update Notification
Exec=bash /usr/local/bin/updateNotification.sh
Terminal=false
Type=Application
NoDisplay=true
EOF
How would I do these things in Python?
Heredoc in Python
Use multiline string (triple-quote string ''' or """). See Strings from tutorial.
Run command
import subprocess
subprocess.Popen(['cat'], stdin=subprocess.PIPE).communicate('''
Hello multiline-string
simliar to heredoc.
''')
sh (previously pbs) is a full-fledged subprocess interface for Python that allows you to call any program as if it were a function:
from sh import ifconfig
print(ifconfig("wlan0"))
Full docs: http://amoffat.github.com/sh
Follow on Github: http://github.com/amoffat/sh
Example of how it can solve the first problem from this question:
from sh import ssh
import os, sys
# open stdout in unbuffered mode
sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0)
aggregated = ""
def ssh_interact(char, stdin):
global aggregated
sys.stdout.write(char.encode())
aggregated += char
if aggregated.endswith("password: "):
stdin.put("correcthorsebatterystaple\n")
p = ssh("10.10.10.100", _out=ssh_interact, _out_bufsize=0, _tty_in=True)
p.wait()
It can handle sudo like this:
with sudo:
print(ls("/root"))
It has a neat feature called STDOUT/ERR callbacks:
sh can use callbacks to process output incrementally. This is done much like redirection: by passing an argument to either the _out or _err (or both) special keyword arguments, except this time, you pass a callable. This callable will be called for each line (or chunk) of data that your command outputs.
Finally, as part of the standard Python tools there is raw_input which writes to standard output and reads from standard input. That will also solve the second problem in this question.

Python Scripting - How can I launch a subprocess and pipe the stdout of that process to the stdout of a Python script

I am writing a Python script that will help me submit homework assignments. What I want to do is pass the login credentials I have stored in a file to a submission script, after which I will hand over to the user for further input. I have come up with the shell command, cat login_credentials.txt /dev/stdin | python3 submit_hw0.py. I am almost finished, I just don't know how to execute the shell command cat login_credentials.txt /dev/stdin | python3 submit_hw0.py, where submit_hw0.py is replaced by an arbitrary Python file I have stored in a variable named submission_script. Also, I have tried to say:
import subprocess
subprocess.call("cat login_credentials.txt /d")
However, python3 just closes with no output.
Any help would be appreciated,
Kind regards, Kabelo Moiloa.
Take a look on the Popen class of subprocess module (link). You can pass stdin and stdout parameters to it. That should solve your problems.

Resources