Is there a way to structure a single command to login via SSH to a remote server and run a program on the remote login shell?

In the OpenSSH manual, it reads "If command is specified, it is executed on the remote host instead of a login shell." So, for example, ssh user@server mail will login to the remote server, display the mailbox status, and then return you to the local shell. Is there a way to stay on the remote shell after displaying the mail status?

Furthermore, ssh user@server [command] does not seem to work if the command is a program, for example vim or mutt. Is there a way to login to the remote shell and run a program while staying in the remote shell during the program and after exiting the program, only finally exiting upon a specific logout command (just like in a normal SSH session)?

I would eventually like to be able to put such a command as an alias in the local .bashrc, so that it could be run quickly when desired. An example would be to login via SSH to a remote server and open mutt on the remote server to read or send email.

  • What do you mean by stay on the remote shell after displaying the mail status? Wouldn't that be equivalent to doing an ssh after running the mail command: ssh remote@server mail ; ssh remote@server? Also any remotely executed command that needs a terminal will not survive/work over ssh right? – mkc Mar 16 '14 at 3:07
  • Well, that does seem to work to run the command and then get the remote shell, but it does it by opening one SSH connection and running the command and closing the SSH connection and then opening another SSH connection. Is there any way to do this all with single first connection? Also, this still does not enable the command to run a program. – user981178 Mar 16 '14 at 3:12

Have you tried ssh -t user@server "mail && bash" (or replace bash with whatever shell you like)?

The -t is necessary in order to create a pseudo-tty for bash to use as an interactive shell.

  • 1
    Although, I'll admit, I'd probably prefer to just run mutt inside a tmux or screen, and then I can attach and reattach at will, plus I can run as many additional shells as I wanted. – jsbillings Mar 16 '14 at 3:16
  • 1
    Well, I am finding various behaviour with this command. Used as above (in the answer), it still only displays the mailbox status and then logs out, but now also displays that the connection is closed. Used as above, but with nano, for example, instead of mail, it opens a nano session and then upon exiting does return to the remote bash shell, which then has to be exited with exit instead of logout. I am unable to test it yet with mutt (or similar). – user981178 Mar 16 '14 at 3:23
  • Does the -t option produce any sort of security concerns? I have not used this before. – user981178 Mar 16 '14 at 3:25
  • As far as I know -t does not introduce any security concerns. Have you tried replacing the && with a ; ? – brm Mar 16 '14 at 12:47
  • any command that exits with a non-zero exit status would cause the second command to not run, due to shell logic. – jsbillings Mar 16 '14 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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