forked from JoeKuan/Network-Interfaces-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadInterfaces.awk
85 lines (76 loc) · 2.1 KB
/
readInterfaces.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
BEGIN { start = 0;
if (ARGC < 3 || ARGC > 4) {
print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
exit 1;
}
for (i = 2; i < ARGC; i++) {
if (ARGV[i] == debug) {
debug = 1;
continue;
}
split(ARGV[i], arg, "=");
if (arg[1] == "device")
device = arg[2];
}
if (!length(device)) {
print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
exit 1;
}
}
{
# Look for iface line and if the interface comes with the device name
# scan whether it is dhcp or static or manual
# e.g. iface eth0 inet [static | dhcp | manual]
if ($1 == "iface") {
# Ethernet name matches - switch the line scanning on
if ($2 == device) {
if (debug)
print $0;
# It's a DHCP interface
if (match($0, / dhcp/)) {
print "dhcp";
exit 0;
# It's a static network interface. We want to scan the
# addresses after the static line
} else if (match ($0, / static/)) {
static = 1;
next;
} else if (match ($0, / manual/)) {
print "manual";
exit 0;
}
# If it is other inteface line, switch it off
# Go to the next line
} else {
static = 0;
next;
}
}
# At here, it means we are after the iface static line of
# after the device we are searching for
# Scan for the static content
if (static) {
if (debug)
print "static - ", $0, $1;
if ($1 == "address") {
address = $2;
gotAddr = 1;
}
if ($1 == "netmask") {
netmask = $2;
gotAddr = 1;
}
if ($1 == "gateway") {
gateway = $2;
gotAddr = 1;
}
}
}
END {
if (gotAddr) {
printf("%s %s %s\n", address, netmask, gateway);
exit 0;
} else {
exit 1;
}
}