#!/bin/bash
#1-获取指定ip mac id 2-获取macidlsit iplist

system_type=''
system=$(uname)
if [ "$system" == "Darwin" ]; then
  # 这里是 macOS 下的代码
  system_type='IOS'
else
  # 这里是 Linux 下的代码
  system_type='OTHER'
fi


# 定义支持命令的常量
OS_SUPPORT_IFCONFIG="OS_SUPPORT_IFCONFIG"
OS_SUPPORT_IP="OS_SUPPORT_IP"

# 初始化变量，默认假设支持 ifconfig
system_support_cmd=$OS_SUPPORT_IFCONFIG

# 检查命令是否可用的函数
is_command_supported() {
    command -v "$1" >/dev/null 2>&1
}

# 检查 ifconfig 命令是否支持
if is_command_supported "ifconfig"; then
    # echo "System supports 'ifconfig' command."
    system_support_cmd=$OS_SUPPORT_IFCONFIG
else
    # echo "System does not support 'ifconfig' command."

    # 如果不支持 ifconfig，检查 ip 命令是否支持
    if is_command_supported "ip"; then
        system_support_cmd=$OS_SUPPORT_IP
        # echo "System supports 'ip' command."
    fi
fi


get_mac_id()
{
    if [ "$system_type" == "IOS" ]; then
        macid=$(ifconfig $1 | awk '/ether/{print $NF}')
    elif [ "$system_support_cmd" == "OS_SUPPORT_IP" ]; then
        macid=$(ip link show $1 | awk '/ether/{print $2}')
    else
        macaddr=$1
        pcimacdir="/sys/class/net/"
        #先获取所有本地网卡名称
        if [ ! -d $pcimacdir ];then
            echo "NotSupportThisOs!"
            exit 1
        fi

        cd $pcimacdir
        cd $macaddr
        macid=`cat address`
    fi
    echo "$macid"
}

get_mac_id_()
{
    if [ "$system_type" == "IOS" ]; then
        macid=$(ifconfig $1 | awk '/ether/{print $NF}')
    elif [ "$system_support_cmd" == "OS_SUPPORT_IP" ]; then
        macid=$(ip link show $1 | awk '/ether/{print $2}')
    else
        macaddr=$1
        pcimacdir="/sys/class/net/"
        #先获取所有本地网卡名称
        if [ ! -d $pcimacdir ];then
	    echo "NotSupportThisOs!"
	    exit 1
        fi

        cd $pcimacdir
        cd $macaddr
        macid=`cat address`
    fi
    echo "$macid" | sed 's/:/-/g'
}

get_mac_ip_list_info()
{
    if [ "$system_type" == "IOS" ]; then
        local macname_array
        macname_array=$(ifconfig | grep -E 'en'  | grep -v "\t" | awk -F':' '{print $1}' |sort|uniq)
        for name in ${macname_array}
        do
            ipv4=$(ifconfig $name | grep "inet\ " | awk '{ print $2}')
            if [ "$ipv4" != "" ]; then
                macid=$(ifconfig $name | awk '/ether/{print $NF}')
                maclist=$maclist$macid','
                ipv4list=$ipv4list$ipv4','
            fi

            ipv6=$(ifconfig $name | grep "inet6\ "| awk '{ print $2}')
            if [ "$ipv6" != "" ]; then
                if [ "$ipv4" == "" ]; then
                    macid=$(ifconfig $name | awk '/ether/{print $NF}')
                    maclist=$maclist$macid','
                fi
                ipv6list=$ipv6list$ipv6','
            fi
        done
        echo "maclist=${maclist%?};" | sed 's/:/-/g'
        echo "ipv4list=${ipv4list%?};ipv6list=${ipv6list%?};" | sed ':a;N;s/\n/|/g;ta'
    elif [ "$system_support_cmd" == "OS_SUPPORT_IP" ]; then
        local macname_array
        macname_array=$(ip link show | grep -E '^[0-9]+: ' | awk -F': ' '{print $2}' | awk -F'@' '{print $1}' | grep -E 'enp|ens|eth|wlan|wlp' | awk -F: '{print $1}' | sort | uniq)
        for name in ${macname_array}; do
            macid=$(ip link show $name | awk '/ether/{print $2}')
            if [ -n "$macid" ]; then
                maclist=${maclist}${macid},
            fi

            ipv4=$(ip addr show $name | grep -E 'inet ' | awk '{print $2}' | cut -d'/' -f1)
            if [ -n "$ipv4" ]; then
                ipv4list=${ipv4list}${ipv4},
            fi

            ipv6=$(ip addr show $name | grep -E 'inet6 ' | awk '{print $2}' | cut -d'/' -f1)
            if [ -n "$ipv6" ]; then
                ipv6list=${ipv6list}${ipv6},
            fi
        done
        echo "maclist=${maclist%?};" | sed 's/:/-/g'
        echo "ipv4list=${ipv4list%?};ipv6list=${ipv6list%?};"
    else
        pcimacdir="/sys/class/net/"
        #先获取所有本地网卡名称
        if [ ! -d $pcimacdir ];then
            echo "NotSupportThisOs!"
            exit 1
        fi
        local macname_array
        macname_array=$(ifconfig -a|grep -E 'eth|trunk|enp|ens|eno|ena|wlan|wlp'|grep -v 'ether'|awk '{print $1}'|awk -F ':' '{print $1}'|sort|uniq)
        for name in ${macname_array}
        do
            cd $pcimacdir
            cd $name
            macid=`cat address`
            maclist=$maclist$macid','
            ipv4=$(ip addr show dev $name | sed -e's/^.*inet \([^ ]*\)\/.*$/\1/;t;d')
            ipv6=$(ip addr show dev $name | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d')
            ipv4list=$ipv4list$ipv4','
            ipv6list=$ipv6list$ipv6','
        done
        echo "maclist=${maclist%?};" | sed 's/:/-/g'
        echo "ipv4list=${ipv4list%?};ipv6list=${ipv6list%?};" | sed ':a;N;s/\n/|/g;ta'
    fi
}

if [ $2 = "getmacid" ] ; then
        get_mac_id $1
fi

if [ $1 = "getmac_iplist" ] ; then
        get_mac_ip_list_info
fi

if [ $2 = "set_macid" ] ; then
        get_mac_id_ $1
fi
