forked from JoeKuan/Network-Interfaces-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeInterface.awk
More file actions
173 lines (143 loc) · 4.46 KB
/
changeInterface.awk
File metadata and controls
173 lines (143 loc) · 4.46 KB
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
function writeStatic(addr, nw, nm, gw) {
if (length(addr))
print " address ", addr
if (length(nw))
print " network ", nw
if (length(nm))
print " netmask ", nm
if (length(gw))
print " gateway ", gw
}
function usage() {
print "awk -f changeInterfaces.awk <interfaces file> device=<eth device> \n" \
" [address=<ip addr>] [gateway=<ip addr>] [netmask=<ip addr>]\n" \
" [network=<ip addr>] [mode=dhcp|static] [debug]"
}
BEGIN { start = 0;
if (ARGC < 3 || ARGC > 9) {
usage();
exit 1;
}
for (i = 2; i < ARGC; i++) {
if (ARGV[i] == "debug") {
debug = 1;
delete ARGV[i];
continue;
}
split(ARGV[i], pair, "=");
if (pair[1] == "address")
address = pair[2];
else if (pair[1] == "gateway")
gateway = pair[2];
else if (pair[1] == "network")
network = pair[2];
else if (pair[1] == "netmask")
netmask = pair[2];
else if (pair[1] == "device")
device = pair[2];
else if (pair[1] == "mode" && pair[2] == "dhcp")
dhcp = 1;
else if (pair[1] == "mode" && pair[2] == "static")
static = 1;
else if (pair[1] == "mode" && pair[2] == "remove")
remove = 1;
else {
usage();
exit 1;
}
}
# Sort out the logic of argument
if (dhcp && (length(network) || length(gateway) || length(address) || length(netmask))) {
print "Both DHCP and static properties are defined";
usage();
exit 1;
}
}
{
if ($1 == "auto" && remove) {
gsub(device, "");
print;
next;
}
# Look for iface line and if the interface comes with the device name
# scan whether it is dhcp or static
if ($1 == "iface") {
# Ethernet name matches - switch the line scanning on
if ($2 == device) {
if (debug)
print $0;
# If remove is defined, switch on delete mode
if (remove) {
definedRemove=1;
}
# It's a DHCP interface, if defined any static properties
# change it to static
else if (match($0, / dhcp/)) {
definedDhcp=1;
# Change to static if defined properties
if (length(address) || length (gateway) ||
length(netmask) || length (network) || static) {
print "iface", device, "inet static";
next;
}
}
# It's a static network interface
else if (match ($0, / static/)) {
definedStatic=1;
# Change to dhcp if defined
if (dhcp) {
sub(/ static/, " dhcp");
print $0;
next;
}
}
}
# If it is other inteface line, switch it off
else {
definedStatic = 0;
definedDhcp = 0;
definedRemove = 0;
}
if (!definedRemove) {
print $0;
next;
}
}
# Reaches here - means non iface lines
# Change the static content
if (definedStatic) {
# Already defined static, just changing the properties
# Otherwise omit everything until the iface section is
# finished
if (!dhcp) {
if (debug)
print "static - ", $0, $1;
if ($1 == "address" && length(address))
print " address ", address
else if ($1 == "netmask" && length(netmask))
print " netmask ", netmask;
else if ($1 == "gateway" && length(gateway))
print " gateway ", gateway;
else if ($1 == "network" && length(network))
print " network ", network;
else
print $0;
}
next;
}
# If already defined dhcp, then dump the network properties
if (definedDhcp) {
writeStatic(address, network, netmask, gateway);
definedDhcp = 0;
next;
}
if (!definedRemove) {
print $0;
}
}
END {
# This bit is useful at the condition when the last line is
# iface dhcp
if (definedDhcp)
writeStatic(address, network, netmask, gateway);
}