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. -
.is_valid_port?(port) ⇒ Boolean
Return true if
port
is a valid port, 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.
75 76 77 78 79 |
# File 'lib/bettercap/network/validator.rb', line 75 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.
65 66 67 68 69 70 71 72 |
# File 'lib/bettercap/network/validator.rb', line 65 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.
40 41 42 43 44 |
# File 'lib/bettercap/network/validator.rb', line 40 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.
90 91 92 |
# File 'lib/bettercap/network/validator.rb', line 90 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.
82 83 84 85 86 87 |
# File 'lib/bettercap/network/validator.rb', line 82 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.
47 48 49 50 51 52 |
# File 'lib/bettercap/network/validator.rb', line 47 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 |
.is_valid_port?(port) ⇒ Boolean
Return true if port
is a valid port, otherwise false.
31 32 33 34 35 36 37 |
# File 'lib/bettercap/network/validator.rb', line 31 def self.is_valid_port?(port) port ||= "" return false if port.strip.empty? return false unless port =~ /^[0-9]+$/ port = port.to_i return ( port > 0 and port <= 65535 ) end |
.parse_range(r) ⇒ Object
Parse r
as an IP range and return the first and last IP.
55 56 57 58 59 60 61 62 |
# File 'lib/bettercap/network/validator.rb', line 55 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 |