host_range

Make lists of hosts using ranges like:

  • foo-.domain.com

  • bar-[1-3,6-9].domain.com

Library Usage

Get an array of hostname from a range:

require 'host_range'
HostRange.parse 'foo-[01-03].domain.com'
 => ["foo-01.domain.com", "foo-02.domain.com", "foo-03.domain.com"]

Patterns

Ranges are parsed using the ruby ‘..’ operator:

HostRange.parse 'foo-[a-c]'
 => ["foo-a", "foo-b", "foo-c"]

Multiple atoms and ranges may be separated by commas:

HostRange.parse 'foo-[1,3-5,9]'
 => ["foo-1", "foo-3", "foo-4", "foo-5", "foo-9"]

With no commas or range operators (‘-’), atoms are parsed as single characters:

HostRange.parse 'foo-[123]'
 => ["foo-1", "foo-2", "foo-3"]

Combined:

HostRange.parse 'foo-[ab][01-02]'
 => ["foo-a01", "foo-a02", "foo-b01", "foo-b02"]

Command-line Usage

Commandline tool lists hosts, or execs commands on them in a loop:

Get help:

$ hr -h

List some hosts:

$ hr foo-[01-03].domain.com
foo-01.domain.com
foo-02.domain.com
foo-03.domain.com

Get some IP addresses:

$ hr foo-[01-03].domain.com -e host
foo-01.domain.com has address 172.16.0.1
foo-02.domain.com has address 172.16.0.2
foo-03.domain.com has address 172.16.0.3

Run a command, ‘{}’ is replaced with each hostname:

$ hr foo-[01-03].domain.com -e ssh root@{} uptime

You can provide a config file (~/.hr.yaml) with predefined patterns with project names:

web:
  - webfe-[ab][01-05].newco.com
  - webfe-c[20-23,27-29].newco.com
dev:
  - dev-[web,db]-[01-02].newco.com

and then call project by name:

$ hosts dev
dev-web-01.newco.com
dev-web-02.newco.com
dev-db-01.newco.com
dev-db-02.newco.com

Copyright © 2011 Richard Lister. See LICENSE.txt for further details.