summaryrefslogtreecommitdiff
path: root/user/netifrc/switch-l2tp-gawk-to-perl.patch
blob: 9c8bc7e0c56d8cd44e09e6e22405c0bdfb4fe35e (plain) (blame)
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
diff --git a/net/l2tp.sh b/net/l2tp.sh
index 9644e32..24b081c 100644
--- a/net/l2tp.sh
+++ b/net/l2tp.sh
@@ -37,9 +37,20 @@ _is_l2tp() {
 	ip l2tp show session &>/dev/null
 	[ $? -ne 0 ] && return 1
 
-	eval "$(ip l2tp show session | \
-		awk "match(\$0, /^Session ([0-9]+) in tunnel ([0-9]+)\$/, ret) {sid=ret[1]; tid=ret[2]} 
-		match(\$0, /^[ ]*interface name: ${IFACE}\$/) {print \"session_id=\"sid\";tunnel_id=\"tid; exit}")"
+    eval "$(ip l2tp show session | perl -E '
+my $sid;
+my $tid;
+my $IFACE=$ARGV[0];
+while (<STDIN>) {
+    if($_ =~ /^Session ([0-9]+) in tunnel ([0-9+])\$/) {
+        $sid = $1;
+        $tid = $2;
+    }
+    if ($_ =~ /^[ ]*interface name: $IFACE$/) {
+        say "session_id=" . $sid . ";" . "tunnel_id=" . $tid;
+        exit;
+    }
+}' $IFACE)"
 	test -n "$session_id"
 }
 
@@ -49,33 +60,36 @@ _is_l2tp() {
 _l2tp_get_tunnel_info() {
 	local found
 	eval "$(ip l2tp show tunnel | \
-		awk -v id=$2 -v prefix=$1 '
-		match($0, /^Tunnel ([0-9]+), encap (IP|UDP)$/, ret) {
-			if (found == "1") exit;
-			if (ret[1] == id) {
-				print "found=1;"
-				print prefix "tunnel_id=" ret[1] ";"
-				print prefix "encap=" ret[2] ";";
-				found="1"
-			}
-		} 
-		match($0, /^[ ]*From ([^ ]+) to ([^ ]+)$/, ret) {
-			if (found == "1") {
-				print prefix "local=" ret[1] ";"; 
-				print prefix "remote=" ret[2] ";"; 
-			}
-		}
-		match($0, /^[ ]*Peer tunnel ([0-9]+)$/, ret) {
-			if (found == "1") {
-				print prefix "peer_tunnel_id=" ret[1] ";"; 
-			}
-		}
-		match($0, /^[ ]*UDP source \/ dest ports: ([0-9]+)\/([0-9]+)$/, ret) {
-			if (found == "1") {
-				print prefix "udp_sport=" ret[1] ";"; 
-				print prefix "udp_dport=" ret[2] ";"; 
-			}
-		}')"
+		perl -E '
+my ($prefix, $id) = @ARGV;
+my $found = 0;
+while(<STDIN>) {
+    if ($_ =~ /^Tunnel ([0-9]+), encap (IP|UDP)$/) {
+        if ($found) {
+            exit;
+        }
+        elsif ($1 == $id) {
+            say "found=1;";
+            say $prefix . "tunnel_id=" . $1 . ";";
+            say $prefix . "encap=" . $2 . ";";
+            $found = 1;
+        }
+    }
+    elsif ($found) {
+        if ($_ =~ /^[ ]*From ([^ ]+) to ([^ \n]+)$/) {
+            say $prefix . "local=" . $1 . ";";
+            say $prefix . "remote=" . $2 . ";";
+        }
+        elsif ($_ =~ /^[ ]*Peer tunnel ([0-9]+)$/) {
+            say $prefix . "peer_tunnel_id=" . $1 . ";";
+        }
+        elsif ($_ =~ /^[ ]*UDP source \/ dest ports: ([0-9]+)\/([0-9]+)$/) {
+            say $prefix . "udp_sport=" . $1 . ";";
+            say $prefix . "udp_dport=" . $2 . ";";
+        }
+    }
+}
+		' "$1" "$2")"
 	test -n "$found"
 }