[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: SSH -- Running remote command and NOT Exiting



On Wednesday 02 March 2005 10:23 pm, Gregory Seidman wrote:
> On Wed, Mar 02, 2005 at 10:08:14PM -0500, Hal Vaughan wrote:
> } I know I can use SSH to run a remote command like this:
> }
> } ssh newhost command-to-run
> }
> } And if I have it set up to use RSA or some other type of authentication,
> } it'll connect to the system, log in automatically, and run
> } "command-to-run", then log out.
> }
> } But I can't find anything in the man page (or on Google, but there's a
> } high noise-to-signal ratio, including many copies of the man page) where
> } I can tell ssh to run a command on login and NOT exit.  What can I do to
> } make ssh login, run a command, then stay logged in, waiting for another
> } command?
>
> This is not ssh's problem. Consider the following script:
>
> #!/bin/sh
>
> if test $# -lt 2
> then
>  echo "Usage: $0 <shell> <command> [arguments to command...]" >&2
>  exit 1
> fi
>
> SHELL="$1"
> shift
> eval "$@"
> exec "$SHELL"
>
> ------------------------------
>
> Suppose you save this to your bin directory on the remote machice as
> shellexec, and suppose you use bash as your login shell. Also, suppose
> (just for the sake of argument) that the command you want to run is echo
> foo. You'd run the following command from the local machine:
>
> ssh -t user@remotehost shellexec bash echo foo
>
> Note that the -t is necessary to have an interactive session even though a
> command is being run. Also note that since the arguments to shellexec will
> go through at least two shell expansions (one local and at least one
> remote), you will have to be very careful about quotes.
>
> That said, I can think of absolutely no good reason to want to do what you
> are asking. What are you actually trying to accomplish?

First, thanks, it helps a lot.  Actually, I used info from both this and the 
other suggestion to get it to work.

Second: When I ssh in, I have it run several commands in a script to set up a 
few things, including starting kate from the remote system and backing up 
some work in progress (so I can restore from the start of the session if it 
goes fubar).  It may seem like a small thing, but it takes a bit of time to 
do all this, and usually when I'm making the connection is when I start work 
in the morning, after lunch, or other times.  While part of it was the 
principle (I want to do this, so I'm sure there's got to be a way to do it -- 
I won't let my computer have the last word!), the other part is that it is a 
lot easier to start this, then have a few minutes while things are setting up 
to check e-mail, Slashdot, and a few other sites I keep up with.  It means I 
don't have to run ssh, wait for the connection, run the script, then wait 
again.  I like the idea of using one command to start the whole process 
running.

Thanks!

Hal

> } Thanks!
> } Hal
> --Greg



Reply to: