-
Notifications
You must be signed in to change notification settings - Fork 8
/
hda-change-network
executable file
·119 lines (104 loc) · 3.35 KB
/
hda-change-network
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
#
# Amahi Home Server
# Copyright (C) 2007-2013 Amahi
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# 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
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
require 'dbus'
require 'ipaddr'
def usage(error=nil)
puts "error: #{error}" if error
puts "usage: hda-change-network HDA-IP GW-IP DNS1-IP DNS2-IP"
exit(-1)
end
def ip2int(ip_addr)
IPAddr.new(ip_addr).hton.unpack('L').first
end
def rand_hex(l)
"%0#{l}x" % rand(1 << l*4)
end
def rand_uuid
[8,4,4,4,12].map {|n| rand_hex(n)}.join('-')
end
# select an ethernet connection - returns a connection object or nil
def select_connection(network_manager, connections)
connections.each do |conn|
conn_object = network_manager.object(conn)
conn_object.introspect
settings = conn_object["org.freedesktop.NetworkManager.Settings.Connection"].GetSettings
conn_settings = settings.first['connection']
if conn_settings['type'] == "802-3-ethernet"
#puts "Using first ethernet connection \"#{conn_settings['id']}\"."
return conn_object
else
#puts "Ignoring non-ethernet connection \"#{conn_settings['id']}\"."
end
end
nil
end
def setup_network_manager(ip, gw, dns1, dns2)
s_ip4 = {
"addresses"=> ["aau", [[ip2int(ip), 24, ip2int(gw)]]],
"method"=>["s", "manual"],
"dns"=> ["au", [ip2int(dns1), ip2int(dns2)]]
}
con = { "ipv4" => s_ip4 }
system_bus = DBus::SystemBus.instance
nm_service = system_bus.service("org.freedesktop.NetworkManager")
settings = nm_service.object("/org/freedesktop/NetworkManager/Settings")
settings.introspect
settings_iface = settings["org.freedesktop.NetworkManager.Settings"]
connections = settings_iface.ListConnections.first
ethernet = select_connection(nm_service, connections)
if ethernet
s = ethernet["org.freedesktop.NetworkManager.Settings.Connection"].GetSettings
new_settings = s[0].merge(con)
# FIXME we're not sure why this needs to be done now to avoid dbus exceptions
new_settings["connection"].delete 'permissions'
new_settings["connection"].delete 'secondaries'
new_settings["connection"].delete 'mac-address'
new_settings.delete "802-3-ethernet"
new_settings.delete "ipv6"
ethernet["org.freedesktop.NetworkManager.Settings.Connection"].Update(new_settings)
else
# if we have not found an ethernet connection configured, create one
con["connection"] = {
"type" => "802-3-ethernet",
"uuid"=> rand_uuid,
"id" => "Amahi Network"
}
con["802-3-ethernet"] = {}
settings_iface.AddConnection(con)
end
end
def main
usage unless ARGV.size == 4
args = ARGV.map{ |a| a.strip }
ARGV.each do |addr|
usage("arguments must be IP addresses") unless addr =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
end
ip = args[0]
gw = args[1]
dns1 = args[2]
dns2 = args[3]
#puts "IP: #{ip}, GW: #{gw}, DNS1: #{dns1}, DNS2: #{dns2}"
begin
setup_network_manager(ip, gw, dns1, dns2)
rescue
puts "error: NetworkManager setup falied"
exit -1
end
exit 0
end
main