local_ip: Add interface selection (#1327)

* local_ip: add interface selection

local_ip: add interface selection for macOS
local_ip: add interface support for Linux and others
local_ip: fix quotes
local_ip: fix line length
local_ip: fix line length for realsies this time

* local ip: change default to auto and simplify if statement

* local ip: make error for invalid interface actually work on linux

* local ip: remove unneeded slash

* local ip: silence errors from ip and ifconfig

Co-authored-by: dylan <dylan.araps@gmail.com>
This commit is contained in:
Andrew Titmuss 2020-10-04 23:09:36 +11:00 committed by GitHub
parent d76815d3a2
commit 9ca24eaa0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 5 deletions

View File

@ -407,6 +407,13 @@ public_ip_host="http://ident.me"
# Flag: --ip_timeout # Flag: --ip_timeout
public_ip_timeout=2 public_ip_timeout=2
# Local IP interface
#
# Default: 'auto' (interface of default route)
# Values: 'auto', 'en0', 'en1'
# Flag: --ip_interface
local_ip_interface=('auto')
# Desktop Environment # Desktop Environment
@ -3702,9 +3709,25 @@ get_battery() {
get_local_ip() { get_local_ip() {
case $os in case $os in
"Linux" | "BSD" | "Solaris" | "AIX" | "IRIX") "Linux" | "BSD" | "Solaris" | "AIX" | "IRIX")
local_ip="$(ip route get 1 | awk -F'src' '{print $2; exit}')" if [[ "${local_ip_interface[0]}" == "auto" ]]; then
local_ip="${local_ip/uid*}" local_ip="$(ip route get 1 | awk -F'src' '{print $2; exit}')"
[[ -z "$local_ip" ]] && local_ip="$(ifconfig -a | awk '/broadcast/ {print $2; exit}')" local_ip="${local_ip/uid*}"
[[ "$local_ip" ]] || local_ip="$(ifconfig -a | awk '/broadcast/ {print $2; exit}')"
else
for interface in "${local_ip_interface[@]}"; do
local_ip="$(ip addr show "$interface" 2> /dev/null |
awk '/inet / {print $2; exit}')"
local_ip="${local_ip/\/*}"
[[ "$local_ip" ]] ||
local_ip="$(ifconfig "$interface" 2> /dev/null |
awk '/broadcast/ {print $2; exit}')"
if [[ -n "$local_ip" ]]; then
prin "$interface" "$local_ip"
else
err "Local IP: Could not detect local ip for $interface"
fi
done
fi
;; ;;
"MINIX") "MINIX")
@ -3712,8 +3735,19 @@ get_local_ip() {
;; ;;
"Mac OS X" | "macOS" | "iPhone OS") "Mac OS X" | "macOS" | "iPhone OS")
local_ip="$(ipconfig getifaddr en0)" if [[ "${local_ip_interface[0]}" == "auto" ]]; then
[[ -z "$local_ip" ]] && local_ip="$(ipconfig getifaddr en1)" interface="$(route get 1 | awk -F': ' '/interface/ {printf $2; exit}')"
local_ip="$(ipconfig getifaddr "$interface")"
else
for interface in "${local_ip_interface[@]}"; do
local_ip="$(ipconfig getifaddr "$interface")"
if [[ -n "$local_ip" ]]; then
prin "$interface" "$local_ip"
else
err "Local IP: Could not detect local ip for $interface"
fi
done
fi
;; ;;
"Windows") "Windows")
@ -4882,6 +4916,7 @@ INFO:
--ip_host url URL to query for public IP --ip_host url URL to query for public IP
--ip_timeout int Public IP timeout (in seconds). --ip_timeout int Public IP timeout (in seconds).
--ip_interface value Interface(s) to use for local IP
--song_format format Print the song data in a specific format (see config file). --song_format format Print the song data in a specific format (see config file).
--song_shorthand on/off Print the Artist/Album/Title on separate lines. --song_shorthand on/off Print the Artist/Album/Title on separate lines.
--memory_percent on/off Display memory percentage. --memory_percent on/off Display memory percentage.
@ -5078,6 +5113,17 @@ get_args() {
"--shell_version") shell_version="$2" ;; "--shell_version") shell_version="$2" ;;
"--ip_host") public_ip_host="$2" ;; "--ip_host") public_ip_host="$2" ;;
"--ip_timeout") public_ip_timeout="$2" ;; "--ip_timeout") public_ip_timeout="$2" ;;
"--ip_interface")
unset local_ip_interface
for arg in "$@"; do
case "$arg" in
"--ip_interface") ;;
"-"*) break ;;
*) local_ip_interface+=("$arg") ;;
esac
done
;;
"--song_format") song_format="$2" ;; "--song_format") song_format="$2" ;;
"--song_shorthand") song_shorthand="$2" ;; "--song_shorthand") song_shorthand="$2" ;;
"--music_player") music_player="$2" ;; "--music_player") music_player="$2" ;;