Class: BetterCap::Network::Validator
- Inherits:
-
Object
- Object
- BetterCap::Network::Validator
- Defined in:
- lib/bettercap/network/validator.rb
Overview
Simple class to perform validation of various addresses, ranges, etc.
Constant Summary collapse
- IP_ADDRESS_REGEX =
Basic expression to validate an IP address.
'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})'
- IP_OCTECT_MAX =
Quite self explainatory :)
255
Class Method Summary collapse
-
.each_in_netmask(m) ⇒ Object
Parse
m
as a netmask and yields each address in it. -
.each_in_range(r) ⇒ Object
Parse
r
as an IP range and yields each address in it. -
.each_ip(data) ⇒ Object
Extract valid IP addresses from
data
and yields each one of them. -
.is_ip?(ip) ⇒ Boolean
Return true if
ip
is a valid IP address, otherwise false. -
.is_mac?(mac) ⇒ Boolean
Return true if
mac
is a valid MAC address, otherwise false. -
.is_netmask?(n) ⇒ Boolean
Return true if
n
is a valid IP netmask range ( 192.168.1.1/24 ), otherwise false. -
.is_range?(r) ⇒ Boolean
Return true if
r
is a valid IP address range ( 192.168.1.1-93 ), otherwise false. -
.parse_range(r) ⇒ Object
Parse
r
as an IP range and return the first and last IP.
Class Method Details
.each_in_netmask(m) ⇒ Object
Parse m
as a netmask and yields each address in it.
66 67 68 69 70 |
# File 'lib/bettercap/network/validator.rb', line 66 def self.each_in_netmask(m) IPAddr.new(m).to_range.each do |o| yield o.to_s end end |
.each_in_range(r) ⇒ Object
Parse r
as an IP range and yields each address in it.
56 57 58 59 60 61 62 63 |
# File 'lib/bettercap/network/validator.rb', line 56 def self.each_in_range(r) first, last = self.parse_range(r) loop do yield first.to_s break if first == last first = first.succ end end |
.each_ip(data) ⇒ Object
Extract valid IP addresses from data
and yields each one of them.
31 32 33 34 35 |
# File 'lib/bettercap/network/validator.rb', line 31 def self.each_ip(data) data.scan(/(#{IP_ADDRESS_REGEX})/).each do |m| yield( m[0] ) if m[0] != '0.0.0.0' end end |
.is_ip?(ip) ⇒ Boolean
Return true if ip
is a valid IP address, otherwise false.
23 24 25 26 27 28 |
# File 'lib/bettercap/network/validator.rb', line 23 def self.is_ip?(ip) if /\A#{IP_ADDRESS_REGEX}\Z/ =~ ip.to_s return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX } end false end |
.is_mac?(mac) ⇒ Boolean
Return true if mac
is a valid MAC address, otherwise false.
81 82 83 |
# File 'lib/bettercap/network/validator.rb', line 81 def self.is_mac?(mac) ( /^[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}$/i =~ mac.to_s ) end |
.is_netmask?(n) ⇒ Boolean
Return true if n
is a valid IP netmask range ( 192.168.1.1/24 ), otherwise false.
73 74 75 76 77 78 |
# File 'lib/bettercap/network/validator.rb', line 73 def self.is_netmask?(n) if /\A#{IP_ADDRESS_REGEX}\/(\d+)\Z/ =~ n.to_s return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX } end false end |
.is_range?(r) ⇒ Boolean
Return true if r
is a valid IP address range ( 192.168.1.1-93 ), otherwise false.
38 39 40 41 42 43 |
# File 'lib/bettercap/network/validator.rb', line 38 def self.is_range?(r) if /\A#{IP_ADDRESS_REGEX}\-(\d{1,3})\Z/ =~ r.to_s return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX } end false end |
.parse_range(r) ⇒ Object
Parse r
as an IP range and return the first and last IP.
46 47 48 49 50 51 52 53 |
# File 'lib/bettercap/network/validator.rb', line 46 def self.parse_range(r) first, last_part = r.split('-') last = first.split('.')[0..2].join('.') + ".#{last_part}" first = IPAddr.new(first) last = IPAddr.new(last) [ first, last ] end |