Class: Snackhack2::IpLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/snackhack2/iplookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_save: false) ⇒ IpLookup

Returns a new instance of IpLookup.



10
11
12
13
# File 'lib/snackhack2/iplookup.rb', line 10

def initialize(file_save: false)
  @file_save = file_save
  # Clean the URL immediately to avoid repeating .gsub everywhere
end

Instance Attribute Details

#site ⇒ Object

Returns the value of attribute site.



8
9
10
# File 'lib/snackhack2/iplookup.rb', line 8

def site
  @site
end

Instance Method Details

#get_ip ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/snackhack2/iplookup.rb', line 27

def get_ip
  # Uses ping command to get IP of the host
  ips = []
  output = `ping -c 2 #{@site}`
  # Using scan to find all IP-like patterns
  output.scan(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/).each do |match|
    ip = match.first
    ips << ip unless ips.include?(ip)
  end
  ips
end

#nslookup ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/snackhack2/iplookup.rb', line 39

def nslookup
  # Uses the nslookup command to extract the IP
  output = `nslookup #{@site}`
  # Simplified extraction using regex for "Address: [IP]"
  ips = output.scan(/Address: (\d{1,3}(?:\.\d{1,3}){3})/).flatten.uniq

  if @file_save
    # Note: Ensure Snackhack2.file_save is defined in your main module
    Snackhack2.file_save(@site, 'ip_lookup', ips.join("\n"))
  else
    ips
  end
end

#run ⇒ Object



19
20
21
22
23
24
25
# File 'lib/snackhack2/iplookup.rb', line 19

def run
  # Running all methods and displaying results
  puts "Methods for: #{@site}".bold.blue
  puts "Ping:   #{get_ip.join(', ')}"
  puts "NS:     #{nslookup.is_a?(Array) ? nslookup.join(', ') : 'Saved to file'}"
  puts "Socket: #{socket_lookup}"
end

#socket_lookup ⇒ Object



53
54
55
56
57
# File 'lib/snackhack2/iplookup.rb', line 53

def socket_lookup
  IPSocket.getaddress(@site)
rescue SocketError
  "Could not resolve"
end