简体   繁体   中英

bash or python command to cycle through terminals and execute commands on them?

Stating the problem in a simplified form: I'm ssh'ing to two servers using two bash terminals and running programs on the servers whose outputs I need to continuously view. Server1's output appears on terminal1 and Server2's output on terminal2.

Is there a way to run a script which is aware of how many terminals are open, and be able to cycle through them and execute bash commands on them?

Pseudocode:

open terminal1
run program1
open terminal2
run program2
switch to terminal1
run program3 on terminal1

Looked at the man page for xterm, but there was no option to switch between terminals.
The closest I could get was this and this . But both didn't help.

In [5]: import subprocess

In [6]: import shlex

In [7]: subprocess.Popen(shlex.split('gnome-terminal -x bash -c "ls; read -n1"'))
Out[7]: <subprocess.Popen object at 0x9480a2c>

An alternative to screen would be tmux . Once you split your screens as you need them you can send commands to either one from a separate terminal something like:

tmux send-keys -t sessionname:0.0 "ls -al" "Enter"
tmux send-keys -t sessionname:0.1 "ls -al" "Enter"

The -t option references "sessionname":"window number"."pane number". I believe you can do a similar thing with screen but I've never used it.

Another option you might consider, if having two separate screens is not highly pertinent, is the python utility fabric . You can script commands to multiple servers and fetch results.

Creating a bash script that runs screen was the solution for me in a similar case. You can use screen to create a screen session, and inside, create multiple numbered windows, and execute commands on them.

I am running a scrip on a cluster with 8 computers, so I ssh in each of them and run the command htop to check if no one is using.

The flag -S names a session on screen, -p enumerates the session window, and -X stuff runs a command. Note that in order to run a command " is needed on a new line to simulate carriage return(Enter)

Here is the script

#!/bin/bash
screen -d -m -S dracos
# window 0 is created by default, command ssh is executed " needed in new line to simulate pressing Enter
screen -S dracos -p 0 -X stuff "ssh draco1
"
screen -S dracos -p 0 -X stuff "htop
"
for n in {2..8}; do
  # create now window using `screen` command
  screen -S dracos -X screen $n
  #ssh to each draco
  screen -S dracos -p $n -X stuff "ssh draco$n
"
  #run htop in each draco
 screen -S dracos -p $n -X stuff "htop
"
screen -S dracos -p $n -X stuff "<your_new_command_here>
"
done

If you want to run commands in other order you can put the line bellow after the for

screen -S dracos -p $n -X stuff "<your_new_command_here>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM