CentOS 7 获取网卡 IP 地址 Shell 脚本:eth0、bond0、net0
以下是一个示例的 shell 脚本,可以根据不同的网卡名称获取 IP 地址:
#!/bin/bash
# 获取 eth0 网卡的 IP 地址
get_eth0_ip() {
ip=$(ifconfig eth0 | grep 'inet ' | awk '{print $2}')
echo $ip
}
# 获取 bond0 网卡的 IP 地址
get_bond0_ip() {
ip=$(ifconfig bond0 | grep 'inet ' | awk '{print $2}')
echo $ip
}
# 获取 net0 网卡的 IP 地址
get_net0_ip() {
ip=$(ifconfig net0 | grep 'inet ' | awk '{print $2}')
echo $ip
}
# 主函数
main() {
ip=$(get_eth0_ip)
if [ -z "$ip" ]; then
ip=$(get_bond0_ip)
if [ -z "$ip" ]; then
ip=$(get_net0_ip)
fi
fi
echo 'IP address: $ip'
}
# 调用主函数
main
可以根据需要修改函数中的网卡名称和获取 IP 地址的方法。在主函数中,先尝试获取 eth0 网卡的 IP 地址,如果不存在则尝试获取 bond0 网卡的 IP 地址,如果还不存在则尝试获取 net0 网卡的 IP 地址。如果所有网卡都不存在,则输出空字符串。最后输出获取到的 IP 地址。
原文地址: https://www.cveoy.top/t/topic/j41Y 著作权归作者所有。请勿转载和采集!