Saturday, March 9, 2013

iTerm2 Multiple Server Window applescript

To open multiple server terminals with iTerm2 automatically (in tabs), use an applescript with a list as shown below.  This is especially handy for admin tasks broadcast to all servers with the Command-Shift-I hotkey.





MyServers.list looks like:

username@server1.com
username2@server2.com



Run with:
osascript ./MyServers.scpt



AppleScript (MyServers.scpt):




-- Launch iTerm and log into multiple servers using SSH


tell application "iTerm"

activate

-- Read serverlist from file path below

set Servers to paragraphs of (do shell script "/bin/cat $HOME/itermstuff/MyServers.list")

repeat with nextLine in Servers

-- If line in file is not empty (blank line) do the rest

if length of nextLine is greater than 0 then

set server to "nextLine"
set term to (current terminal)

-- Open a new tab
tell term
launch session "Default Session"
tell the last session
write text "ssh -p 22 " & nextLine
-- sleep to prevent errors if we spawn too fast
do shell script "/bin/sleep 0.01"
end tell
end tell
end if
end repeat

-- Close the first tab since we do not need it

terminate the first session of the current terminal
end tell

No comments:

Post a Comment