examples: add a few examples in ruby
This commit is contained in:
@@ -744,6 +744,7 @@ docs/libnm-util/Makefile
|
|||||||
NetworkManager.pc
|
NetworkManager.pc
|
||||||
examples/Makefile
|
examples/Makefile
|
||||||
examples/python/Makefile
|
examples/python/Makefile
|
||||||
|
examples/ruby/Makefile
|
||||||
examples/C/Makefile
|
examples/C/Makefile
|
||||||
examples/C/glib/Makefile
|
examples/C/glib/Makefile
|
||||||
examples/C/qt/Makefile
|
examples/C/qt/Makefile
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
SUBDIRS= \
|
SUBDIRS= \
|
||||||
python \
|
python \
|
||||||
|
ruby \
|
||||||
C
|
C
|
||||||
|
4
examples/ruby/Makefile.am
Normal file
4
examples/ruby/Makefile.am
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
EXTRA_DIST = \
|
||||||
|
add-connection.rb \
|
||||||
|
get-basic-nm-info.rb \
|
||||||
|
list-devices.rb
|
86
examples/ruby/add-connection.rb
Executable file
86
examples/ruby/add-connection.rb
Executable file
@@ -0,0 +1,86 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# vim: ft=ruby ts=2 sts=2 sw=2 et ai
|
||||||
|
# -*- Mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along
|
||||||
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2011 Red Hat, Inc.
|
||||||
|
#
|
||||||
|
|
||||||
|
require 'dbus'
|
||||||
|
require 'ipaddr'
|
||||||
|
|
||||||
|
#
|
||||||
|
# This example adds a new ethernet connection via Addconnection() D-Bus call.
|
||||||
|
# It also shows how to specify D-Bus signature for properties like "addresses"
|
||||||
|
# and "clone-mac-address".
|
||||||
|
#
|
||||||
|
# Configuration settings are described here:
|
||||||
|
# http://projects.gnome.org/NetworkManager/developers/api/09/ref-settings.html
|
||||||
|
#
|
||||||
|
|
||||||
|
# Helper functions
|
||||||
|
def ip_to_int(ip_addr)
|
||||||
|
return IPAddr.new(ip_addr).hton.unpack('L').first
|
||||||
|
end
|
||||||
|
|
||||||
|
def rand_hex_3(l)
|
||||||
|
"%0#{l}x" % rand(1 << l*4)
|
||||||
|
end
|
||||||
|
|
||||||
|
def rand_uuid
|
||||||
|
[8,4,4,4,12].map {|n| rand_hex_3(n)}.join('-')
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# Create new connection settings
|
||||||
|
s_con = {
|
||||||
|
"type" => "802-3-ethernet",
|
||||||
|
"uuid"=> rand_uuid,
|
||||||
|
"id" => "__MyConnection__"
|
||||||
|
}
|
||||||
|
|
||||||
|
s_wired = { "cloned-mac-address" => ["ay", [0x00, 0x22, 0x68, 0x01, 0x02, 0x03]]}
|
||||||
|
|
||||||
|
ip1 = ip_to_int("192.168.1.12")
|
||||||
|
ip2 = ip_to_int("192.168.1.13")
|
||||||
|
gw1 = ip_to_int("192.168.1.1")
|
||||||
|
ip3 = ip_to_int("10.0.2.5")
|
||||||
|
gw2 = ip_to_int("10.0.2.254")
|
||||||
|
dns1 = ip_to_int("8.8.8.8")
|
||||||
|
dns2 = ip_to_int("8.8.4.4")
|
||||||
|
|
||||||
|
s_ip4 = {
|
||||||
|
"addresses"=> ["aau", [[ip1, 24, gw1], [ip2, 24, gw1], [ip3, 24, gw2]]],
|
||||||
|
"method"=>["s", "manual"],
|
||||||
|
"dns"=> ["au", [dns1, dns2]]
|
||||||
|
}
|
||||||
|
s_ip6 = {"method" => "ignore"}
|
||||||
|
|
||||||
|
con = {
|
||||||
|
"802-3-ethernet" => s_wired,
|
||||||
|
"connection" => s_con,
|
||||||
|
"ipv4" => s_ip4,
|
||||||
|
"ipv6" => s_ip6
|
||||||
|
}
|
||||||
|
|
||||||
|
system_bus = DBus::SystemBus.instance
|
||||||
|
nm = system_bus.service("org.freedesktop.NetworkManager").object("/org/freedesktop/NetworkManager/Settings")
|
||||||
|
nm.introspect
|
||||||
|
settings_iface = nm["org.freedesktop.NetworkManager.Settings"]
|
||||||
|
|
||||||
|
ret = settings_iface.AddConnection(con)
|
||||||
|
puts "New connection added: #{ret.first}"
|
||||||
|
|
51
examples/ruby/get-basic-nm-info.rb
Executable file
51
examples/ruby/get-basic-nm-info.rb
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# vim: ft=ruby ts=2 sts=2 sw=2 et ai
|
||||||
|
# -*- Mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along
|
||||||
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2011 Red Hat, Inc.
|
||||||
|
#
|
||||||
|
|
||||||
|
require 'dbus'
|
||||||
|
|
||||||
|
#
|
||||||
|
# This example gets basic information about NetworkManager.
|
||||||
|
# Namely, it gets properties from /org/freedesktop/NetworkManager object.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Get system bus
|
||||||
|
system_bus = DBus::SystemBus.instance
|
||||||
|
|
||||||
|
# Get the NetworkManager service
|
||||||
|
nm_service = system_bus.service("org.freedesktop.NetworkManager")
|
||||||
|
|
||||||
|
# Get the object from the service
|
||||||
|
nm_object = nm_service.object("/org/freedesktop/NetworkManager")
|
||||||
|
|
||||||
|
# Set default interface for the object
|
||||||
|
nm_object.default_iface = "org.freedesktop.NetworkManager"
|
||||||
|
|
||||||
|
# Introspect it
|
||||||
|
nm_object.introspect
|
||||||
|
|
||||||
|
properties = nm_object["org.freedesktop.DBus.Properties"].GetAll("org.freedesktop.NetworkManager")
|
||||||
|
|
||||||
|
puts "Basic NM properties:"
|
||||||
|
puts "===================="
|
||||||
|
properties[0].each do |prop,val|
|
||||||
|
puts "#{prop} = #{val}"
|
||||||
|
end
|
||||||
|
|
85
examples/ruby/list-devices.rb
Executable file
85
examples/ruby/list-devices.rb
Executable file
@@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# vim: ft=ruby ts=2 sts=2 sw=2 et ai
|
||||||
|
# -*- Mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along
|
||||||
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2011 Red Hat, Inc.
|
||||||
|
#
|
||||||
|
|
||||||
|
require 'dbus'
|
||||||
|
|
||||||
|
#
|
||||||
|
# This example lists basic information about network interfaces known to NM
|
||||||
|
#
|
||||||
|
|
||||||
|
devtypes = { 1 => "Ethernet",
|
||||||
|
2 => "WiFi",
|
||||||
|
5 => "Bluetooth",
|
||||||
|
6 => "OLPC",
|
||||||
|
7 => "WiMAX",
|
||||||
|
8 => "Modem" }
|
||||||
|
|
||||||
|
states = { 0 => "Unknown",
|
||||||
|
10 => "Unmanaged",
|
||||||
|
20 => "Unavailable",
|
||||||
|
30 => "Disconnected",
|
||||||
|
40 => "Prepare",
|
||||||
|
50 => "Config",
|
||||||
|
60 => "Need Auth",
|
||||||
|
70 => "IP Config",
|
||||||
|
80 => "IP Check",
|
||||||
|
90 => "Secondaries",
|
||||||
|
100 => "Activated",
|
||||||
|
110 => "Deactivating",
|
||||||
|
120 => "Failed" }
|
||||||
|
|
||||||
|
# Get system bus
|
||||||
|
system_bus = DBus::SystemBus.instance
|
||||||
|
|
||||||
|
# Get the NetworkManager service
|
||||||
|
nm_service = system_bus.service("org.freedesktop.NetworkManager")
|
||||||
|
|
||||||
|
# Get the object from the service
|
||||||
|
nm_object = nm_service.object("/org/freedesktop/NetworkManager")
|
||||||
|
|
||||||
|
# Set default interface for the object
|
||||||
|
nm_object.default_iface = "org.freedesktop.NetworkManager"
|
||||||
|
|
||||||
|
# Introspect it
|
||||||
|
nm_object.introspect
|
||||||
|
|
||||||
|
# Get all devices known to NM
|
||||||
|
devices = nm_object.GetDevices.first
|
||||||
|
|
||||||
|
# and print their properties
|
||||||
|
devices.each do |d|
|
||||||
|
dev_obj = system_bus.service("org.freedesktop.NetworkManager").object(d)
|
||||||
|
dev_obj.introspect
|
||||||
|
props = dev_obj["org.freedesktop.DBus.Properties"].GetAll("org.freedesktop.NetworkManager.Device")
|
||||||
|
|
||||||
|
puts "============================"
|
||||||
|
puts "Interface: #{props[0]['Interface']}"
|
||||||
|
|
||||||
|
devtype = devtypes[props[0]['DeviceType']]
|
||||||
|
devtype = "Unknown" if devtype.nil?
|
||||||
|
puts "Type: #{devtype}"
|
||||||
|
|
||||||
|
puts "Driver: #{props[0]['Driver']}"
|
||||||
|
|
||||||
|
state = states[props[0]['State']]
|
||||||
|
state = "Unknown" if state.nil?
|
||||||
|
puts "State: #{state}"
|
||||||
|
end
|
Reference in New Issue
Block a user