summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJiří Klimeš <jklimes@redhat.com>2011-11-29 16:20:04 +0100
committerJiří Klimeš <jklimes@redhat.com>2011-11-29 16:20:04 +0100
commit77ef5e8676d62f0e880b33d332c46a91351d6c55 (patch)
treeac959fadee843953ea62bdc25fce3bb838c43c0c /examples
parent576acdd2bf4cfde11f9b8ea15ec87bb7271c5502 (diff)
downloadNetworkManager-77ef5e8676d62f0e880b33d332c46a91351d6c55.tar.gz
examples: add a few examples in ruby
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am1
-rw-r--r--examples/ruby/Makefile.am4
-rwxr-xr-xexamples/ruby/add-connection.rb86
-rwxr-xr-xexamples/ruby/get-basic-nm-info.rb51
-rwxr-xr-xexamples/ruby/list-devices.rb85
5 files changed, 227 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 643959b143..f0f62efd0a 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1,3 +1,4 @@
SUBDIRS= \
python \
+ ruby \
C
diff --git a/examples/ruby/Makefile.am b/examples/ruby/Makefile.am
new file mode 100644
index 0000000000..14f97ef89d
--- /dev/null
+++ b/examples/ruby/Makefile.am
@@ -0,0 +1,4 @@
+EXTRA_DIST = \
+ add-connection.rb \
+ get-basic-nm-info.rb \
+ list-devices.rb
diff --git a/examples/ruby/add-connection.rb b/examples/ruby/add-connection.rb
new file mode 100755
index 0000000000..4256848f3c
--- /dev/null
+++ b/examples/ruby/add-connection.rb
@@ -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}"
+
diff --git a/examples/ruby/get-basic-nm-info.rb b/examples/ruby/get-basic-nm-info.rb
new file mode 100755
index 0000000000..1cf679477e
--- /dev/null
+++ b/examples/ruby/get-basic-nm-info.rb
@@ -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
+
diff --git a/examples/ruby/list-devices.rb b/examples/ruby/list-devices.rb
new file mode 100755
index 0000000000..5831a12687
--- /dev/null
+++ b/examples/ruby/list-devices.rb
@@ -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