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 () { + 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() { + 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" }