hotalexnet/color_ifconfig
2025-09-16 17:03:52 +08:00

58 lines
2.1 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
ifconfig $@ | awk '
# 接口名称(蓝色加粗),处理 mtu 数值为青色
/^[a-z]+[0-9]+:/ {
interface = substr($0, 1, index($0, ":")+0) # 提取接口名(如 "em0:"
rest = substr($0, length(interface)+1) # 剩余部分flags、mtu 等)
# 处理状态标志UP/BROADCAST/RUNNING/SIMPLEX/MULTICAST
if (match(rest, /<[^>]+>/)) {
flags_str = substr(rest, RSTART, RLENGTH)
gsub(/UP/, "\033[32mUP\033[0m", flags_str) # UP 绿色
gsub(/BROADCAST/, "\033[33mBROADCAST\033[0m", flags_str) # BROADCAST 黄色
gsub(/RUNNING/, "\033[32mRUNNING\033[0m", flags_str) # RUNNING 绿色
gsub(/SIMPLEX/, "\033[34mSIMPLEX\033[0m", flags_str) # SIMPLEX 蓝色
gsub(/MULTICAST/, "\033[35mMULTICAST\033[0m", flags_str) # MULTICAST 紫色
rest = substr(rest, 1, RSTART-1) flags_str substr(rest, RSTART+RLENGTH)
}
# 手动处理 mtu 后的数字
if (match(rest, /mtu [0-9]+/)) {
mtu_str = substr(rest, RSTART, RLENGTH)
split(mtu_str, parts, " ")
colored_mtu = "mtu \033[36m" parts[2] "\033[0m"
rest = substr(rest, 1, RSTART-1) colored_mtu substr(rest, RSTART+RLENGTH)
}
printf "\033[1;34m%s\033[0m%s\n", interface, rest
next
}
# IPv4 地址inet 黄色IP 和子网掩码绿色)
/inet / {
sub(/inet /, "\033[33minet \033[0m")
gsub(/(([0-9]+\.){3}[0-9]+)|(0x[0-9a-fA-F]{8})/, "\033[32m&\033[0m")
print
next
}
# IPv6 地址inet6 紫色,地址蓝色)
/inet6 / {
sub(/inet6 /, "\033[35minet6 \033[0m")
gsub(/([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/, "\033[34m&\033[0m")
print
next
}
# MAC 地址lladdr 标签紫色MAC 地址红色)
/lladdr / {
sub(/lladdr /, "\033[35mlladdr \033[0m")
gsub(/([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}/, "\033[31m&\033[0m")
print
next
}
# 默认行(白色无格式)
{ print "\033[0m" $0 "\033[0m" }
'