4

From the command line I'd like to open a new Terminal window that is already SSH'ing to a host.

From the command line, I can open a new Terminal window very easily:

open -a Terminal

or

open -a Terminal /path/name

(The second one starts the Terminal already "cd"'ed to /path/name.)

However I want the Terminal to open and immediately run "ssh foo" instead.

2 Answers 2

5

There is a way to do this with

osascript -e 'tell application "Terminal" to do script "exec ssh localhost"'

Replace 'localhost' with the host name to which you wish to connect. You can include a username if the remote name is different from your local username, this would look like

rname@remotey.remote-face.org
\____/\_____/\______________/
 |     |      `- Domain name, you may be able to omit if on the same network as 'remotey'
 |     `- Remote hostname
 `- Specify remote login name followed by '@', can be omitted for same username

This suggestion is a variation on the top answer at https://stackoverflow.com/questions/31524499/open-terminal-from-shell-and-execute-commands

Use exec ssh instead of ssh if you want the window to close after logging out. If you use the 'exec ssh' option and your window stays open, then check your Terminal preferences--under "Profiles" > "Shell" > "When the shell exits:" is probably set to "Don't close the window"

2
  • 1
    That's great! One small tip: I changed ssh to exec ssh so that the window disappears when I log out.
    – TomOnTime
    Sep 20, 2019 at 13:50
  • @TomOnTime, That's a good suggestion. Most people will want the window to close after they log out.
    – Ken Tanaka
    Sep 20, 2019 at 20:15
1

As far as I know, you can't do it using open command.

But, you can do it using a combination of keyboard shortcuts and Terminal profiles.

Define a new profile on your Terminal app (let's call it ssh) and add your ssh command on Shell tab under Run command:.

Then your can define a shortcut on System Preferences - Keyboard - Shorcuts - App Shortcuts like the following:

enter image description here

Once done, you will have to type the shortcut on your Terminal app and will open a new window with your ssh command.

Careful, the Menu Title must have the exact same name as your newly created profile.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .