Skip to content

Instantly share code, notes, and snippets.

@desterhuizen
Last active February 3, 2021 12:37
Show Gist options
  • Save desterhuizen/be12b5f4c6f1f690715b225662f954c9 to your computer and use it in GitHub Desktop.
Save desterhuizen/be12b5f4c6f1f690715b225662f954c9 to your computer and use it in GitHub Desktop.

Auto connect to Servers

Automatically connect to hosts from a list in a file

  • line 1: panes / tabs, panes will split the new tab as needed and tabs make a new tab for each host.
  • line 2: command to enter befotre the hostname in each host
  • line 3 -> X: The number of hosts you want to connect to.

Create a file in ~/servers/new_servers

panes
ssh -i key.pem ec2-user@
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6

Import Script under Scripts > Manage > Import...

#!/usr/bin/env python3.7

import iterm2
import math
ompirt os

async def main(connection):
    #load server list
    input = []
    filePath = os.path.expanduser("~")+'/servers/new_servers'
    print (filePath)
    with open(filePath) as file:
        input = file.read().splitlines()

    # get a list of server
    servers = input
    command = input[1]

    open_type = input[0]
    servers.pop(0)
    servers.pop(0)

    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    print (open_type)
    if window is not None:
        if open_type == 'panes':
            tab = await window.async_create_tab(index=0)
            sessions = []
            server_count = len(servers)
            #Add the firt Session the the session list for clarity and ease of use
            sessions.append(tab.sessions[0])

            for i in range(2):
                print("split Vertical")
                sessions.append(await sessions[i].async_split_pane(vertical=True))

            if server_count > 3:
                for i in range ( (math.ceil(server_count / 3)-1)*3 ):
                    if (i+3) < server_count:
                        print("Split Horizontal")
                        sessions.append(await sessions[i].async_split_pane(vertical=False))

            counter = 0;
            for server in servers:
                await sessions[counter].async_set_name("Server " + str(counter))
                await sessions[counter].async_send_text(command + server)
                profile = await sessions[counter].async_get_profile()
                await profile.async_set_badge_text("Server " + str(counter))
                counter+= 1
        elif open_type == 'tabs':
            tabs = []
            for server in servers:
                tabs.append(await window.async_create_tab())
                await tabs[-1].sessions[0].async_send_text(command + server)
                profile = await tabs[-1].sessions[0].async_get_profile()
                await profile.async_set_badge_text("Server " + str(counter))
 
    else:
        # You can view this message in the script console.
        print("No current window")

iterm2.run_until_complete(main)

Set a Badge in the current terminal

Add the following to your bash profile

function badge () {
  printf "\e]1337;SetBadgeFormat=%s\a" \
  $(echo -n "$1" | base64)
}

Once profile is loaded run command

badge NewHeading

#or

badge "Multi word heading"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment