Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In my Mac, how do I write a script that can do this?

  1. Open "terminal".
  2. In that terminal, "ssh myserver"
  3. Open a new "tab" inside the terminal.
  4. In that tab, "ssh myserver2"

Then, how would I run this script?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Open /Applications/Utilities/AppleScript Editor.app and enter the following:

tell application "Terminal"
    activate
    tell application "System Events"
        keystroke "t" using command down # new tab
        keystroke "ssh myserver"
        key code 36 # press enter
        keystroke "t" using command down # new tab
        keystroke "ssh myserver2"
        key code 36 # press enter
    end tell
end tell

Save as script or application. Anytime you execute, Terminal will be brought to front (started if necessary), and two new tabs will be created for your SSH sessions.

In this answer I outlined how to check whether a window is already open (without an application running) to prevent opening one tab/window too many.

share|improve this answer

You can create a Window Group to make it convenient to recreate windows and tabs, and you can use Settings profiles to issue ssh commands when windows/tabs are created.

  1. Duplicate or create two new profiles (Terminal > Preferences > Settings).
  2. In each profile, go to Shell and enable "Run command". Fill in the appropriate ssh commands for each profile.
  3. Create a new window with one of the profiles (Shell > New Window > [the first ssh profile]).
  4. Create a new tab with the other profile (Shell > New Tab > [the second ssh profile]).
  5. Create a window group for the window (Window > Save Windows as Group). Be sure you have no other terminal windows open at the time, otherwise they will all be saved in the group.

Now, whenever you want to recreate that window and start the ssh sessions, choose Window > Open Window Group > [your group].

You can also tell Terminal to open the window group every time you start Terminal (Terminal > Preferences > Startup > On startup, open: > Window group:).

share|improve this answer

Use iTerm 2 (scroll down to profiles)

Profiles

Many settings are stored in profiles. A profile is a named collection of settings, and you can have as many of them as you like. Most users only have one profile, but if you find that you often connect to different servers, they may be useful for you. A key feature of a profile is that you can associate a command with it that is run when it begins. For instance, if you often ssh to a particular host, you could create a profile with the command "ssh example.com" to automate that process.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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