Skip to content
Browse files

Added more network framework methods to get all

interfaces and an IP for specific interface.
  • Loading branch information...
1 parent b758c06 commit 64e7b0d47fb1b4823127fbcf072c5679447a301d @ErebusBat committed May 25, 2011
Showing with 66 additions and 7 deletions.
  1. +18 −0 current_ips.rb
  2. +19 −0 docs/interface_info.md
  3. +29 −7 framework/network.rb
View
18 current_ips.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby -I.
+
+require "framework/colors"
+require "framework/network"
+
+ips = []
+current_ips = Network.ips
+current_ips.each do |ip|
+ show_ip = true
+ #show_ip = false if Network.ip_matches_pattern ip,
+ ips << ip
+end
+
+# Print them
+puts "#{Colors.white}Current IPs#{Colors.normal}"
+ips.each do |ip|
+ puts " #{ip}"
+end
View
19 docs/interface_info.md
@@ -0,0 +1,19 @@
+
+Joins to eat the rest of the line, and start the next:
+ .*$\s+
+
+Interface:
+ ^(\w+\d+):
+
+Mac Addr, ether or fw:
+ (?:ether|lladdr)\s+((?:[a-z0-9]{2}:?)+)
+
+IPv4 Addr:
+ (?:inet)\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})
+
+Link Status:
+ (?:status):\s+(\w+)
+
+Return just interfaces:
+ ^(\w+\d+):.*\s(?:^\s+.*\s)+
+
View
36 framework/network.rb
@@ -7,6 +7,23 @@ def self.ips
ips
end
+ def self.interface_list
+ interfaces = []
+ rx = /^(\w+\d+):.*\s(?:^\s+.*\s)+/i
+ ifconfig = %x[ifconfig]
+ m = ifconfig.scan rx
+ m.each { |iif| interfaces << iif[0] }
+ interfaces
+ end
+
+ def self.ip_for_interface iif
+ rx = /(?:inet)\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/i
+ ifconfig = %x[ifconfig #{iif}]
+ m = rx.match ifconfig
+ return nil unless m
+ m[1]
+ end
+
def self.default_gateway
routes = %x[netstat -nr]
rx = /^default\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i
@@ -30,20 +47,25 @@ def self.site_up? host, timeout = 1000
end
def self.on_network network
+ ips.each do |ip|
+ return true if ip_matches_pattern ip, network
+ end
+
+ # nope :(
+ false
+ end
+
+ def self.ip_matches_pattern ip, pattern
# Simple test to identify if you are on the specified network
# at the end it will turn the network specified into a ^REGEX
#
# Can use * wildcard which will translate to \d{1,3} in regex parlance
- rx_str = "^#{network}"
+ rx_str = "^#{pattern}"
rx_str.gsub! '*', '\d{1,3}'
rx_str.gsub! '.', '\.'
rx = Regexp.new rx_str
- ips.each do |ip|
- return true if rx.match ip
- end
-
- # nope :(
+ return true if rx.match ip
false
end
-end
+end

0 comments on commit 64e7b0d

Please sign in to comment.
Something went wrong with that request. Please try again.