Home > Enterprise >  SSH the output to different terminals
SSH the output to different terminals

Time:01-28

I am using for loop to SSH multiple hosts

#!/usr/bin/bash


bandit=$(cat /root/Desktop/bandit.txt)

for host in {1..2}
do
        echo "inside the loop"
        ssh bandit$host@$bandit -p 2220  
        echo "After the loop"
done


#ssh [email protected] -p 2220

bandit.txt has the following content " bandit.labs.overthewire.org"

I am getting the SSH prompt but one at a time, say for example First I got "bandit1" host login prompt, and after closing the "bandit1" ssh host I am getting second ssh session for "bandit1"

I would like to get two different terminals for each SSH session.

CodePudding user response:

But there is no such things as "terminal window" in bash (well, there is a tty, yours; but I mean, you can't just open a new window. Bash is not aware that it is running inside a specific program that emulate the behavior of a terminal in a GUI window).

So it can't be as easy as you would think.

Of course, you can choose a terminal emulator, and run it yourself.

For example

for host is {1..2}
do
    xterm -e ssh bandit$host@$bandit -p 2220 &
done

maybe what you are looking for, if you have xterm program installed.

CodePudding user response:

Maybe with some additional error checking, something like this -

scr=( /dev/stdout
      $(ps -fu $USERNAME |
          awk '$4~/^pty/{lst[$4]} END{for (pty in lst) print pty}' ) )
for host in {1..2}; do echo ssh bandit$host@etc... >> /dev/${scr[$host]}; done

There are a lot of variations and kinks to work out though. tty or pty? what is there's no other window open? etc... But with luck it will give you something to work from.

  • Related