mirror of
https://gitee.com/hotalexnet/openbsd.git
synced 2025-12-26 17:44:57 +08:00
58 lines
2.2 KiB
Bash
Executable file
58 lines
2.2 KiB
Bash
Executable file
#!/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 绿色[2,5](@ref)
|
||
gsub(/BROADCAST/, "\033[33mBROADCAST\033[0m", flags_str) # BROADCAST 黄色[2,5](@ref)
|
||
gsub(/RUNNING/, "\033[32mRUNNING\033[0m", flags_str) # RUNNING 绿色[2,5](@ref)
|
||
gsub(/SIMPLEX/, "\033[34mSIMPLEX\033[0m", flags_str) # SIMPLEX 蓝色[2,5](@ref)
|
||
gsub(/MULTICAST/, "\033[35mMULTICAST\033[0m", flags_str) # MULTICAST 紫色[2,5](@ref)
|
||
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" }
|
||
'
|