I have this code here:
#!/usr/bin/env ruby
require 'resolv'
require 'socket'
class Resolver
def initialize(hostname = nil)
@hostname = hostname
end
def is_host?(reg)
@hostname =~ /\w\.[a-zA-Z]{2,3}/
end
def is_ip?(reg)
@hostname =~ /(?:\d{1,3}\.){3}\d{1,3}/
end
def main
hosts = []
ARGV.empty? ? printf("Missing arguments!\n") : hosts << @hostname
begin
hosts.each do |r|
if is_host?(r)
puts Socket.getaddrinfo(r, nil)[0][2]
elsif is_ip?(r)
puts Resolv.new.getname(r)
end
end
rescue Resolv::ResolvError
puts "Bad IP address!"
rescue SocketError
puts "Bad hostname!"
end
end
end
res = Resolver.new(ARGV[0])
res.main
It is working just fine. What it does is accept one argument, and based on the formatting of the argument it's does an appropriate lookup. So for example, if I type: ./dnslookup.rb evilzone.org, then it returns the IP address of evilzone.org. On the other hand, if I type:
./dnslookup.rb 5.9.107.151, then it returns the domain name.
The code works just fine. I'm mainly just looking for input regarding how well it's been coded, and if it could be coded more efficiently, or utilize better methods. I've grown rather unsatisfied with my code recently and am trying to make more interesting, more dynamic, and basically more mature code. Too often I feel like I'm just using a ton of if statements to do the heavy lifting, and while it works, it's unsatisfying. So any input is appreciated.