user1170533
user1170533

Reputation: 79

gnome terminal tabs open multiple ssh connections

I have a file with a list of servers:

SERVERS.TXT:

    192.168.0.100
    192.168.0.101
    192.168.0.102

From a gnome terminal script, I want open a new terminal, with a tab for each server.

Here is what I tried:

gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e 'ssh usr@$SERVER'"; done < SERVERS.TXT`

Here is the error:

Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was ''ssh')

Tried removing the space after the -e

gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e'ssh usr@$SERVER'"; done < SERVERS.TXT`

And I get a similar error:

Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was '[email protected]'')

Obviously there is a parsing error since the the shell is trying to be helpful by using the spaces to predict and place delimiters. The server file is changed without notice and many different sets of servers need to be looked at.

Upvotes: 2

Views: 2591

Answers (4)

DarkSwiss
DarkSwiss

Reputation: 1

Try to type

eval

before gnome terminal command.

it should be something like this:

eval /usr/bin/gnome-terminal $xargs

worked for me!

Upvotes: 0

Tigger
Tigger

Reputation: 9130

I found this question while searching for an answer to the issue the OP had, but my issue was a little different. I knew the list of servers, they where not in a file.

Anyway, the other solutions posted did not work for me, but the following script does work, and is what I use to get around the "--command/-e" is not a valid command" error.

The script should be very easy change to suit any need:

#!/bin/sh
# Open a terminal to each of the servers
#
# The list of servers
LIST="server1.info server2.info server3.info server4.info"
cmdssh=`which ssh`

for s in $LIST
do
    title=`echo -n "${s}" | sed 's/^\(.\)/\U\1/'`
    args="${args} --tab --title=\"$title\" --command=\"${cmdssh} ${s}.com\""
done

tmpfile=`mktemp`
echo "gnome-terminal${args}" > $tmpfile
chmod 744 $tmpfile
. $tmpfile
rm $tmpfile

Now the big question is why does this work when run from a file, but not from within a script. Sure, the issue is about the escaping of the --command part, but everything I tried failed unless exported to a temp file.

Upvotes: 1

jane arc
jane arc

Reputation: 594

Is this a problem of parsing command-line options? Sometimes if you have one command sending arguments to another command, the first can get confused. The convention is to use a -- like so:

echo -- "--tab -e 'ssh usr@$SERVER'";

Upvotes: 1

gpoo
gpoo

Reputation: 8638

I would try something like:

$ while read SERVER;do echo -n "--tab -e 'ssh usr@$SERVER' "; \
  done < SERVERS.txt | xargs gnome-terminal --profile=TabProfile

This is to avoid any interpretation that the shell could do of the parameters (anything starting with a dash).

Because it is concatenating strings (using -n), it is necessary to add an space between them.

Upvotes: 1

Related Questions