Skip to content

Instantly share code, notes, and snippets.

@SlevinBE
Last active February 5, 2016 14:28
Show Gist options
  • Save SlevinBE/f3883b314341e08bfa1f to your computer and use it in GitHub Desktop.
Save SlevinBE/f3883b314341e08bfa1f to your computer and use it in GitHub Desktop.
SSHs to a set of servers and executes a certain command once logged in
on run argv
-- How to execute:
-- osascript iTerm2-Multi-SSH.scpt development.mycluster htop
-- accepts two arguments:
-- 1. common part of the server names. eg. development.mycluster
-- 2. the command to execute once logged into the servers
set findhostnames to "less ~/.ssh/config | sed -n \"s|^Host \\(" & item 1 of argv & ".*\\)|\\1|gp\" | sed \"s/,$//g\""
set serverList to paragraphs of (do shell script findhostnames)
-- Print help if no servers are given
if (count serverList) = 0 then
log ("no servers found")
return
end if
set rightArrow to ASCII character 29
set numServers to count serverList
set numHorizontal to (round (numServers ^ 0.5) rounding up)
set createdPanels to 1
tell application "iTerm 2"
activate
select first window
tell current window
set newTab to (create tab with profile "Default")
tell newTab
tell current session
-- create the horizontal panels
repeat numHorizontal - 1 times
tell application "System Events" to keystroke "d" using {command down}
set createdPanels to createdPanels + 1
end repeat
set splitColumns to 0
-- split each of the panels vertically
repeat createdPanels times
-- re-evaluate number of vertical panels we need
set numVertical to (round ((numServers - createdPanels) / (numHorizontal - splitColumns)) rounding up) + 1
tell application "System Events" to keystroke rightArrow using {option down, command down}
repeat numVertical - 1 times
if createdPanels is less than numServers then
tell application "System Events" to keystroke "d" using {command down, shift down}
set createdPanels to createdPanels + 1
end if
end repeat
set splitColumns to splitColumns + 1
end repeat
end tell
delay 1
-- open ssh sessions--
repeat with loopIndex from 1 to numServers
set currentServer to item loopIndex of serverList
tell item loopIndex of sessions to write text "ssh " & currentServer
tell item loopIndex of sessions to write text (item 2 of argv)
end repeat
end tell
end tell
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment