Skip to content

Commit

Permalink
fix: prefer using iproute2 instead of ifconfig
Browse files Browse the repository at this point in the history
For `_comp_compgen_available_interfaces`, we prefer to use ip
(iproute) since long interface names will be truncated by ifconfig
(respective packages in the operating system, e.g. inetutils) [1].
Even for the other functions that use "ifconfig" and "ip", we change
to use `ip` because `ip`'s behavior is more uniform among the systems
and also `ip` is becoming more common in Linux distributions.

[1]: https://github.com/scop/bash-completion/pull/1090/files

Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
  • Loading branch information
hellodword and akinomyoga committed May 14, 2024
1 parent 0543d1a commit a3fdc0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 7 additions & 4 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ _comp_compgen_mac_addresses()
# - ip link: link/ether
_comp_compgen -v addresses split -- "$(
{
LC_ALL=C ifconfig -a || ip -c=never link show || ip link show
ip -c=never link show || ip link show || LC_ALL=C ifconfig -a
} 2>/dev/null | command sed -ne \
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]].*/\1/p" -ne \
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]]*$/\1/p" -ne \
Expand Down Expand Up @@ -1697,7 +1697,7 @@ _comp_compgen_ip_addresses()
local PATH=$PATH:/sbin
local addrs
_comp_compgen -v addrs split -- "$({
LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show
ip -c=never addr show || ip addr show || LC_ALL=C ifconfig -a
} 2>/dev/null |
command sed -e 's/[[:space:]]addr:/ /' -ne \
"s|.*inet${_n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")" ||
Expand Down Expand Up @@ -1731,9 +1731,12 @@ _comp_compgen_available_interfaces()
if [[ ${1-} == -w ]]; then
iwconfig
elif [[ ${1-} == -a ]]; then
ifconfig || ip -c=never link show up || ip link show up
# Note: we prefer ip (iproute2) to inetutils (ifconfig) since long
# interface names will be truncated by ifconfig [1].
# [1]: https://github.com/scop/bash-completion/issues/1089
ip -c=never link show up || ip link show up || ifconfig
else
ifconfig -a || ip -c=never link show || ip link show
ip -c=never link show || ip link show || ifconfig -a
fi
} 2>/dev/null | _comp_awk \
'/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" &&
Expand Down
36 changes: 36 additions & 0 deletions test/fixtures/shared/bin/ip
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

# Dummy "ip addr show" and "ip link show up" emulator

for arg in "$@"; do
case "$arg" in
link)
cat <<EOF
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
EOF
exit 0
;;
addr)
cat <<EOF
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.80.11/24 brd 192.168.80.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::000:0000:0000:0000/64 scope link
valid_lft forever preferred_lft forever
EOF
exit 0
;;
esac
done

exit 1

0 comments on commit a3fdc0d

Please sign in to comment.