82

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.

user981178
  • 1,047
  • 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? – Ketan Mar 16 '14 at 03: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 03:12

1 Answers1

127

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
jsbillings
  • 24,406
  • 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 03: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 03:23
  • Does the -t option produce any sort of security concerns? I have not used this before. – user981178 Mar 16 '14 at 03: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
  • Interesting, using the ; does allow the mail command to stay in a shell. It also still works for the nano example. With both, it is still necessary to use exit instead of logout to end the SSH session. – user981178 Mar 16 '14 at 20:43
  • logout and exit are shell commands, and logout only works in a login shell. You could probably run bash -l instead of bash if you want logout to work. – jsbillings Mar 18 '14 at 00:35
  • I don't understand why it works even without -t! – Shayan Mar 22 '23 at 12:36