-
Notifications
You must be signed in to change notification settings - Fork 39
/
changeInterface.awk
183 lines (166 loc) · 4.73 KB
/
changeInterface.awk
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function writeStatic(device, fields, orders) {
# Create the order as original
for (o = 0; o in orders; o++) {
field = orders[o];
value = fields[field];
delete fields[field];
if (length(value))
printf(" %s %s\n", field, value);
}
# additional items have no order
for (f in fields) {
value = fields[f];
if (length(value))
printf(" %s %s\n", f, value);
}
}
function usage() {
print "awk -f changeInterfaces.awk <interfaces file> dev=<eth device> \n" \
" [mode=dhcp|static|manual] [action=add|remove] \n" \
" [address=<ip addr> networkmask=<ipaddress> <name=value> ]\n" \
" [arg=debug]\n"
}
BEGIN {
start = 0;
order = 0
version = "inet"
if (ARGC < 3 || ARGC > 10) {
usage();
exit 1;
}
for (i = 2; i < ARGC; i++) {
num = split(ARGV[i], pair, "=");
if (pair[1] == "arg" && pair[2] == "debug")
debug = 1;
else if (pair[1] == "mode")
mode = pair[2];
else if (pair[1] == "action" && pair[2] == "remove")
remove = 1;
else if (pair[1] == "action" && pair[2] == "add")
add = 1;
else if (pair[1] == "version" && pair[2] == "ipv6")
version = "inet6";
else if (pair[1] == "device" || pair[1] == "dev")
device = pair[2];
else if (num == 2) {
if (pair[1] == "dns")
pair[1] = "dns-nameservers";
settings[pair[1]] = pair[2];
}
else {
usage();
exit 1;
}
}
# Sort out the logic of argument
if (mode == "dhcp" && (length(network) || length(gateway) || length(address) || length(netmask))) {
print "Both DHCP and static properties are defined";
usage();
exit 1;
}
else if (!mode && !remove) {
print "Missing mode input";
usage();
exit 1;
}
if (debug) {
for (f in settings) {
print f, ": ", settings[f];
}
}
}
{
# auto <device> line
if ($1 == "auto" || $1 == "allow-hotplug") {
if ($2 != device) {
# We come to different device
# Good place to write all the settings
if (targetDev) {
targetDev = 0;
if (!add && !remove) {
if (mode == "static" || mode == "manual")
writeStatic(device, settings, fieldOrders);
print "";
}
}
print $0;
next;
}
else if (!remove) {
print $0;
add = 0;
next;
}
# Remove - don't print
next;
}
# iface <device> .. line
else if ($1 == "iface") {
if ($2 != device) {
# We come to different device
# Good place to write all the settings
if (targetDev) {
targetDev = 0;
if (!add && !remove) {
if (mode == "static" || mode == "manual")
writeStatic(device, settings, fieldOrders);
print "";
}
}
print $0;
next;
}
else {
# If already specified 'add' and found an existing entry
# cancel it
add = 0;
# Go to different condition in next loop
targetDev = 1;
if (!remove)
printf("iface %s %s %s\n", device, version, mode);
next;
}
}
# Matched device found - working through each line
# until found a 'auto' line or end of file
else if (targetDev) {
# Comment line - leave it
if (substr($1, 0, 1) == "#") {
print $0;
next;
}
field = $1;
if (field in settings) {
# It means we specify the argument in command line
# as preference over the file content
}
else {
# field not in the command line
# copy it over
settings[field] = substr($0, index($0, $2))
}
fieldOrders[order] = field
order++;
next;
# Other type of lines e.g. comment
}
else {
print $0;
next;
}
}
END {
# Come to the last line and we may not print out the
# matched device settings
if (!remove) {
if (add || targetDev) {
if (add) {
printf("auto %s\n", device);
printf("iface %s %s %s\n", device, version, mode);
}
if (mode != "dhcp")
writeStatic(device, settings, fieldOrders);
print "";
}
}
}