I want to create host_install.py from bash variables.
So I have tried following :
#!/bin/bash
CM_HOST="10.0.5.99"
CM_USER="admin"
CM_PASSWORD="admin"
CM_CLUSTER_NAME="cluster"
INSTANCE_TYPE=`wget -q -O- http://169.254.169.254/latest/meta-data/instance-type`
cat > /tmp/host_install.py <<EOF
import socket
import commands
from time import sleep
cluster_name = "$CM_CLUSTER_NAME"
role_template = "$INSTANCE_TYPE"
print cluster_name
print role_template
EOF
chmod a+x /tmp/host_install.py
/tmp/host_install.py
This is printing
$CM_CLUSTER_NAME
$INSTANCE_TYPE
instead of
cluster
c3.2xlarge
What wrong I am doing here ?
Environment variables are accessed through os.environ
import os
cluster_name = os.environ['CM_CLUSTER_NAME']
role_template = os.environ['INSTANCE_TYPE']
Python default installation on Windows is C:\Python. If you want to find out while running python you can do:
import sys
print sys.prefix
Related
I have python app that runs some bash commands. Bash shell commands get network bandwidth so it need device name (enp35s0) of my gnu/linux machine.
I run python app typing sudo main.py en35s0 then enp35s0 going to 'n' variable. Below in the same file I have to pass n variable to bash part of app.
main.py
#!/usr/bin/python3
import os
import sys
import subprocess
import time
n = (sys.argv[1])
print(n)
def capture():
subprocess.run(["pkexec", '/bin/bash', '-c', bash], check=True)
bash = '''
#!/usr/bin/env bash
#set -x
INTERVAL="1" # update interval in seconds
#-----HERE I MUST PASS N VARIABLE----
n=enp35s0
# How many time script will work.
start=1
end=8 # put the number for minues. 3600 is 1 hour.
while [[ $start -le $end ]]
do
#echo "$start"
((start = start + 1))
# Capture packets.
R1=`cat /sys/class/net/$n/statistics/rx_bytes`
T1=`cat /sys/class/net/$n/statistics/tx_bytes`
sleep $INTERVAL
R2=`cat /sys/class/net/$n/statistics/rx_bytes`
T2=`cat /sys/class/net/$n/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TKBPS=`expr $TBPS / 1024`
RKBPS=`expr $RBPS / 1024`
echo "TX $1: $TKBPS kB/s RX $1: $RKBPS kB/s"
....
....
....
capture()
Use .format like this
#!/usr/bin/python3
default_var = 'AAA'
some_text_var = '''
some text
insert your new var here {value}
some more text
'''.format(value=default_var)
Testing:
>>> print(some_text_var)
some text
insert your new var here AAA
some more text
Example.
#!/usr/bin/python3
import sys
import subprocess
n = ""
script = "date.sh" # /path/to/your/shell/script.sh
if len(sys.argv) > 1:
n = sys.argv[1]
def capture():
subprocess.run(["/bin/bash", script, n], check=True)
capture()
My shell sctipt
$ cat date.sh
date $#
Exec
$ ./test.py
Sat 18 Jun 2022 09:48:51 PM CEST
$ ./test.py "+%Y"
2022
I have actualy python script running on background, you can see how it's displayed when i use command "ps -aux" :
root 405 0.0 2.6 34052 25328 ? S 09:52 0:04 python3 -u /opt/flask_server/downlink_server/downlink_manager.py
i want to check if this script are running from another python script, so i try to us psutil module, but it just detect that python3 are running but not my script precisely !
there is my python script :
import os
import psutil
import time
import logging
import sys
for process in psutil.process_iter():
if process.cmdline() == ['python3', '/opt/flask_server/downlink_server/downlink_manager.py']:
print('Process found: exiting.')
It's look like simple, but trust me, i already try other function proposed on another topic, like this :
def find_procs_by_name(name):
"Return a list of processes matching 'name'."
ls = []
for p in psutil.process_iter(attrs=["name", "exe", "cmdline"]):
if name == p.info['name'] or \
p.info['exe'] and os.path.basename(p.info['exe']) == name or \
p.info['cmdline'] and p.info['cmdline'][0] == name:
ls.append(p)
return ls
ls = find_procs_by_name("downlink_manager.py")
But this function didn't fin my script, it's work, when i search python3 but not the name of the script.
Of course i try to put all the path of the script but nothing, can you please hepl me ?
I resolve the issue with this modification :
import psutil
proc_iter = psutil.process_iter(attrs=["pid", "name", "cmdline"])
process = any("/opt/flask_server/downlink_server/downlink_manager.py" in p.info["cmdline"] for p in proc_iter)
print(process)
I would like to pass the variable "NUMBER_CAMS" and value from my python script to a bash environmental file "env_roadrunner"
following is the code that i have written
import subprocess
import os
import sys
import ConfigParser
os.chdir("/home/vasudev/LTECamOrchestrator_docker/tools/")
NUMBER_CAMS=sys.argv[2]
cmd = "xterm -hold -e sudo /home/vasudev/LTECamOrchestrator_docker/tools/create_pcap_replay_encoder " \
" /home/vasudev/LTECamOrchestrator_docker/tools/env_roadrunner"
p = subprocess.Popen([cmd] , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Following is my bash script which takes environmental variables
#!/bin/bash
# the name or ip address of the orchestrator
ORCHESTRATOR_IP="192.168.212.131"
# the port of the orchestrator
ORCHESTRATOR_PORT=9000
# password for the admin user
ORCHESTRATOR_PASSWORD='.qoq~^c^%l^U#e~'
# number of cameras to create from this pcap file
NUMBER_CAMS="$N"
# three port numbers that are only used internally but need to be free
I wanted to pass the value NUMBER_CAMS through my python script but i am getting following error
Traceback (most recent call last):
File "/home/vasudev/PycharmProjects/Test_Framework/Stream_provider.py", line 19, in <module>
NUMBER_CAMS=sys.argv[2]
IndexError: list index out of range
any suggestions why i am getting index out of range error
You to set the value of N in the environment so that your script can see that value to assign to NUMBER_CANS.
import subprocess
import os
import sys
import ConfigParser
os.environ["N"] = "2" # Must be a string, not an integer
cmd = ["xterm",
"-hold",
"-e",
"sudo",
"-E",
"./create_pcap_replay_encoder",
"env_roadrunner"]
p = subprocess.Popen(cmd,
cwd="/home/vasudev/LTECamOrchestrator_docker/tools/"
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Note that sudo ignores the current environment by default when running a command; the -E option I added allows create_pcap_replay_encoder to see the inherited environment. However, you can only use -E if sudo is configured to allow the environment to be preserved.
I'm having difficulties passing arguments to a embedded bash script.
#!/bin/bash/
function my_function() {
MYPARSER="$1" python - <<END
<<Some Python Code>>
class MyParser(OptionParser):
def format_epilog(self, formatter):
return self.epilog
parser=MyParser(version=VER, usage=USAGE, epilog=DESC)
parser.add_option("-s", "--Startdir", dest="StartDir",
metavar="StartDir"
)
parser.add_option("-r", "--report", dest="ReportDir",
metavar="ReportDir"
)
<<More Python Code>>
END
}
foo="-s /mnt/folder -r /storagefolder/"
my_function "$foo"
I've read Steve's Blog: Embedding python in bash scripts which helped but I'm still unable to pass the argument. I've tried both parser and myparser as environmental variables.
Is it as simple as defining $2 and passing them individually?
Thanks
You're overcomplicating this rather a lot. Why mess with a parser where
value="hello" python -c 'import os; print os.environ["value"]'
Or, for a longer script:
value="hello" python <<'EOF'
import os
print os.environ["value"]
EOF
If you need to set sys.argv for compatibility with existing code:
python - first second <<<'import sys; print sys.argv'
Thus:
args=( -s /mnt/folder -r /storagefolder/ )
python - "${args[#]}" <<'EOF'
import sys
print sys.argv # this is what an OptionParser will be looking at by default.
EOF
Summary: I am ssh'ing to a remote server and executing a fork1.py script over there which is shown below. But the trouble is that I want the processes to execute in the background, so that I can start multiple services.
I know we can use nohup, etc. but they are not working. Even when I use a & at the end, the process starts, but gets killed when the script terminates.
Here is the code:
import os
import sys
import commands
from var import key_loc
import subprocess
import pipes
import shlex
def check(status):
if status != 0:
print 'Error! '
quit()
else:
print 'Success :) '
file1=open('/home/modiuser/status.txt','a')
file1.write("Success :)\n")
if(sys.argv[1]=="ES"):
os.chdir('/home/modiuser/elasticsearch-0.90.0/bin/')
proc1=subprocess.Popen(shlex.split("nohup ./elasticsearch -p /home/modiuser/es.pid"))
if(sys.argv[1]=="REDIS"):
os.chdir('/home/modiuser/redis-2.6.13/src')
proc2=subprocess.Popen(shlex.split("./redis_ss -p /home/modiuser/redis.pid"))
if(sys.argv[1]=="PARSER"):
proc3=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f parser.conf"))
file1=open('/home/modiuser/pid.txt','a')
file1.write("PARSER-"+str(proc3.pid)+"\n")
file1.write(str(proc3.poll()))
file1.close()
if(sys.argv[1]=="SHIPPER_TCP"):
proc4=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f shipper_TCP.conf"))
file1=open('/home/modiuser/pid.txt','a')
file1.write("SHIPPER_TCP-"+str(proc4.pid)+"\n")
file1.close()
Where am I going wrong?
just try with
import os
os.system('python program1.py &') #this one runs in the background
os.system('python program2.py') #this one runs in the foreground