#!/bin/bash

function returnIP() { local NIC=$1 local IP=$(ifconfig $NIC | grep 'inet addr' | cut -d ':' -f 2 | cut -d ' ' -f 1) if [ -z "$IP" ]; then echo "IP not found for $NIC" else echo "$IP" fi }

IP=$(returnIP eth0) if [ -z "$IP" ]; then IP=$(returnIP bond0) if [ -z "$IP" ]; then IP=$(returnIP net0) fi fi

echo "IP: $IP"

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

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

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