diff options
author | Dominik Ritter <dritter03@googlemail.com> | 2019-02-26 02:07:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-26 02:07:51 +0300 |
commit | 613b798bb352086b62c00e2494fc0bbefad8e8b4 (patch) | |
tree | dc70d8b1b0890b36d8ecc2fbd9cf3e04167ea143 /functions | |
parent | dcd7718c62a935b545bd60148e9ea4d7bdf9e70a (diff) | |
parent | 0dbfa4e1e4bfd65311403884168b7cddb17cdbdc (diff) |
Merge pull request #1126 from Shini31/master
Use ip command for VPN segment - fix #1125
Diffstat (limited to 'functions')
-rwxr-xr-x | functions/utilities.zsh | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/functions/utilities.zsh b/functions/utilities.zsh index 95f89d99..8c18bb44 100755 --- a/functions/utilities.zsh +++ b/functions/utilities.zsh @@ -373,3 +373,58 @@ function upsearch () { popd > /dev/null fi } + +# Parse IP address from ifconfig on OSX and from IP on Linux +# Parameters: +# $1 - string The desired Interface +# $2 - string A root prefix for testing purposes +function p9k::parseIp() { + local desiredInterface="${1}" + + if [[ -z "${desiredInterface}" ]]; then + desiredInterface="^[^ ]+" + fi + + local ROOT_PREFIX="${2}" + if [[ "$OS" == "OSX" ]]; then + # Get a plain list of all interfaces + local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l 2>/dev/null)" + # Parse into array (split by whitespace) + local -a interfaces + interfaces=(${=rawInterfaces}) + # Parse only relevant interface names + local pattern="${desiredInterface}[^ ]?" + local -a relevantInterfaces + for rawInterface in $interfaces; do + [[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH ) + done + local newline=$'\n' + for interfaceName in $relevantInterfaces; do + local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)" + if [[ "${interface}" =~ "lo[0-9]*" ]]; then + continue + fi + # Check if interface is UP. + if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then + local ipFound="${match[3]}" + local -a interfaceStates=(${(s:,:)match[1]}) + if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then + echo "${ipFound}" + return 0 + fi + fi + done + else + local -a interfaces + interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" ) + local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)" + for interface in "${(@)interfaces}"; do + if [[ "$interface" =~ $pattern ]]; then + echo "${match[1]}" + return 0 + fi + done + fi + + return 1 +} |