class Ronin::CLI::Commands::Ip

Queries or processes IP addresses.

## Usage

ronin ip [options] [IP ... | --public | --local]

## Options

-f, --file FILE                  Optional file to read values from
-P, --public                     Gets the machine's public IP address
-L, --local                      Gets the machine's local IP address
-r, --reverse                    Prints the IP address in reverse name format
-X, --hex                        Converts the IP address to hexadecimal format
-D, --decimal                    Converts the IP address to decimal format
-O, --octal                      Converts the IP address to octal format
-B, --binary                     Converts the IP address to binary format
    --hex-octet                  Converts the IP address to hexadecimal format by octet
    --octal-octet                Converts the IP address to octal format by octet
    --ipv6-compat                Converts the IPv4 address to an IPv6 compatible address
    --ipv6-expanded              Expands a shortened or compressed IPv6 address
-C, --cidr NETMASK               Converts the IP address into a CIDR range
-H, --host                       Converts the IP address to a host name
-p, --port PORT                  Appends the port number to each IP
-U, --uri                        Converts the IP address into a URI
    --uri-scheme SCHEME          The scheme for the URI (Default: http)
    --uri-port PORT              The port for the URI
    --uri-path /PATH             The path for the URI (Default: /)
    --uri-query STRING           The query string for the URI
    --http                       Converts the IP address into a http:// URI
    --https                      Converts the IP address into a https:// URI
-h, --help                       Print help information

## Arguments

[IP ...]                         The IP address(es) to process

## Examples

ronin ip --public
ronin ip --local
ronin ip --decimal 1.2.3.4
ronin ip --cidr 20 1.2.3.4
ronin ip --host 192.30.255.113

Public Instance Methods

format_ip(ip) click to toggle source

Formats an IP address.

@param [Ronin::Support::Network::IP] ip

The IP address to format.

@return [String]

The formatted IP address.
# File lib/ronin/cli/commands/ip.rb, line 242
def format_ip(ip)
  if ip.ipv4?
    format_ipv4(ip)
  else
    format_ipv6(ip)
  end
end
process_value(ip) click to toggle source

Processes an individual IP address.

@param [String] ip

The IP address string to process.
# File lib/ronin/cli/commands/ip.rb, line 205
def process_value(ip)
  ip = Support::Network::IP.new(ip)

  if options[:reverse]
    puts ip.reverse
  elsif options[:cidr]
    ip = Support::Network::IP.new("#{ip}/#{options[:cidr]}")

    puts "#{ip}/#{ip.prefix}"
  elsif options[:host]
    if (host = ip.host)
      puts host
    end
  elsif options[:port]
    puts "#{format_ip(ip)}:#{options[:port]}"
  elsif options[:uri]
    puts URI::Generic.build(
      scheme: options[:uri_scheme],
      host:   format_ip(ip),
      port:   options[:uri_port],
      path:   options[:uri_path],
      query:  options[:uri_query]
    )
  else
    puts format_ip(ip)
  end
end
run(*ips) click to toggle source

Runs the ‘ronin ip` command.

@param [Array<String>] ips

Additional IP arguments to process.
Calls superclass method
# File lib/ronin/cli/commands/ip.rb, line 184
def run(*ips)
  if options[:public]
    if (address = Support::Network::IP.public_address)
      puts address
    else
      print_error 'failed to lookup public IP address using https://ipinfo.io/ip'
      exit(1)
    end
  elsif options[:local]
    puts Support::Network::IP.local_address
  else
    super(*ips)
  end
end

Private Instance Methods

format_ipv4(ip) click to toggle source

Formats an IPv4 address.

@param [Ronin::Support::Network::IP] ip

The IP address to format.

@return [String]

The formatted IP address.
# File lib/ronin/cli/commands/ip.rb, line 261
def format_ipv4(ip)
  if options[:hex]
    "0x%x" % ip.to_i
  elsif options[:hex_octet]
    ipv4_hex_octet(ip)
  elsif options[:decimal]
    "%u" % ip.to_i
  elsif options[:octal]
    "0%o" % ip.to_i
  elsif options[:octal_octet]
    ip.to_s.split(".").map { |octet| "0%o" % octet.to_i }.join(".")
  elsif options[:binary]
    "%b" % ip.to_i
  elsif options[:ipv6_compat]
    ip.ipv4_mapped.to_s
  elsif options[:ipv6_expanded]
    print_error "called with --ipv6-expanded for #{ip}"
    exit(1)
  else
    ip.to_s
  end
end
format_ipv6(ip) click to toggle source

Formats an IPv6 address.

@param [Ronin::Support::Network::IP] ip

The IP address to format.

@return [String]

The formatted IP address.
# File lib/ronin/cli/commands/ip.rb, line 293
def format_ipv6(ip)
  if options[:decimal]
    "%u" % ip.to_i
  elsif options[:hex]
    "0x%x" % ip.to_i
  elsif options[:octal]
    "0%o" % ip.to_i
  elsif options[:octal_octet]
    print_error "called with --octal-octet for #{ip}"
    exit(1)
  elsif options[:hex_octet]
    if ip.ipv4_mapped?
      "::ffff:#{ipv4_hex_octet(ip.ipv4)}"
    else
      print_error "called with --hex-octet for #{ip}"
      exit(1)
    end
  elsif options[:binary]
    "%b" % ip.to_i
  elsif options[:ipv6_expanded]
    ip.canonical
  elsif options[:ipv6_compat]
    print_error "called with --ipv6-compat for #{ip}"
    exit(1)
  else
    ip.to_s
  end
end
ipv4_hex_octet(ip) click to toggle source

Converts the octets of an IP address to hex

@param [Ronin::Support::Network::IP] ip

The IP address to convert.

@return [String]

The formatted IP address.
# File lib/ronin/cli/commands/ip.rb, line 331
def ipv4_hex_octet(ip)
  ip_uint = ip.to_i

  format(
    "%<octet1>x.%<octet2>x.%<octet3>x.%<octet4>x",
    octet1: (ip_uint & 0xff000000) >> 24,
    octet2: (ip_uint & 0xff0000) >> 16,
    octet3: (ip_uint & 0xff00) >> 8,
    octet4: (ip_uint & 0xff)
  )
end