以下是一个示例的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地址

centos7网卡名称可能是eth0 可能是bond0 可能是 net0 请获取网卡IP如果eth0卡不存则去获取bond0以次类推写出shell使用函数方式返回IP再写个main函数输出获取的IP

原文地址: http://www.cveoy.top/t/topic/cEht 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录