executing a command over ssh, and then running bash

2.1k views Asked by At

I'm trying to set up a script that opens a terminal, does an ssh to a remote server, and executes a command (tail -F logfile in my case).

What I have so far is the following

gnome-terminal -e 'ssh -t server "tail -F logfile"'

This works to some extent. -t ensures that signals like SIGINT are sent through the commands running remotely.

However, when I ctrl-c the tail, I would really like to drop down to a bash terminal on the remote server. Right now, if I ctrl-c the tail, then the tail is closed, which causes ssh to exit, which causes the whole terminal to be closed.

What I would like is for the tail to be terminated and be left with a bash shell on the remote server.

I have tried the following:

gnome-terminal -e 'ssh -t server "tail -F logfile; /bin/bash"'

but that doesn't seem to work. That is, if I run this without gnome-terminal, just ssh -t ..., then see the following:

some lines
from the log
^CConnection to server closed.

But, If I do

gnome-terminal -e 'ssh -t server "nonexistantcommand; /bin/bash"'

Then I get an error that nonexistantcommand is not found, and then I do drop down to a bash on a remote server...

Does anyone have any suggestions or hints on how to get this going? Thanks in advance.

3

There are 3 answers

1
chaos On BEST ANSWER

Here, have a nasty hack:

gnome-terminal -e 'ssh -t server "echo \"tail -F logfile;rm /tmp/foo\" > /tmp/foo; bash --rcfile /tmp/foo"'
0
chx On
--init-file filename
--rcfile filename

Execute commands from filename (instead of `~/.bashrc') in an interactive shell.

so run bash with --rcfile pointing to a script running tail -F.

0
ephemient On

Why not do the obvious? "If you got a SIGINT, execute an interactive shell."

gnome-terminal -e 'ssh -t server "trap \"exec sh -i\" INT; tail -F logfile"'