shell获取网口和ip,选择网口Ip列表
#!/bin/sh array=() getEno(){ network_name=`ifconfig |awk '{print $1}'|grep :|awk -F: '{print $1}'` # 按网卡名遍历 for net_name in ${network_name} do if [ ${net_name} = 'lo' ] then continue fi ifconfig_num=`ifconfig ${net_name}|grep 'IEEE'|wc -l` if [ ${ifconfig_num} -ge 1 ] then running_num=`ifconfig ${net_name}|grep RUNNING|wc -l` if [ ${running_num} -ge 1 ] then wireless_name=${net_name} wireless_ip=`ifconfig ${wireless_name}|grep 'inet'|awk '{print $2}'|sed -n '1p'` array=("${array[@]}" ${wireless_name},${wireless_ip}) else wireless_name=${net_name} wireless_ip=`ifconfig ${wireless_name}|grep 'inet'|awk '{print $2}'|sed -n '1p'` array=("${array[@]}" ${wireless_name},${wireless_ip}) fi else running_num=`ifconfig ${net_name}|grep RUNNING|wc -l` if [ ${running_num} -ge 1 ] then wired_name=${net_name} wired_ip=`ifconfig ${wired_name}|grep 'inet'|awk '{print $2}'|sed -n '1p'` array=("${array[@]}" ${wired_name},${wired_ip}) else wired_name=${net_name} wired_ip=`ifconfig ${wired_name}|grep 'inet'|awk '{print $2}'|sed -n '1p'` array=("${array[@]}" ${wired_name},${wired_ip}) fi fi done } getIpAddr() { getEno num=${#array[@]} if [ $num -ge 1 ];then echo -e "\033[035m******************************\033[0m" echo -e "\033[036m* 请选择本机真实IP地址(序号) \033[0m" for ((i=0;i<${#array[@]};i++)) do echo -e "\033[032m* 【$i】 ${array[i]} \033[0m" done echo -e "\033[035m******************************\033[0m" input="" while : do read -r -p "*请选择配置文件的本机IP地址(序号): " input expr $input + 1 >/dev/null 2>&1 if [ $? -ne 0 ];then echo "*请输入有效的序号:" else if [ $(($input)) -ge 0 ] && [ $(($input)) -lt $(($num)) ];then local_ip=${array[$input]} break else echo "请输入有效范围的序号:" fi fi done else echo -e "\033[31m*未设置网卡IP,请检查服务器环境! \033[0m" exit 1 fi } local_ip='' getIpAddr array=(${local_ip//,/ }) ip='' num=${#array[@]} enp=${array[0]} echo $enp ip=${array[1]} echo $ip
效果: