Last Updated: July 28, 2017
·
17.63K
· mattball

SSH to many hosts in split panes with iTerm

Opens a new, full screen iTerm window with 2 x n split panes in iTerm 2. Then SSHs to each of the hosts in the hostnames list.

You can drop this file in ~/Library/Application\ Support/iTerm/Scripts and then run the script from the iTerm "Script" menu. Create the Scripts folder if it doesn't exist. If this menu is not visible after this script is in place, just relaunch iTerm.

Requires iTerm2 (this should be obvious), and may not work depending on your key bindings.

set hostnames to {"foo", "bar", "baz", "quux", "corge", "spam", "eggs", "ham"}

launch "iTerm"

tell application "iTerm"
    activate

    -- ssh in split panes to my queue processor hosts
    set myterm to (make new terminal)
    tell myterm
        launch session "Default Session"

        -- make the window fullscreen
        tell i term application "System Events" to key code 36 using command down

        -- split horizontally
        tell i term application "System Events" to keystroke "D" using command down
        -- move to upper split
        tell i term application "System Events" to keystroke "[" using command down

        set num_hosts to count of hostnames

        repeat with n from 1 to num_hosts
            if n - 1 is num_hosts / 2 then
                -- move to lower split
                tell i term application "System Events" to keystroke "]" using command down
            else if n > 1 then
                -- split vertically
                tell i term application "System Events" to keystroke "d" using command down
            end if
            delay 1
            tell the current session to write text "ssh " & (item n of hostnames)
        end repeat
    end tell

end tell    

6 Responses
Add your response

so the script menu thing..is that a no go? cause i created the dir in App Support folder for iTerm and nada

over 1 year ago ·

@hcavillones it certainly works for me. You may need to relaunch iTerm or click "Script → Refresh" in the menu bar.

over 1 year ago ·

ok i got it to show but it won't launch

over 1 year ago ·

also the file has to be saved as a .scpt file in order to activate the Scripts Folder in iTerm2

over 1 year ago ·

iTermocil enables you to do this flexibly by defining profiles in YAML files:

https://github.com/TomAnthony/itermocil

(Disclaimer: I wrote it [just for this purpose])

over 1 year ago ·

Hi,

I'm a noob and I'm using iTerm2. I don't see "Scripts" in main menu of iTerm2.
Also, I saved this in file login.scpt and tried to run from Finder window, which took me in AppleScripts. I ran the script, which gave me error "Expected end of line but found identifier" for line of "-- make the window fullscreen"

Please elaborate step-by-step guide on how exactly to create,save and execute this script. Thanks in advance!

over 1 year ago ·