How to use os.system() to run `> >(tee -a log)`? - python

I run the following command in the terminal.
sh -c "echo out; echo err 2>&1" >>(tee -a stdout.log) 2>>(tee -a stdout.log >&2)
output:
out
err
Using os.system in Python will report an error.
import os
cmd = """
sh -c "echo out; echo err 2>&1" > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)
"""
os.system(cmd)
sh: -c: line 1: syntax error near unexpected token `>'
sh: -c: line 1: `sh -c "echo out" > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'

>(...) is bash-specific syntax. Make that bash -c instead of sh -c.
Also you should enclose the entire command in quotes since -c expects a single argument.
cmd = """
bash -c 'echo out > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'
"""
To test writing to both stdout and stderr like your original example, try like this with curly braces:
cmd = """
bash -c '{ echo out; echo err 2>&1; } > >(tee -a stdout.log) 2> >(tee -a stdout.log >&2)'
"""

Related

broken pipe when executing bash in python

everyone, I encouter the error of Broken pipe when trying to executing bash in python.
Here is my bash file, run.sh
INPUT=`python -c "print 'uid='+'A'*0x4"`
TEST=$INPUT
LEN=$(echo -n "$INPUT" | wc -c)
cp $(which qemu-mipsel-static) ./qemu
echo "$INPUT" | chroot . ./qemu -E CONTENT_LENGTH=$LEN -E CONTENT_TYPE="application/x-www-form-urlencoded" -E REQUEST_METHOD="POST" -E HTTP_COOKIE=$TEST -E REQUEST_URI="/authentication.cgi" -E REMOTE_ADDR="192.168.1.1" htdocs/web/authentication.cgi 2>/dev/null
echo 'run ok'
rm -f ./qemu
Here is how I tried to call execute the bash in python:
bash_file_path =run.sh
op = commands.getstatusoutput("bash %s" % (bash_file_path) )
print op[1]
However, I encouter the error in the line 5 of the run.sh:
run.sh: line 5: echo: write error: Broken pipe
I also tried the subprocess, however, got the same errors:
p = subprocess.Popen(["bash", bash_file_path], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
print p.stdout.readlines()
print p.stderr.readlines()
the results:
[]
[run.sh: line 5: echo: write error: Broken pipe]

How to run the docker commands from python?

I want to run a set of docker commands from python.
I tried creating a script like below and run the script from python using paramiko ssh_client to connect to the machine where the docker is running:
#!/bin/bash
# Get container ID
container_id="$(docker ps | grep hello | awk '{print $1}')"
docker exec -it $container_id sh -c "cd /var/opt/bin/ && echo $1 &&
echo $PWD && ./test.sh -q $1"
But docker exec ... never gets executed.
So I tried to run the below python script below, directly in the machine where the docker is running:
import subprocess
docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)
I get a message: "Usage: docker COMMAND..."
But I get the expected results if I run the command
docker exec 7f34a9c1b78f /bin/bash -c "cd /var/opt/bin/ && ls -a"
directly in the machine
How to run multiple docker commands from the python script? Thanks!
You have a mistake in your call to subprocess.call. subprocess.call expects a command with a series of parameters. You've given it a list of parameter pieces.
This code:
docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)
Runs this:
subprocess.call([
'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c',
'"cd', '/var/opt/bin/', '&&', 'ls', '-a"'
], shell=True)
Instead, I believe you want:
subprocess.call([
'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c',
'"cd /var/opt/bin/ && ls -a"' # Notice how this is only one argument.
], shell=True)
You might need to tweak that second call. I suspect you don't need the quotes ('cd /var/opt/bin/ && ls -a' might work instead of '"cd /var/opt/bin/ && ls -a"'), but I haven't tested it.
Following are a few methods worked:
Remove double quotes:
subprocess.call([
'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c',
'cd /opt/teradata/tdqgm/bin/ && ./support-archive.sh -q 6b171e7a-7071-4975-a3ac-000000000241'
])
If you are not sure of how the command should be split up to pass it as an argument of subprocess method, shlex module:
https://docs.python.org/2.7/library/shlex.html#shlex.split

Execute a shell command in Python

I'm trying to execute the following shell command in Python:
echo abc && echo -ne \\x00 > file && echo efg
I tried doing it by calling os.system but it seems to ignore the -ne flag of echo.
What can I do to run it properly?
import os
os.system('echo abc && echo -ne \\x00 > file && echo efg')

How do i run nikto.pl file from wapiti.py file?

I am working on a project which involves wapiti and nikto web tools. i have managed to produce one report for both these tool with this command
python wapiti.py www.kca.ac.ke ;perl nikto.pl -h www.kca.ac.ke -Display V -F htm -output /root/.wapiti/generated_report/index.html.
But i would like to run a command like
python wapiti.py www.kca.ac.ke
and get both the wapiti and nikto web scan report. How do i achieve this guys?
A shell script would work. Save the following as 'run_wapiti_and_nikto_scans', then run it as:
bash run_wapiti_and_nikto_scans www.my.site.com
Here is the script:
#!/bin/bash
SITE=$1
if [ -n "$SITE" ]; then # -n tests to see if the argument is non empty
echo "Looking to scan $SITE"
echo "Running 'python wapiti.py $SITE'"
python wapiti.py $SITE || echo "Failed to run wapiti!" && exit 1;
echo "Running 'perl nikto.pl -h $SITE -Display V -F htm -output /root/.wapiti/generated_report/index.html'"
perl nikto.pl -h $SITE -Display V -F htm -output /root/.wapiti/generated_report/index.html || echo "Failed to run nikto!" && exit 1;
echo "Done!"
exit 0; # Success
fi
echo "usage: run_wapiti_and_nikto_scans www.my.site.com";
exit 1; # Failure

How to execute any external windows command with multiple parameter using python

I need to execute the command below using python but unable to execute:
cmd="C:\Program Files\Java\jdk1.7.0_51\bin\java.exe" -classpath ./;sqljdbc4.jar InsertTestIncidentData -h 172.20.240.57 -p 1433 -u sa -w Recnex#1 -d ePO_WINEP02 -n 10
import os
cmd='"C:\Program Files\Java\jdk1.7.0_51\bin\java.exe" -classpath ./;sqljdbc4.jar InsertTestIncidentData -h 172.20.240.57 -p 1433 -u sa -w Recnex#1 -d ePO_WINEP02 -n 10'
os.system(cmd)
Since there's a whitespace in Program Files, you should quote the path with another double quote.
cmd='"C:\Program Files\Java\jdk1.7.0_51\bin\java.exe" -classpath ./;sqljdbc4.jar InsertTestIncidentData -h 172.20.240.57 -p 1433 -u sa -w Recnex#1 -d ePO_WINEP02 -n 10'
You can use os.system:
import os
os.system(cmd)
or with subprocess:
import subprocess
ret=subprocess.Popen([cmd])
print "Returning status",ret.wait()
if you are verifying some output from your command:
import subprocess
output=subprocess.chec_call([cmd])

Categories

Resources