lib/string_ext.rb in ssh_scan-0.0.11 vs lib/string_ext.rb in ssh_scan-0.0.12.beta.1
- old
+ new
@@ -1,7 +1,8 @@
require 'ipaddr'
require 'resolv'
+require 'timeout'
# Extend string to include some helpful stuff
class String
def unhexify
[self].pack("H*")
@@ -21,33 +22,46 @@
end
return true
end
- def resolve_fqdn_as_ipv6
- Resolv::DNS.open do |dns|
- ress = dns.getresources self, Resolv::DNS::Resource::IN::AAAA
- temp = ress.map { |r| r.address }
- return temp[0]
+ def resolve_fqdn_as_ipv6(timeout = 3)
+ begin
+ Timeout::timeout(timeout) {
+ Resolv::DNS.open do |dns|
+ ress = dns.getresources self, Resolv::DNS::Resource::IN::AAAA
+ temp = ress.map { |r| r.address }
+ return temp[0]
+ end
+ }
+ rescue Timeout::Error
+ return ""
end
end
- def resolve_fqdn_as_ipv4
- Resolv::DNS.open do |dns|
- ress = dns.getresources self, Resolv::DNS::Resource::IN::A
- temp = ress.map { |r| r.address }
- return temp[0]
+ def resolve_fqdn_as_ipv4(timeout = 3)
+ begin
+ Timeout::timeout(timeout) {
+ Resolv::DNS.open do |dns|
+ ress = dns.getresources self, Resolv::DNS::Resource::IN::A
+ temp = ress.map { |r| r.address }
+ return temp[0]
+ end
+ }
+ rescue Timeout::Error
+ return ""
end
+
end
def resolve_fqdn
@fqdn ||= TCPSocket.gethostbyname(self)[3]
end
def fqdn?
begin
resolve_fqdn
- rescue SocketError
+ rescue SocketError, Timeout::Error
return false
end
if ip_addr?
return false