ssh with command....Plus the shell
Asked Answered
P

5

15

I'd like a command that ssh'es into a machine, runs a command (cd or execute a script or su), and then gives me the shell. Passing a command to ssh seems to always exit.

Some examples of what I'm looking for: 'ssh me@machine1 "./executeMyScript && cd /developmentDirectory"' and then it gives me my shell back.

The reasons I want to do this is because I'm using mrxvt (tabbed x terms) which allows me to define commands for all of my tabs to execute on startup. I'd like to have a few ssh connections to the same machine but have them do different things after sshing.

-Rob

Thanks!

Pendentive answered 27/10, 2009 at 21:18 Comment(0)
B
10

Variation on the other answers really, use the -t option of ssh to force pseudo-tty allocation:

ssh -t me@machine ./executeMyScript '&&' bash -i
Bernardobernarr answered 27/10, 2009 at 23:4 Comment(2)
thanks! on first inspection, that works exactly as I'd like! Hurray for "-t"!Pendentive
Even though this question was answered suitably, if anyone is reading, do you know how I can have the instance of bash that I start with this command open up emacs (so that when I close emacs, it gets back to my bash terminal)? Heck..even something that opens up emacs without bash would be good (such that emacs doesn't exit immediately after starting).Pendentive
A
4

How about

    ssh me@machine ./executeMyScript '&&' bash -i

You have to quote the && so it will be passed to the remote machine instead of swallowed by the local shell.

Quote a ';' character and you can change your start directory, too:

    ssh me@machine ./executeMyScript '&&' cd /developmentDirectory ';' bash -i
Abfarad answered 27/10, 2009 at 21:26 Comment(2)
Thanks. The call to bash worked...infosar as it gave me a shell. Unfortunately it doesn't work very well.. simple keyboard shortcuts such as ctrl^a don't work which means I need to try and find a different solution. btw, it also prints out "bash: no job control in this shell".Pendentive
Try with the -t option: ssh -t me@machine ...Abfarad
S
2

Put something like this at the end of the script you want to run:

bash -li

That starts Bash as an interactive login shell. It's not perfect, since you'll be missing out on things like TERM forwarding, but it might be adequate to your needs.

If it isn't sufficient, I'd just make two separate ssh calls, one to run the script and another to log me in.

Substituent answered 27/10, 2009 at 21:27 Comment(0)
O
2

How about using the SendEnv option in ssh_config to send a specific environment option from your local machine to your remote machine followed up by checking for that environment variable in the remote host's configuration?

In your .ssh/special-config-file:

remotehost:
    SendEnv *

Your local script:

RUN_THIS_FIRST_COMMAND=blah
ssh -F special-config-file

Your remote .bash_profile:

if [ "$RUN_THIS_FIRST_COMMAND" != "" ] ; then
    $RUN_THIS_FIRST_COMMAND
fi

You might need to check some of these lines, I just threw this together from the man pages and haven't tried any of this before. Personally I don't like having to modify both my local and remote environment for this solution.

Ob answered 27/10, 2009 at 22:55 Comment(0)
C
1

This works for me.

/usr/bin/ssh -t yourdomain.com "cd /directory; bash -i"
Carbonate answered 8/8, 2014 at 18:43 Comment(3)
Why did it work for you? Give a bit more detail so that the OP can understand why you wrote what you did.Latinize
It's also a more or less direct copy of the existing chosen answer, posted 4 years ago...Dedal
An explanation would be nice.Shelbyshelden

© 2022 - 2024 — McMap. All rights reserved.