Open tabs in gnome-terminal from python script - python

I'd like to open different gnome-terminal tabs and connect via ssh to different servers. To do this, I used to use a little bash script.
#!/bin/sh
hosts=("172.16.1.183" "172.16.1.184")
pass=Password123!
for host in "${hosts[#]}"; do
gnome-terminal --tab -- sshpass -p $pass ssh -o StrictHostKeyChecking=no root#$host
done
I'm forcing myself to start scripting in python, as I would like to learn it. That's why now I'm trying to do this with this python script instead.
#!/usr/bin/env python3
import subprocess
for servidor in ['172.16.1.183', '172.16.1.184']:
subprocess.run(['gnome-terminal', '--tab', '--', 'sshpass', '-p', 'Password123!', 'ssh', '-o', 'StrictHostKeyChecking=no', 'root#',servidor])
It seems that for a second the tabs do open but then they close. As I don't know how to see any kind of log, it is hard for me to understand what's happening.
Could you please help me to understand what's happening? Is there any way to run these scripts with a "verbose" option like in bash with "-x" or things like -v, -vv, -vvv for instance?
Thank you very much!
edit: I'm using PYTHONVERBOSE=1 but I can't understand that output :/ is not like I can see an "error" anywhwere or even follow the step-by-step execution to understand what's happening.

You are passing the username and hostname as two list elements. Instead it should be just one element. Change 'root#',servidor to 'root#'+servidor
Your code can be written in a better way as follows
cmd = 'gnome-terminal --tab -- sshpass -p sshpass ssh -o StrictHostKeyChecking=no root#{}'
for servidor in ['172.16.1.183', '172.16.1.184']:
subprocess.run(cmd.format(servidor).split())

Related

Run commands in newly opened terminal

I want to open a new terminal through python. In that newly spawned terminal I want to execute commands. I've checked multiple posts which are suggested through similar questions but I couldn't find the answer.
I've tried this:
import os
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
The new terminal spawns but the commands don't get executed in the new terminal.
❯ python3 TestOpenTerminal.py
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Error: Failed to execute child process “cd” (No such file or directory)
How can I do this? Thx
The cd command don't work with gnome-terminal command, you need to use the option --working-directory=/path/to/dir.
The -e option is deprecated, you need to use -- followed by the command, like gnome-terminal -- 'command'.
To manipulate files the only way I could make work is using bash and option -c before the command.
So the command you want to do is:
gnome-terminal --working-directory=/home/john/Documents -- bash -c "echo 'writing to file' > testfile.txt"
I really recommend you to use the fabric library to use with terminal commands, it works with all systems and returns the output to you.
I don't know if you really want to open a terminal, but you could just open the file through python and write with it. Try to use the open() and write() functions.
Look closely at your command. Your opening single quote is closed by the single quotes around 'writing to file' and that causes a mess.
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
Also, the default behavior of gnome-terminal is to close when complete. You can keep it open by editing the profile as follows...
In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command

How to open a 2nd terminal from 1st one and run command in it? (without it being child process)

I am trying to write a script in python which should change my desktop wallpaper on my raspberry pi. I am a beginner in both python and linux, have been stuck on this problem the whole day. Would love to hear from you guys <3
This is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg
Concerning only the linux terminal syntax: i would like to open a second terminal and run a command in it, all initiated from the first terminal. If i type into my first terminal:
pi#raspberrypi:~ $ lxterminal &
it opens a new terminal window which stays open, and is not a child process right? In this 2nd terminal my change wallpaper command works. The following command does not work and if i put a "&" next to gnome-terminal it opens a new terminal but does not execute the command that was specified with -e and gives me an error.
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'
How do you open a new terminal with a command passed with -e which is also not a child process?
I know you are new so I want to introduce some concepts to you before I can answer your question.
The "&" operator in shell/unix is not meant to open a new terminal. It is an operator that invokes unix's handy little job control protocol, which allows the parallelization of complex programs! It's awesome. It makes that command a background process, which basically means it starts a new shell (or "terminal" in the language of your OP) which runs that process and leaves you in control of your current shell (or terminal). The shell you are still in control of is called the foreground process.
now, what you have going on with gnome-terminal is a little more complicated. gnome-terminal is executing a bash terminal (which has a shell for each process you run inside it) in the GNOME environment. -e is the command you want to send to this terminal. So, you put the ampersand (&) at the end of that command if that is the command you desire to send to the background.
Now, let's look at the command you want to run:
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'
-e indicates the command you want to run in the new terminal. bash-c (commmand) is changing your wallpaper. Ok, cool. exec bash is probably any weird error thrown, if I had to guess. But that line should perform nothing at all.
What it sounds like to me is you don't really need to send anything to the background.
gnome-terminal -e 'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg
should change your wallpaper. But, to answer the question completely, just place the & AFTER whichever command you wish to send in the background.

How to run python script at startup

I'm trying to make a Python script run as a service.
It need to work and run automatically after a reboot.
I have tried to copy it inside the init.d folder, But without any luck.
Can anyone help?(if it demands a cronjob, i haven't configured one before, so i would be glad if you could write how to do it)
(Running Centos)
run this command
crontab -e
and then add
#reboot /usr/bin/python /path/to/yourpythonscript
save and quit,then your python script will automatically run after you reboot
There is no intrinsic reason why Python should be different from any other scripting language here.
Here is someone else using python in init.d: blog.scphillips.com/posts/2013/07/… In fact, that deals with a lot that I don't deal with here, so I recommend just following that post.
For Ubuntu Variant:-
open the /etc/rc.local file with:
nano /etc/rc.local
add the following line just before the exit 0 line:
start-stop-daemon -b -S -x python /root/python/test.py
or
Give absolute path of your command i.e
nohup /usr/bin/python2 /home/kamran/auto_run_py_script_1.py &
The start-stop-daemon command creates a daemon to handle the execution of our program. The -b switch causes the program to be executed in the background. The -S switch tells the daemon to start our program. And the -x switch tells the daemon that our program is an executable.
To check and Run
sudo sh /etc/rc.local

nc.traditional -e script.py Not printing anything

I am starting a basic netcat server with the command nc.traditional -l -e server.py -p 4567
Problem is when I'm connecting to it (telnet 127.0.0.1 4567), the script starts but nothing gets on screen.
I have print instructions on the beginning of the script that are read by the interpreter (I tested that it starts via file manipulation) but nothing is written on my telnet terminal.
Moreover, it stays stuck on a raw_input instruction. I can write in the telnet terminal, but nothing seems to be sent to the python script.
I've tried with a bash script replacing the python one and this works, it prints things on screen and read inputs.
I've also tried connecting via ftp instead of telnet without results.
So, I finally found the solution
In the shebang, add the option -u to the interpreter to unbuffer stdin stdout and stderr
Shebang line:
#!usr/bin/python -u

Missing 'read' prompt in bash when using ssh?

Please tell me I'm missing something really obvious here:
$ cat ~/bashplay/f
#!/bin/bash
read -p 'RDY> ' x
echo $x
$ ~/bashplay/f
RDY> direct execution
direct execution
$ ssh somehost ~/bashplay/f
indirect via ssh
indirect via ssh
Note the missing "RDY>" prompt when using ssh. I see the same thing in python when using the "readline" package. Anyone know why?
From man bash:
-p prompt
Display prompt on standard error, without a trailing new‐
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
Use the ssh option -t which forces pseudo tty allocation:
ssh -t somehost ~/bashplay/f

Categories