Skip to content

luciopaiva/itermoxyl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

iTermoxyl

iTermoxyl

iTermoxyl is command line tool to automatically open multiple ssh connections in iTerm2 by directly querying ~/.ssh/config using patterns provided via the command line.

example

It is a slightly different medicine for a slightly different problem. iTermoxyl was inspired by itermocil, which was inspired by teamocil.

iTermoxyl is designed to be simple to use, with minimal interaction needed to get it running. Once ssh connections are established, use iTerm2's broadcast input feature to send commands to all machines at once (Shell -> Broadcast input or simply cmd + shift + i).

Features:

  • magically learns about existing hosts by reading from ~/.ssh/config
  • doesn't require any configuration files (tools like itermocil and i2cssh require you to manually create YAML descriptions of your environments)
  • smart pattern matching: no need to type in the name of each machine you want to connect to
  • supports ssh config Include directives
  • supports loose matches (see below)

How to install

cd to the destination directory and:

curl -O https://raw.githubusercontent.com/luciopaiva/itermoxyl/master/itermoxyl
chmod u+x itermoxyl

Make sure to add it to the path so it can be called anywhere.

You need to have iTerm2 installed (obviously), but nothing else. The script is written in Python 2.7 and your macOS already comes with it.

How to use

Consider the following sample ~/.ssh/config file:

Host foo-1
    HostName 10.0.0.1
Host foo-2
    HostName 10.0.0.2
Host foo-3
    HostName 10.0.0.3
Host foo-4
    HostName 10.0.0.4
Host foo-5
    HostName 10.0.0.5

Host bar-1
    HostName 10.0.1.1
Host bar-2
    HostName 10.0.1.2

Host server-1-a
    HostName 192.168.0.1
Host server-1-b
    HostName 192.168.0.2
Host server-2-a
    HostName 192.168.1.1
Host server-2-b
    HostName 192.168.1.2

Let's try a few combinations. To open:

  1. all foo machines:

    itermoxyl foo
    
  2. foo-2 and bar-2:

    itermoxyl foo,bar 2
    
  3. server-1-a and server-2-a:

    itermoxyl server a
    
  4. foo-1, foo-2, foo-3:

    itermoxyl foo 1-3
    
  5. foo-1, foo-3, foo-4, foo-5:

    itermoxyl foo 1,3-5
    

The underlying pattern-matching algorithm

The general rule is:

itermoxyl TERM_0 TERM_1 ... TERM_N

Terms will be joined into the following regular expression:

(?:TERM_0).*?(?:TERM_1).*?(?:TERM_N)

Which acts as a kind of a loose search. Moreover, each term is treated as a potential comma-separated list of names:

TERM => NAME_0,NAME_1,...,NAME_N

Which will get converted to the following regular expression:

(?:NAME_0|NAME_1|NAME_N)

This allows for matches like the foo,bar in the example above.

Finally, the last term in a list with more than one term is treated differently. If its name parts are actually numbers, they will exceptionally be translated into the following regular expression:

(?<!\d)(?:NUMBER_0|NUMBER_1|NUMBER_N)$

This was implemented to match specific machine numbers. We usually have a series of machines sharing a same prefix and only differing by the number at the end. Even if the common prefix has numbers in them, we almost never want to match those numbers, only the ones at the very end. That's why this special regular expression has the $ at the end. Moreover, if we specify number 2, we usually want to match only 2 and not 12; that's why the expression has a negative lookbehind making sure that no \d precedes our intended number.

Well, there's one last bit. Each NUMBER in the pattern above can actually be a range; for instance, 1-5. iTermoxyl will try and match ranges, expanding them accordingly. So if the last term passed in the command line is 1,3-5,8-10,12, the resulting expanded version will be 1,3,4,5,8,9,10,12.

The script will always show all hosts matching your query and ask for confirmation before actually connecting to them.