In your comment on fons's answer, you say that it doesn't work while using screen
.
Could you elaborate on that? Looking at the source code for openssh, sshd executes the command by calling
YOUR_DEFAULT_SHELL -c COMMAND
So, for example, if your default shell is screen
, then this won't work all that well because screen
's -c
flag just overrides its .scrreenrc
. So, there's really no way to send commands to screen if it's your default shell. You'll have to actually run screen as the command given to ssh, but with a default shell that isn't screen.
If that's what you're trying to do, I think things will get really weird, since screen
will also close windows with non-interactive programs, so you'll have to do a similar trick to fons's, but one level deeper. SO, with, e.g. /bin/bash (and not screen) as your default shell Something like:
ssh user@host -t 'screen bash -l -c "ls;bash"'
Which should --take a deep breath-- ssh into the host, run bash -c with a command of screen, which will make a new window. If this window just opened up ls, it would end, and screen would terminate, so we use fons's trick inside the new screen window.
I think that'll work, if that's even what you were trying to do ;)
this is a similar question http://superuser.com/questions/261617/with-ssh-how-can-you-run-a-command-on-the-remote-machine-without-exiting but none of the answers really seem to fit what I'm trying to do.
– matthew – 2011-07-05T14:16:59.513How about
ssh example.com 'ls;bash'
? – Andrejs Cainikovs – 2011-07-05T14:40:34.153you need the -i on my systems to make the second shell an interactive one. – Flexo – 2011-07-05T15:01:50.203
option -t is the answer to your question. Other options (for example keychain) exist but depends on your real needs, which are not clear enough to me . – hornetbzz – 2011-07-05T23:33:58.813
@hornetbzz -t gives me pseudo-tty. But otherwise the behavior is the same. I want to launch an interactive shell, run a command inside that shell and have the shell remain open after the command is run. – matthew – 2011-07-06T03:50:20.137