PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/NetworkManager-0.9.5.95/examples/ruby/add-connection.rb

#
Ruby | 86 lines | 42 code | 14 blank | 30 comment | 0 complexity | 4166cb0d46e6c70cb0684f4918e82c6a MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  1. #!/usr/bin/env ruby
  2. # vim: ft=ruby ts=2 sts=2 sw=2 et ai
  3. # -*- Mode: ruby; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. #
  19. # Copyright (C) 2011 Red Hat, Inc.
  20. #
  21. require 'dbus'
  22. require 'ipaddr'
  23. #
  24. # This example adds a new ethernet connection via Addconnection() D-Bus call.
  25. # It also shows how to specify D-Bus signature for properties like "addresses"
  26. # and "clone-mac-address".
  27. #
  28. # Configuration settings are described here:
  29. # http://projects.gnome.org/NetworkManager/developers/api/09/ref-settings.html
  30. #
  31. # Helper functions
  32. def ip_to_int(ip_addr)
  33. return IPAddr.new(ip_addr).hton.unpack('L').first
  34. end
  35. def rand_hex_3(l)
  36. "%0#{l}x" % rand(1 << l*4)
  37. end
  38. def rand_uuid
  39. [8,4,4,4,12].map {|n| rand_hex_3(n)}.join('-')
  40. end
  41. # Create new connection settings
  42. s_con = {
  43. "type" => "802-3-ethernet",
  44. "uuid"=> rand_uuid,
  45. "id" => "__MyConnection__"
  46. }
  47. s_wired = { "cloned-mac-address" => ["ay", [0x00, 0x22, 0x68, 0x01, 0x02, 0x03]]}
  48. ip1 = ip_to_int("192.168.1.12")
  49. ip2 = ip_to_int("192.168.1.13")
  50. gw1 = ip_to_int("192.168.1.1")
  51. ip3 = ip_to_int("10.0.2.5")
  52. gw2 = ip_to_int("10.0.2.254")
  53. dns1 = ip_to_int("8.8.8.8")
  54. dns2 = ip_to_int("8.8.4.4")
  55. s_ip4 = {
  56. "addresses"=> ["aau", [[ip1, 24, gw1], [ip2, 24, gw1], [ip3, 24, gw2]]],
  57. "method"=>["s", "manual"],
  58. "dns"=> ["au", [dns1, dns2]]
  59. }
  60. s_ip6 = {"method" => "ignore"}
  61. con = {
  62. "802-3-ethernet" => s_wired,
  63. "connection" => s_con,
  64. "ipv4" => s_ip4,
  65. "ipv6" => s_ip6
  66. }
  67. system_bus = DBus::SystemBus.instance
  68. nm = system_bus.service("org.freedesktop.NetworkManager").object("/org/freedesktop/NetworkManager/Settings")
  69. nm.introspect
  70. settings_iface = nm["org.freedesktop.NetworkManager.Settings"]
  71. ret = settings_iface.AddConnection(con)
  72. puts "New connection added: #{ret.first}"