Normally, if you pass a command to ssh like so

ssh me@example.com 'some-command.sh'

You get a non-interactive session, and ssh exits as soon as the command (or script, or whatever) is run. How can I get an interactive session, and yet run a command that I've specified from my local machine. For instance, I'd like to be able to open screen and restart a specific screen session:

ssh me@example.com 'screen -r 12345'

but this is not allowed, returning the error "Must be connected to a terminal." which I presume means essentially "must call this command from an interactive shell".

As another example, I use a ton of machines at work that often require me to use a shared account to have rights to do certain things (yeah I know: bad idea but don't blame me, I didn't set things up that way), and they usually have ksh as the default shell. I'd like to log in, switch to bash (or zsh), and source my own account's .bash_profile (or .zshrc). This would be easy if I could establish an interactive ssh session and pass a command to the remote machine to run upon login.

I have a very kludgey (and untested) solution in mind that I will post, but I'm hoping someone knows a better way.

UPDATE: for those who didn't realize this, I'm not planning to do all this by hand, so telling me to open an interactive session and then do it by hand doesn't actually solve anything.

2nd UPDATE: To attempt once again to clarify: I'm trying to run (arbitrary) commands on the server (programmatically specified on the client) but then have an interactive session open that is affected by anything done by those programmatic commands.

link|improve this question

50% accept rate
1  
belongs on unix.stackexchange.com – dogbane Mar 23 '11 at 16:15
feedback

migrated from stackoverflow.com Mar 24 '11 at 0:51

This question came from our site for professional and enthusiast programmers.

5 Answers

You can try the following command to start up bash and source your own profile when you ssh:

ssh  -t hostname "bash --rcfile ~user/.bashrc"
link|improve this answer
1  
+1. The -t switch fixes the questioner's problem with screen as well. – Patches Mar 24 '11 at 2:00
feedback

This is kludgey, and untested, but I think it should work.

Create a script named (e.g.) remote-starter that takes a quoted argument after a user@host argument:

remote-starter user@example.com 'some-command.sh arg1 arg2'

This script should first save the quoted command (without quotes) in file named (e.g.) .startup on the remote machine, and then run a regular ssh command, this time not passing any command to be run on the remote machine. (If running ssh user@example.com from within a script, it may work to run exec ssh user@example.com.)

For this to work, the remote machine must source .startup from its .bash_profile or .zshrc or whatever. After running .startup, it should delete .startup, so the file is not run the next time you do a regular login.

link|improve this answer
feedback

Use ssh -X to use termIO cmds.

Also - you can chain your cmds like "ssh 'source ~/.bashrc && cd && do '"

link|improve this answer
but after the whole chain is done, I don't have an interactive session remaining, right? It still closes, right? – Brandon Apr 3 '11 at 16:15
And would you care to elaborate on the termIO solution? – Brandon Apr 3 '11 at 16:15
feedback

Fairly simple - just run ssh with no commands, and whatever arguements you need. this will connect you to the remote system. You then do what you need to do, and then, once your done, use the 'quit' command. For example - for a user 'geek' with interactive login

geek@crossroads:~$ ssh -l geek example.com
geek@example.com's password:
geek@tamandua:~$ uname -r
2.6.32-30-generic-pae
geek@tamandua:~$ exit
logout
Connection to lupinenet.co.cc closed.
link|improve this answer
This is merely a normal interactive session, and misses the whole point of the question. I'm trying to run remote commands with ssh and then have an interactive session. I don't want to manually run the commands IN the interactive session. – Brandon Dec 19 '11 at 15:56
feedback

why not install detach on the remote system and use:

ssh -t user@example.com "/home/user/bin/detach ls"
link|improve this answer
will this run 'ls' and THEN leave me with an interactive session? What if, instead of 'ls', I run 'source .somefile' to get some aliases and functions. Will they be available in my interactive session, or will it have been run under a different session? – Brandon Dec 19 '11 at 15:59
No, due to detach, ls doesn't keep the session open and will disconnect without printing the output of ls but it'll still run. – Dan D. Dec 22 '11 at 8:25
I'm trying to run (arbitrary) commands on the server (programmatically specified on the client) but then have an interactive session open that is affected by anything done by those programmatic commands. So if I understand you correctly, this doesn't help me. – Brandon Dec 23 '11 at 18:03
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.