I am trying to write a python script that benchmarks several pieces of code. The issue is when the script is run the output of the command id different from what I would get it I run it directly at bash prompt.
Here is the code for the python script
import subprocess
import re
import os
app = os.getcwd() + "/" + "myapp"
testPopen = subprocess.Popen(args=['/usr/bin/time',app],
stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=False)
testPopen.wait()
Here is the ouput of above code
real 1.0
user 0.8
sys 0.0
When you run time myapp from the bash prompt, you are using bash's time command. (Type help time to see that bash understands it as a bash command.)
When you run the python script you are using /usr/bin/time, which is a different program. (Type man time or which time to see that time is also a program. How confusing!)
To get the python script to use the bash time command:
import shlex
testPopen = subprocess.Popen(shlex.split('bash -c "time {a}"'.format(a = app)),
stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=False)
out,err = testPopen.communicate()
print(err)
Related
I am new to the nifi platform.
I am trying to use a python script to capture network packet which works on VScode and want to implement same script using NiFi but unable to do so.
This python code I used:
import os, subprocess
from subprocess import PIPE
from datetime import datetime
n = 10
filename = str(datetime.now()).replace(" ","")
b = subprocess.run(f'sudo tcpdump udp -e -i wlp6s0 -nn -vvv -c {n} -w {filename}.raw',shell=True)
c = '"X%02x"'
a = subprocess.run(f"sudo hexdump -v -e '1/1 {c}' {filename}.raw| sed -e 's/\s\+//g' -e 's/X/\\x/g' ", shell=True , stdout=PIPE, stderr=PIPE)
output_file = open (f'{filename}.txt', 'w')
output_file.write(str(a.stdout))
# print("*************************File Created*************************")
output_file.close()
I am using Execute Script Processor for implementing the python script. But it doesn't seem to be working. For executing the "sudo command" I have set to use no password so that no input is needed while executing the script.
Thank you!
Since you're just calling shell commands, you might consider ExecuteStreamCommand instead. You can still run the top-level Python script to call the subprocesses, but since you're not working with flowfile attributes you might be better served being able to call "real" Python. In ExecuteScript the engine is actually Jython and it doesn't let you import native (CPython) modules such as scikit, you can only import pure Python modules (Python scripts that don't themselves import native modules)
I want to execute commands in the terminal through a python scripts.
i want to create a script which takes data from a .txt file adds that in a list and then one by one execute them in the terminal.
what i am looking for is a process to execute commands in the terminal in Kali Linux, I couldn't find anything online.
like in windows we use import subprocess or import os
Thank you.
example command is like
python3 app.py
Try this:
import subprocess
command = "python3 app.py"
subprocess.call(command, shell=True)
You can use the os.system function. It returns the return value of the command run.
E.g.,
status = os.system('echo hello')
I would like to connect a remote machine and run background script in that machine from python.
I tried:
os.system("ssh root#10.0.0.1 \' nohup script.sh & \')
But it seems not working. And if I put nohup in script.sh, and simply run
os.system("ssh root#10.0.0.1 \' script.sh \'")
The nohup command would not work in either cases.
I'm confused why so, and is there anybody knows how to do background job from python or it's just impossible doing it this way?
What kind of errors are you getting? What version of Python are you using?
You should take a look at this Python subprocess - run multiple shell commands over SSH
import subprocess
sshProcess = subprocess.Popen(["ssh", "root#10.0.0.1"],
stdin=subprocess.PIPE,
stdout = subprocess.PIPE,
universal_newlines=True,
bufsize=0)
sshProcess.stdin.write("nohup script.sh &")
For example you have a local script (python, bash, etc. Here I am demonstrating you using a python script)
First you create a python file locally. Lets say hello.py
# 'hello.py'
import os
print os.system('hostname')
Secondly now a python script which would execute the above hello.py on a remote machine
import pathos
copy = pathos.core.copy('hello.py', destination='abc.remote.com:~/hello.py')
exec = pathos.core.execute('python hello.py', host='.remote.com')
print exec.response()
I'm on Ubuntu 16.04.1, and I have a python script to download image files from websites, the codes are as follows:
import sys
import os
import time
import json
import shlex, subprocess
import signal
import random
R = 0
r = 0
targets = ['192.0.78.13', '134.109.133.7', '216.58.212.227', '54.246.159.107', '185.60.216.35', '98.136.103.24']
if __name__ == "__main__":
while True:
cmd = 'wget -A pdf,jpg,png -m -p -E -k -K -np --delete-after '
R = random.randint(0,5)
cmd += targets[R]
args = shlex.split(cmd)
p = subprocess.Popen(args, shell=False)
time.sleep(2.0)
# killing all processes in the group
os.kill(p.pid, signal.SIGTERM)
if p.poll() is None: # Force kill if process
os.kill(p.pid, signal.SIGKILL)
r = random.randint(3,20)
time.sleep(r-1)
it run perfectly with command "python webaccess.py", now I want to run it automatically on startup in the background.
I've tried two methods but all of them are fail (the scripty does not run):
Use crontab using the guide here: Run Python script at startup in Ubuntu
#reboot python /bin/web1.py &
Edit the rc.local using the guide here: https://askubuntu.com/questions/817011/run-python-script-on-os-boot
python /bin/web1.py &
Is there any way to solve this?
Thank you in advance.
your rc.local method should work, check using your full python path. if that is default /usr/bin/python
/use/bin/python your_file_py
also you said you verified python webaccess.py do verify it from outside the folder of script.
also note that scrips in rc.local are executed by root
so check path_to_python python_file from root #
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.")