1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/env lua
--
-- 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 2015 Red Hat, Inc.
--
--
-- Adding an Ethernet connection to NetworkManager in Lua.
-- The example uses libnm library using GObject introspection via Lua lgi module.
-- Most distribution ship the module as lua-lgi package.
-- libnm guide: https://developer.gnome.org/libnm/1.0/
-- Lua-lgi guide: https://github.com/pavouk/lgi/blob/master/docs/guide.md
--
local lgi = require 'lgi'
local GLib = lgi.GLib
local NM = lgi.NM
function uuid()
math.randomseed(os.time())
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
local uuid = string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
return uuid
end
-- function creating NMConnection
function create_profile(name)
profile = NM.SimpleConnection.new()
s_con = NM.SettingConnection.new()
s_wired = NM.SettingWired.new()
s_con[NM.SETTING_CONNECTION_ID] = name
s_con[NM.SETTING_CONNECTION_UUID] = uuid()
s_con[NM.SETTING_CONNECTION_TYPE] = "802-3-ethernet"
profile:add_setting(s_con)
profile:add_setting(s_wired)
-- show the connection
-- profile:dump()
return profile
end
-- callback function for add_connection_async()
function added_cb(client, result, data)
local con,err,code = client:add_connection_finish(result)
if con then
print("The connection profile has been successfully added to NetworkManager:")
print(con:get_id(), con:get_uuid())
else
print(string.format("Error: (%d) %s", code, err))
end
main_loop:quit() -- exit now
end
---------------------------
-- Main code starts here --
---------------------------
-- parse command-line arguments
local name, persist = ...
if (not name or (persist and not string.find("persistent", persist, 1))) then
print(string.format("Usage: %s <connection name> [persistent]", arg[0]:gsub(".*/","")))
os.exit(1)
end
-- create GLib main loop
main_loop = GLib.MainLoop(nil, false)
-- create Client object
local client = NM.Client.new()
-- create a connection profile
local con = create_profile(name)
-- send the connection to NetworkManager
client:add_connection_async(con, persist, nil, added_cb, nil)
-- run main loop so that the callback could be called
main_loop:run()
|