Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh with command....Plus the shell

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!

like image 802
robarson Avatar asked Oct 27 '09 21:10

robarson


People also ask

What is SSH T option?

ssh(1) -t. OpenSSH SSH client (remote login program) -t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.


5 Answers

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

ssh -t me@machine ./executeMyScript '&&' bash -i
like image 175
martin clayton Avatar answered Oct 06 '22 05:10

martin clayton


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
like image 25
mob Avatar answered Oct 06 '22 06:10

mob


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.

like image 45
Warren Young Avatar answered Oct 06 '22 07:10

Warren Young


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.

like image 40
Epsilon Prime Avatar answered Oct 06 '22 07:10

Epsilon Prime


This works for me.

/usr/bin/ssh -t yourdomain.com "cd /directory; bash -i"
like image 30
user3174711 Avatar answered Oct 06 '22 07:10

user3174711