Execute shell script on cygwin from Python - cygwin

I want to execute a shell script, on cygwin from Python. The shell script is creating a file as an output.
I tried
import os
import subprocess
os.chdir(r"C:\\cygwin64\\bin\\ ")
cmd = ["bash", "-c", 'cd /<path for the script>; ./test.sh']
subprocess.call(cmd)

This works:
import os, subprocess
os.chdir(r"C:\cygwin64\bin")
cmd = ["bash", "-c", "cd $HOME; pwd; exit"]
ret = subprocess.call(cmd)

Related

Getting stdout of nested shell command/script execution in Python

I am running a ansible playbook through python program
cmd = 'ansible-playbook -i inventory primary.yml'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, encoding='utf-8')
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if stdout:
print(output.strip())
rc = process.poll()
return rc
This playbook internally calls a shell script like below
- name: call the install script
shell: sudo ./myscript.sh
currently, I am getting the streaming stdout of the ansible-playbook command but I cannot see anything when this myscript.sh get executed.
I have to only wait blindly while the script finished its execution without any stdout. How can see the output of all the child process triggered from main shell command through python.

Python subprocess.run to execute bash commands via ssh

I am currently creating a function that will run at least 2 bash commands on a remote system via subprocess.run module because I need to capture the return code as well.
The example bash commands is ssh username#ipaddress 'ls && exit'
This will basically print out the contents of the remote home directory and exit the terminal created.
This works when using os.system("ssh username#ipaddress 'ls && exit'") but will not capture the return code.
In subprocess.run
### bashCommand is "ssh username#ipaddress 'ls && exit'"
bashCommand = ['ssh', f"username#ipaddress", "'ls && exit'"]
output = subprocess.run(bashCommand)
However when the subprocess.run command was run,
the terminal says that bash: ls && exit: command not found
the return code is 127 (from output.returncode)

How to execute CURL API command from python

Here is the CURL API I have and I want to execute through python but I'm getting error " FileNotFoundError: [Errno 2] No such file or directory: 'curl' " but I'm using OS library in python.
import os
os.system(curl -H "public-api-token: 2dd81854fca7bcbd657bd06a99a46l9s" -X PUT -d "dataMapsGoogle=google.com" https://api.googlemaps/v1/data/url')
How to execute the CURL command through python? I tried with os.system() but if there is any better answer then it would be helpful.
Use 'Subprocess' module:
import subprocess
cmd = ['curl', '-H', "public-api-token:....", ...]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()

How do I execute the following command using subprocess?

I want to execute the following command using subprocess:
sudo sh -c "echo nameserver 1.1.1.1 > /etc/resolv.conf"
In the shell it does work well.
This is what I did:
update_resolv_conf_cmd = (["sudo", "sh", "-c", '"echo nameserver 1.1.1.1 > /etc/resolv.conf"'])
subprocess.Popen(update_resolv_conf_cmd, stdout=subprocess.PIPE, shell=True)
However, this does not work.
I think this may work out
import subprocess
subprocess.call("sudo sh -c 'echo nameserver 1.1.1.1 > /etc/resolv.conf'", shell=True)

Should the args of subprocess.run() be expanded or not?

With python 3.7.1, both centos7 and chromeos, the following code errors out
from subprocess import run
run('git diff --quiet #{u} #{0}'.split(), shell=True)
but
run('git diff --quiet #{u} #{0}', shell=True)
works.
There are other occasions where split and not-split both works. When do they differ? Is one be more preferable than the other?

Resources