Skip to content

Instantly share code, notes, and snippets.

@totomz
Created February 3, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totomz/d0a56f5f6dee05efb9ab2f9ebf00f15c to your computer and use it in GitHub Desktop.
Save totomz/d0a56f5f6dee05efb9ab2f9ebf00f15c to your computer and use it in GitHub Desktop.
iTerm 2 script to open multiple SSH sessions to a remote cluster
#!/usr/bin/env python3.7
import iterm2
from pathlib import Path
USER = "debian"
HOSTS = x = open("{}/Desktop/_cluster".format(Path.home())).readlines()
async def main(connection):
app = await iterm2.async_get_app(connection)
window = app.current_terminal_window
if window is None:
print("No current window")
exit(1)
await split(window, len(HOSTS))
for i in range(0, len(window.current_tab.sessions)):
session = window.current_tab.sessions[i]
host = HOSTS[i]
await session.async_send_text("ssh {0}@{1}\n".format(USER, host))
async def split(window, n_split):
sessions = list()
tempsess = list()
panels = dict()
sessions.append(window.current_tab.current_session)
panels[window.current_tab.current_session.session_id] = window.current_tab.current_session
is_vertical = True
i = 0
while i < n_split:
is_vertical = not is_vertical
for session in sessions:
splitted = await session.async_split_pane(vertical=is_vertical)
panels[splitted.session_id] = splitted
i = i + 1
tempsess.append(session)
tempsess.append(splitted)
await splitted.async_send_text("echo zio\n")
if i == n_split - 1:
return list(panels.values())
sessions = tempsess
tempsess = list()
iterm2.run_until_complete(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment