Python fabric cant be executed from python using subproccess - python

In python I am trying to execute a fabfile and ,I get the below error from the subproccess output. I installed fabric using easy install. If I run the code from the command line it works. From python no go. I assume there is an issue with how I am using the Popen command?
/bin/sh: 1: fab: not found
Below is how I start fabric from python:
cmd = """fab -H 111.111.111.111 aws_bootstrap initial_chef_run:aws_server.json,aws-test,development -w """
os.chdir(fab_path) #change to the dir where the fabfile is located
p = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
PS I added the below to the Popen but get the below error:
executable="/bin/bash"
/bin/bash: fab: command not found
From the command line I get the below which means the terminal can find fab.
fab
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.
Aborting.

Try to use the whole path for fab. To get the path to fab on your system, you can use which fab.
However, I can't think of any reason why calling fab from python might be better than using the execute function of fab:
from fabric.tasks import execute
import my_fabfile
r = execute(my_fabfile.aws_bootstrap, hosts=["root#%s" % '111.111.111.111])
Return value r will contain a dict with the hosts as key(s).

Related

Unable to create process using '/usr/bin/env

I am trying to use Pidcat for logging. I downloaded the Pidcat.py and pasted it in the following directory:
C:\Python\Scripts
Added this path in the Environment Variables as well. But when I try to log in using the following command:
pidcat -s deviceId
I get the following error:
Unable to create process using '/usr/bin/env -S python -u "C:\Python\Scripts\pidcat.py" -s deviceId'
I used CMD to run a python file getofflinelog.py.
The command is:
python getofflinelog.py -m txt -d camlog -o offline.log
I got a similar error Unable to create process using '/usr/bin/env...., and I solved it by changing the command to:
C:\Users\liutongjun\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe getofflinelog.py -m txt -d camlog -o offline.log
I also found that if I added this path to the environment variables, the first short command would work.
I'm not very clear why it works, I do not install Python in this path and there are only a series of exe programs in this folder. Just for reference.

How to run bash commands from Python preferably with the os library?

I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:
import os
os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')
os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')
os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')
I am running the Python script using the following in the terminal:
python3 test.py
The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:
sh: 1: source: not found
The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .
Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that):
https://stackoverflow.com/a/62355400/11535508
source is a bash built-in command, not an executable.
Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').
The second option is to write your commands into a separate bash script and call it from python:
os.system('bash script.sh')

uWSGI + Python subprocess does not execute shell command

uwsgi + Python subprocess
Hello everyone,
I am trying to run a simple command in shell using Python subprocess module, everything works fine till I put
uwsgi on the top. I also use flask as web framework.
Here is very simplified part of the code
if request.method == 'POST':
testquery = subprocess.run( "ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
whoisresults=whoisquery.stdout
print(whoisresults)
I was getting following error : /bin/sh: 1: ifconfig: not found
I replaced "ifconfig" with the full path where Python virtual evn runs.
testquery = subprocess.run( "/home/net/netools/netoolsenv/bin ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
whoisresults=whoisquery.stdout
print(whoisresults)
But it still does not work, just error is different - /bin/sh: 1: /home/net/netools/netoolsenv/bin: Permission denied
Can anyone please advice in what direction should I dig? I am beginner here.
I was getting following error : /bin/sh: 1: ifconfig: not found
uWSGI daemon usually run as another user, and this user has no PATH set. Use full absolute path to ifconfig.
To find full path to your tools, use which, e.g. run this command at your terminal:
$ which ifconfig
/usr/bin/ifconfig
and use that full path to ifconfig in your Python script.
testquery = subprocess.run( "/usr/bin/ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )

Python - Executing shell command does not work on Linux

I like to run a shell command from Python on my Linux Mint system.
Specifically the command runs all Bleachbit cleaners and works perfectly
fine when run maually.
Yet, trying to run the same command via the subprocess.call module
always results in an exception raised.
I just can not see why it should not work.
The command does not require sudo rights, so not requiring
right not given.
I also have firefox/browsers closed when executing the python command.
Anybody, any suggestions how to fix this issue?
My code:
try:
subprocess.call('bleachbit -c firefox.*')
except:
print "Error."
subprocess module does not run the shell by default therefore the shell wildcards (globbing patterns) such as * are not expanded. You could use glob to expand it manually:
#!/usr/bin/env python
import glob
import subprocess
pattern = 'firefox.*'
files = glob.glob(pattern) or [pattern]
subprocess.check_call(["bleachbit", "-c"] + files)
If the command is more complex and you have full control about its content then you could use shell=True to run it in the shell:
subprocess.check_call("bleachbit -c firefox.*", shell=True)
When shell is False you need to pass a list of args:
import subprocess
try:
subprocess.call(["bleachbit", "-c","firefox.*"])
except:
print ("Error.")

Why is my Python script writing Windows style carriage returns?

I am trying to write a script that creates a fabfile, saves it and then runs it. Here is my code so far:
#!/usr/bin/python
bakery_internalip = "10.10.15.203"
print "[....] Preparing commands to run within fabfile.py"
fabfile = open("sfab.py", "w")
fabfile.write("from fabric.api import run, sudo, task\n\n#task\ndef myinstall():\n\tsudo('yum install httpd')")
fabfile.close
print "Running Fab Commands"
import subprocess
subprocess.call(['fab', '-f', 'sfab.py', '-u ec2-user', '-i', 'id_rsa', '-H', bakery_internalip, 'myinstall'])
The contents of my fabfile are as follows:
[root#ip-10-10-20-82 bakery]# cat sfab.py
from fabric.api import run, sudo, task
#task
def myinstall():
sudo('yum install httpd')
My script gives the following error when I run it:
Fatal error: Fabfile didn't contain any commands!
However, if I run dos2unix on the file and then run the following, it works fine:
fab -f sfab.py -H localhost myinstall
Simple typo fabfile.close should be fabfile.close()
Running without closing will give you:
Running Fab Commands
Fatal error: Fabfile didn't contain any commands!
Aborting
with open("sfab.py", "w") as fabfile:
fabfile.write("from fabric.api import run, sudo, task\n\n#task\ndef myinstall():\n\tsudo('yum install httpd')")
Alway use with as above to open your files, it will automatically close them for you and avoid these simple errors.
I assume you are running it on Windows.
When using open(path, "w"), Python uses the OS's native linebreak combo.
To use \n specifically use open(path, "wb").
For more information see open().

Categories

Resources