Skip to content

Instantly share code, notes, and snippets.

@Aaron2Ti
Forked from mbbx6spp/README.md
Created December 3, 2018 22:14
Show Gist options
  • Save Aaron2Ti/9e72d4bb59f744990f437e9392a6a361 to your computer and use it in GitHub Desktop.
Save Aaron2Ti/9e72d4bb59f744990f437e9392a6a361 to your computer and use it in GitHub Desktop.
Doing DNS SRV lookups in Ruby using stdlib in 2.3.0

DNS SRV Lookups in Ruby

Prerequisites

  • Nix 1.11+ OR Ruby 2.3.x

If using Nix you can just load the environment with nix-shell shell.nix.

Now we can test an entry via command-line using host or dig commands:

[nix-shell:~/src/github/mbbx6spp/ruby-dns-examples]$ host -tSRV _http._tcp.referentiallabs.rocks
_http._tcp.referentiallabs.rocks has SRV record 10 50 80 fake0.referentiallabs.rocks.
_http._tcp.referentiallabs.rocks has SRV record 10 50 80 fake1.referentiallabs.rocks.

Look at the code in srvexample0.rb for Ruby code, and you can run it like so here:

[nix-shell:~/src/github/mbbx6spp/ruby-dns-examples]$ ruby srvexample0.rb
_http._tcp.referentiallabs.rocks has SRV record 10 50 80 fake1.referentiallabs.rocks.
_http._tcp.referentiallabs.rocks has SRV record 10 50 80 fake0.referentiallabs.rocks.

Cheers.

{ pkgs ? import <nixpkgs> {}
, ... }:
let
inherit (pkgs) runCommand ruby;
in runCommand "dummy" {
buildInputs = [
ruby
];
} ""
require 'resolv'
class Resolv::DNS::Resource::IN::SRV
def to_s
"SRV #{priority} #{weight} #{target} #{port}"
end
end
opts = { nameserver: ['8.8.4.4', '8.8.8.8'] }
domain = "referentiallabs.rocks"
resolver = Resolv::DNS.new(opts)
puts resolver.getresource("_http._tcp.#{domain}", Resolv::DNS::Resource::IN::SRV).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment