Skip to content

Instantly share code, notes, and snippets.

@trevoro
Created April 5, 2010 21:09
Show Gist options
  • Save trevoro/356896 to your computer and use it in GitHub Desktop.
Save trevoro/356896 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# List all network interfaces using the ifconfig tool
# Returns an array of interface names
def list_interfaces
get_interfaces.keys
end
# Get all interfaces details. Returns a hash
# Its cheaper to get all interfaces at once and then parse through for which one you want rather than
# do multiple ifconfig runs.
def get_interfaces
`ifconfig -a | grep Ethernet`.split("\n").inject({}) do |result,iface|
result["#{iface.split[0]}"] = {'mac_address' => "#{iface.split[4]}"}
result
end
end
# Get all interface card details from ethtool
def ethtool(interface = 'eth0')
output = `ethtool #{interface}`
output.gsub!(/\n\t\s+/,'')
output.scan(/^\t(.*):\s*(.*)/).inject({}) do |result,element|
result[element.first.downcase.gsub(/(\s|-)/,'_')] = element.last
result
end
end
# List all bridge names. No details. Returns an array
def list_bridges
get_bridges.keys
end
# Get all bridges and put the results into a hash, with the keys being the first elements in the output
def get_bridges
`brctl show`.split("\n").inject({}) do |result,element|
b = element.split(/\t+/)
result[b[0]] = {'bridge_id' => b[1], 'stp_enabled' => b[2], 'interfaces' => b[3] || ''} unless b[0] == 'bridge name'
result
end
end
# Create a new bridge with some name...
def create_bridge(name)
# this is where we'll have to start using open4 to catch error codes, stderr, etc.
`brctl addbr #{name}`.split("\n") do |l|
p "DEBUG: #{l}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment