-
Notifications
You must be signed in to change notification settings - Fork 0
/
wgnames.awk
executable file
·104 lines (93 loc) · 2.09 KB
/
wgnames.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
#!/usr/bin/awk -f
BEGIN {
color = 1;
if (debug)
printf "Num of arguments %d\n", ARGC-1;
if (ARGC > 1) {
color = 0;
for (i = 1; i < ARGC; i++) {
fulline = fulline " " ARGV[i];
if (debug)
printf "Arg no: %d --> %s\n", i, ARGV[i];
}
}
if (debug)
print "ARGS: wg" fulline;
wg = "/usr/bin/wg " fulline;
ln = 0;
while (( wg | getline line) > 0) {
wg_output[ln] = line;
ln++;
}
close(wg)
file="/etc/wireguard/wg0server.conf"
indeep = 0
if (debug)
print "############ wireguard config file parsing start"
while(( getline < file) > 0 ) {
if (debug)
print $0
if ($1 ~ /Peer/ || indeep == 1) {
indeep = 0
while(1) {
if (( getline < file) == 0 ) {
if (debug)
print "break EOF"
break
}
if ($1 ~ /Description/) {
split($0, text, "=")
description = text[2];
description_matched = 1
}
if ($1 ~ /PublicKey/) {
pubkey = $3;
pub_key_matched = 1
}
if (description_matched == 1 && pub_key_matched == 1) {
if (debug)
print "break matched"
description_matched = 0
pub_key_matched = 0
break
}
if ($1 ~ /Peer/) {
if (debug)
print "break peer"
description_matched = 0
pub_key_matched = 0
indeep = 1
break
}
}
description_pubkey_hash[pubkey]=description
pubkey = ""
description = ""
}
}
if (debug)
print "############ wireguard config file parsing end"
i = 0;
while (i < ln) {
$0 = wg_output[i]
if ($0 ~ /peer:/) {
description = description_pubkey_hash[$2]
if (description != "")
print "\033[38;5;198mdesc:"description"\033[0m";
print "\033[1;32mpeer\033[0m:", "\033[38;5;28m"$2"\033[0m";
$0 = wg_output[++i];
} else if ($0 ~ /\[Peer\]/) {
print;
$0 = wg_output[++i];
description = description_pubkey_hash[$3]
if (description != "")
print "#Description = " description
}
if (color) {
gsub("(endpoint|allowed ips|latest handshake|transfer)", "\033[1m&\033[0m"); # Make bold
gsub("(KiB|MiB|GiB|seconds?|minutes?|hours?|/)", "\033[38;5;45m&\033[0m"); # Make blue
}
print;
i++
}
}