Class: ToolsNet
Class Method Summary collapse
-
.doreq(restclient_obj, path, method, method_opts: {}, validate_opts: {}, retry_opts: {}, show_progress: false) ⇒ Object
Do the request, validate and decode response and do the retries if needed.
- .get_current_ip ⇒ Object
-
.is_backend?(original_addr) ⇒ Boolean
Validate.: ip number is a backend?.
-
.ping?(host) ⇒ Boolean
Test a host.
-
.resolv_dns(domain) ⇒ String
Resolv a dns to a ip.
-
.resolv_ip_name(ip) ⇒ String
Resolv a ip to a dns.
- .valid_ip?(original_addr) ⇒ Boolean
-
.valid_network?(original_addr) ⇒ Boolean
Validate.: ip number is avalid network.
-
.valid_port?(port) ⇒ Boolean
Validate.: port number between valid range?.
-
.validate_decode_response(response, request, result, validate_opts) ⇒ Object type
Return a valid decode response.
-
.validate_ipaddress(that) ⇒ Hash
Validate.: ip number or mask are valids?.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ToolsNet
constructor
A new instance of ToolsNet.
Constructor Details
#initialize(options = {}) ⇒ ToolsNet
Returns a new instance of ToolsNet.
5 |
# File 'lib/lib/net.rb', line 5 def initialize( = {}); end |
Class Method Details
.doreq(restclient_obj, path, method, method_opts: {}, validate_opts: {}, retry_opts: {}, show_progress: false) ⇒ Object
Do the request, validate and decode response and do the retries if needed
-
restclient_obj: initialized RestClient object
-
path: URL path to request
-
method: symbol for HTTP Method to request
-
method_opts: options as passed to a RestClient call
-
validate_opts: options for response validation as used with
validate_decode_response()
-
retry_opts: when_res: [nil, [], {}, ‘str’], when_code: [404, 500],
conditionals to retry even if no exceptions were catched
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/lib/net.rb', line 73 def self.doreq(restclient_obj, path, method, method_opts: {}, validate_opts: {}, retry_opts: {}, show_progress: false) res = nil code = nil data = method_opts.fetch(:data, nil) method_opts.delete(:data) # Retry loop due to: # - 404 from Router API right after applying patch over a target # - Intermittent connection reset from Manager API (HTTPS) retries = retry_opts.fetch(:attempts, 10) 1.upto(retries) do |try| flag_error = false # The request begin restclient_obj[path].send(method, *data, **method_opts) do |response, request, result| res = validate_decode_response(response, request, result, validate_opts) code = result.code.to_i end rescue Exception => e flag_error = true end # Other conditionals to retry unless retry_opts.empty? if retry_opts.key?(:when_res) flag_error = true if retry_opts[:when_res].include?(res) end if retry_opts.key?(:when_code) flag_error = true if retry_opts[:when_code].include?(code) end end return res unless flag_error # got response, break the loop doReqMSG = format '%d/%d', try, retries ap doReqMSG if show_progress if try >= retries ap doreq_code return nil else sleep 1 # wait before next end end end |
.get_current_ip ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/lib/net.rb', line 17 def self.get_current_ip ip = `ifconfig | grep -E '10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`.split("\n").first unless valid_ip? ip ip = `ifconfig | grep -E '192\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`.split("\n").first end ip = ip.split("\n").first if ip.include? "\n" return ip if IPAddress.valid?(ip) end |
.is_backend?(original_addr) ⇒ Boolean
Validate.: ip number is a backend?.
213 214 215 216 |
# File 'lib/lib/net.rb', line 213 def self.is_backend?(original_addr) backend = NetAddr::IPv4Net.parse '10.0.0.0/8' backend.contains (NetAddr::IPv4.parse original_addr) end |
.ping?(host) ⇒ Boolean
Test a host.
Net::Ping::External.new(host || ‘0.0.0.1000’, timeout = 1).ping?
13 14 15 |
# File 'lib/lib/net.rb', line 13 def self.ping?(host) Net::Ping::External.new(host || '0.0.0.1000', 1).ping? end |
.resolv_dns(domain) ⇒ String
Resolv a dns to a ip.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lib/net.rb', line 45 def self.resolv_dns(domain) dns = Dnsruby::DNS.new dns.getaddress(domain).to_s rescue Exception => e case e. when 'Dnsruby::NXDomain' then nil else e. end end |
.resolv_ip_name(ip) ⇒ String
Resolv a ip to a dns.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lib/net.rb', line 30 def self.resolv_ip_name(ip) ret = Resolv.new.getname(ip) ret.instance_variable_get('@labels').join('.') rescue Exception => e case e. when 'Dnsruby::NXDomain' then nil else e. end end |
.valid_ip?(original_addr) ⇒ Boolean
218 219 220 221 222 223 224 225 226 |
# File 'lib/lib/net.rb', line 218 def self.valid_ip?(original_addr) status = false begin status = IPAddress.valid?(original_addr) rescue Exception => e e end status end |
.valid_network?(original_addr) ⇒ Boolean
Validate.: ip number is avalid network.
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/lib/net.rb', line 232 def self.valid_network?(original_addr) status = false begin ip = IPAddress original_addr status = true if ip.network? rescue Exception => e e end status end |
.valid_port?(port) ⇒ Boolean
Validate.: port number between valid range?.
154 155 156 157 158 159 |
# File 'lib/lib/net.rb', line 154 def self.valid_port?(port) return false if port.to_s.strip.match(/^\d{1,5}(?!\d)$/).nil? return false unless port.to_i.between?(1, 65_535) true end |
.validate_decode_response(response, request, result, validate_opts) ⇒ Object type
Return a valid decode response.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lib/net.rb', line 123 def self.validate_decode_response(response, request, result, validate_opts) if validate_opts.key? :expected expected = validate_opts[:expected] case expected.class.name when 'Array' if validate_opts[:expected].include? response return true else return false end when 'Proc' response = expected.call(response, request, result) return response when 'String' return result if validate_opts[:expected].eql? 'result' return request if validate_opts[:expected].eql? 'request' return response if validate_opts[:expected].eql? 'response' else ap expected.class.name return response end else return response end true end |
.validate_ipaddress(that) ⇒ Hash
Validate.: ip number or mask are valids?.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/lib/net.rb', line 165 def self.validate_ipaddress(that) result = {} result[:status] = false result[:type] = '' result[:msg] = '' result[:address] = that result[:resolv] = '' if IPAddress.valid_ipv4? that begin ip = IPAddress.valid_ipv4? that result[:status] = true result[:type] = 'ip' result[:oct1] = that.split('.')[0] result[:oct2] = that.split('.')[1] result[:oct3] = that.split('.')[2] result[:oct4] = that.split('.')[3] rescue Exception => e result[:msg] = e.to_s return end else begin net = NetAddr::IPv4Net.parse that result[:type] = 'mask' result[:status] = true rescue Exception => e result[:type] = '' result[:status] = false # ip = resolv_dns that # if IPAddress::valid_ipv4? ip # result[:status] = true # result[:type] = 'ip' # result[:oct1] = ip.split('.')[0] # result[:oct2] = ip.split('.')[1] # result[:oct3] = ip.split('.')[2] # result[:oct4] = ip.split('.')[3] # else # result[:msg] = e.to_s # end end end result end |