Script to open 6 ssh sessions in 1 iTerm window

I manage a number of servers and wanted a quick way to open a session to all of them in a single iTerm window. I wanted to open a single fullscreen iTerm window, split into 6 separate sessions, login to each of the servers, and start a screen session. Here’s what I came up with:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set serverList to {"host1", "host2", "host3", "host4", "host5", "host6"}

tell application "iTerm" to activate
tell application "iTerm"
	
	-- Create a new window
	
	set wsWindow to create window with default profile
        -- Enter full screen
	tell application "System Events"
		keystroke "f" using {command down, control down}
	end tell
	
	-- create 6 separate terminals
	
	tell current session of current window
		split horizontally with default profile
		split vertically with default profile
		split vertically with default profile
	end tell
	tell fourth session of current tab of current window
		split vertically with default profile
		split vertically with default profile
	end tell
	
	-- start sessions for each server in a different tab
	set counter to 0
	repeat with server in serverList
		set counter to counter + 1
		tell session counter of current tab of current window
			write text "ssh " & server & ".example.com"
			write text "screen -D -R"
		end tell
	end repeat
	
end tell
1 Like

If anyone has any suggestions to refactor this, I’d be open to suggestions.