Skip to content

Instantly share code, notes, and snippets.

@zoispag
Last active May 27, 2019 17:39
Show Gist options
  • Save zoispag/3edf2e59f7d52552280582a199461e04 to your computer and use it in GitHub Desktop.
Save zoispag/3edf2e59f7d52552280582a199461e04 to your computer and use it in GitHub Desktop.
iTerm2 Multiple sessions script

Multiple sessions with iTerm2

Save the file as ~/Library/Application Support/iTerm2/Scripts/ssh_multiple.scpt

Restart iTerm and go to scripts menu and select the script.

image

Once opoened, a prompt to select a txt file will open. Select a new-line separated .txt file, containing the hostnames of the servers you want to connect simultaneously.

eg:

example.com
example.org
example.net

In iTerm2, hit ⌘Command + ⇧Shift + I to activate "Keybord input to multiple sessions".

Whatever you type, will be typed to all open tabs. Useful to run interactive apps (like sending multiple keystrokes to vim etc)

--
-- An AppleScript function that reads a file and returns the lines
-- from that file as a list.
--
on returnFileContentsAsList(theFile)
set fileHandle to open for access theFile
set theLines to paragraphs of (read fileHandle)
close access fileHandle
return theLines
end
set theFile to choose file of type "txt" with prompt "Please select a text file to read:"
set hostnames to returnFileContentsAsList(theFile)
if application "iTerm" is running then
tell application "iTerm"
create window with default profile
tell current tab of current window
select
tell current session
-- make the window fullscreen
tell application "System Events" to key code 36 using command down
split horizontally with default profile
set num_hosts to count of hostnames
repeat with n from 1 to num_hosts
if n - 1 is (round (num_hosts / 2)) then
-- move to lower split
tell application "System Events" to keystroke "]" using command down
else if n > 1 then
-- split vertically
tell application "System Events" to keystroke "d" using command down
end if
delay 1
write text "ssh " & (item n of hostnames)
end repeat
end tell
end tell
end tell
else
activate application "iTerm"
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment