Run $Path command in Terminal in a python script - python

i use ipython notebook and I want to call a terminal command:
fft <in> <out>
my "fft" is in my $PATH so using a terminal, this would work.
How can I run this command in my ipython notebook?
the problem is that my fft executable is in my $PATH folder, and python won't recognize this

Found the solution:
import os
os.system("xterm -e 'bash -c \"fft -i 3 AddedK AddedK_ifft; exit -f exec bash\"' ")
xterm opens a new terminal
fft ...; calls the function fft
exit -f closes the terminal

Related

Script to open terminal in Kali, create multiple new tabs, rename and colour code them

I need help creating a script, bash, python whichever works best within Kali linux.
What I need the script to do is open up the default terminal (ZSH) create the following new tabs - main, msf, nc listener, http server, serachsploit and then colour code them.
This is the bash script to create the tabs. I am not sure about the colorcoding.
#!/bin/bash
mate-terminal --tab --title="main"
mate-terminal --tab --title="msf"
mate-terminal --tab --title="NC Listener"
mate-terminal --tab --title="http server"
mate-terminal --tab --title="Searchsploit"
Write a script:
sleep 1m;gnome-terminal –geometry=150×50 –tab –title="echo" -e "bash -c \"echo "hello";echo "there";exec bash\"" –tab –title="idea" -e "bash -c \"/opt/idea-IU-111.69/bin/idea.sh;exec bash\"" –tab –title="sql" -e "bash -c \"mysql -uroot -pigdefault;\"" –tab –title="firefox" -e "bash -c \"/usr/bin/firefox www.gmail.com;\""
The script above will open a terminal with 4 tabs:
echo "hello there"
Idea
MySql
Firefox
The script will also set the titles in the terminal for each tab.
One can personalize the above script accordingly.
Description of the script :
sleep 1m : Executes the script after 1 minute so that the system finishes
its startup process.
gnome-terminal : Open a terminal.
-geometry=150×50 : Set screen size for terminal.
–tab : Open new tab
–title : Set title for the terminal
-e : Execute the argument inside the terminal.
exec bash : Starts a new bash after executing all the commands.
This command is required if you do not want to close the current tab.
Save the script anywhere in the file system and make the file executable using the following command:
$chmod +x file.sh
Add this file to startup applications :
Go to System > Preferences > Startup Applications
Click on add and in "command" write:
bash path/to/your/file.sh
Close and its done!
Next time when you start your system, all the tasks mentioned in the script will be automated.

Using Python to run a sourced script, cd, and then run a command within the SAME shell

How can run a sourced bash script, and then change directories, and then run a command, all within the same shell (Using python)? Is this even possible?
My Attempt:
subprocess.check_call(["env -i bash -c 'source ./init-build ARG'", "cd ../myDir", "bitbake myBoard"], shell =True)
I would make this for you, but I need to see the absolute paths. Here is an example
subprocess.check_call(["""/usr/bin/env bash -c "cd /home/x/y/tools && source /home/x/y/venv/bin/activate && python asdf.py" >> /tmp/asdf.txt 2>&1"""], shell=True)

How can i launch the GIMP command line from a Python script?

From this stackoverflow thread https://stackoverflow.com/questions/4443...mmand-line, I have extracted this command line:
gimp-console -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"
It works perfectly well.
Now, I would like to run this command from a Python script, usually I use subprocess.Popen but this time it does not work and I get this message:
"batch command experienced an execution error"
How can I launch the GIMP command line from a Python script?
One easy way to resolve this is to just put your GIMP startup script into a bash script, say startgimp.sh
#!/bin/bash
#set your path to GIMP or cd into the folder where you installed GIMP
gimp-console -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"
then from Python simply call the bash script like so
import subprocess
subprocess.call(["bash","/path/to/your/script/startgimp.sh"])
If you are able to make the .sh script executable, e.g. chmod +x startgimp.sh then you can skip the bash part and just do subprocess.call("/path/to/your/script/startgimp.sh")
Some caveats
This is assuming you're on a UNIX based system
I used subprocess.call so this WILL block while waiting for GIMP to complete. Use Popen like you've used if you don't want this
I don't have GIMP to try this out, but you could also try splitting your GIMP command into elements in the list and pass it to subprocess and see if that works.
e.g. subprocess.call(["gimp-console","-idf","--batch-interpreter","python-fu-eval" and so on)

how to programmatically(python) execute terminal commands after invoking shell script(control will be under shell script))

OS:Ubuntu 16.04LTS.
Python automation.
Hi, i am trying to do python automation in ubuntu, where i have to invoke a shell script programmatically, after invoking shell script i need to enter and execute terminal commands.
i am invoking shell script with below commands
import os
os.system("gnome-terminal --working-directory='/home/murlee/CTS/android-cts/tools' -e 'bash -c \"./cts-tradefed; exec bash\"'")
*now i need help to enter and executing terminal commands after invoking the shell script
after invoking shell script my terminal will look like this
Terminal_snapshot
"murlee#murlee-Lenovo-ideapad-100-14IBD:~/CTS/android-cts/tools$
./cts-tradefed
Android CTS 6.0_r17 build: 3866870
cts-tf >(here i have to enter commands and execute)"
Thanks!
Create an init script named my-init.sh
./cts-tradefed
Now call
import os
wd = '/home/murlee/CTS/android-cts/tools'
cmd = "bash --init-file {0}".format('./my-init.sh')
os.system("gnome-terminal --working-directory='{0}' -e '{1}'").format(wd,cmd)

Python3 restart a script from another script

This is the situation:
I've 2 script in python running in 2 different shells in Linux:
1° - python3 server.py
2° - python3 roomcontrol.py
I need that the user can restart roomcontrol.py from server.py.
I tried with subprocess:
from subprocess import call
dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
call(["python3",dir])
These instructions just start a new istance of "roomcontrol.py" in the shell of "server.py", I need to restart roomcontrol.py in his shell. Or close his shell and open a new one.
Edit:
I also tried:
import subprocess
dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
subprocess.Popen([dir], stdout=subprocess.PIPE, shell=True)
It doesn't work.
It writes a lot of stuff in the same shell of server.py and my cursor become a cross and if I click somewhere it wrtes stuff like before. A little example of what it writes:
import: unable to grab mouse `': Resource temporarily unavailable # error/xwindow.c/XSelectWindow/9199.
import: unable to grab mouse `': Resource temporarily unavailable # error/xwindow.c/XSelectWindow/9199.
.
.
.
from: can't read /var/mail/xml.dom
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 9: /home/stark/Desktop/Tr: not foundlease/roomcontrol.py:
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 10: /home/stark/Desktop/T: not foundelease/roomcontrol.py: try:
Create a new .sh file ("restart.sh" for example):
#!/bin/bash
kill $(pgrep -f 'python3 roomcontrol.py')
python3 roomcontrol.py &
Then just call
os.system('./restart.sh')
somewhere in your "server.py" script.
PS: You have to make the .sh file executable by running the following command:
chmod +x restart.sh
Edit: I'm not sure how you can start a process from a different shell, but you can start "roomcontrol.py" in another terminal window with the following (bash) command:
gnome-terminal -x sh -c 'python3 roomcontrol.py'
But then you'd have to replace "restart.sh" by
#!/bin/bash
kill -9 $(pgrep -f 'sh -c python3 roomcontrol.py')
gnome-terminal -x sh -c 'python3 roomcontrol.py'

Categories