Linux下显示网络信息的Shell脚本

2019年10月21日

这个脚本是deepin论坛上一个网友的问题,在那个脚本的基础上修改的。可能代码要比原文的代码多一些,但获取更准确一些。多网卡获取,可以获取mac地址等信息 原文:https://bbs.deepin.org/forum.php?mod=viewthread&tid=149000

Shell源码

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#! /bin/bash
#***************************************************************************************************************************************************#
# Author: RucLinux #
# Web: https://www.myzhenai.com.cn https://www.myzhenai.com https://www.haikou-china.com https://jiayu.mybabya.com #
#***************************************************************************************************************************************************#
#网卡接口
indx=$(ifconfig -a|grep encap|awk -F " " '{ print $1 }')
OLD_IFD="$IFD"
IFD=" "
arr=($indx)
IFD="$OLD_IFD"
for d in ${arr[@]}
do
#echo $d
na=$(echo ${d:0:3})
if [ ${na} == "eth" ]; then
#name=$(ethtool -i ${d}|grep driver:|awk -F "driver:" '{ print $2 }')
txqueuelen=$(ethtool ${d}|grep Speed:|awk -F "Speed:" '{ print $2 }')
mac=$(ifconfig ${d} | sed -n '/HWaddr/ s/^.*HWaddr *//pg')
fi
ma=${arr[@]}:
md=$(echo ${ma}|sed 's/:/ /g')
done
#ip=`ifconfig | awk -F'[ ]+|:' '/inet addr/{if($4!~/^192.168|^172.16|^10|^127|^0/) print $4}'`
#服务器上的多个IP地址
ip=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|awk -F ":" '{ print $2 }')
#子网掩码
mask=$(ifconfig -a|grep ${ip}|awk -F "Mask:" '{ print $2 }')
#默认网关
gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
#dns
dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
cpu=$(cat /proc/cpuinfo | grep name | awk -F ":" '{ print $2 }' | sed -n '1p;1q')
name=$(lspci -v | grep Ethernet -A 8 | awk -F "use:" '{ print $2 }')
crad=$(lspci -v | grep VGA -A 12 | grep VGA | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }')
disk=$(lspci -v | grep IDE | grep interface | awk -F "interface:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/IDE//')
board=$(lspci -v | grep bridge | grep Host | awk -F "bridge:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Host bridge//')
net=$(lspci -v | grep Ethernet -A 8 |awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }')
audo=$(lspci -v | grep audio | grep controller | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Ensoniq//')
mon=$(dmidecode -t system | grep Product | awk -F "Name:" '{ print $2 }')
echo 网络名称: ${name}
echo 网卡接口: ${md}
echo ip 地 址: ${ip}
echo 子网掩码: ${mask}
#echo 广播地址: $broadcast
echo 默认网关: ${gateway}
echo 首选 DNS: ${dns}
echo MAC 地址: ${mac}
echo 网卡速率: ${txqueuelen}
echo " "
echo CPU型 号: ${cpu}
echo 显卡名称: ${crad}
echo 硬盘接口: ${disk}
echo 主板名称: ${board}
echo 网卡名称: ${net}
echo 声卡名称: ${audo}
echo 显 示 器:${mon}
#! /bin/bash #***************************************************************************************************************************************************# # Author: RucLinux # # Web: https://www.myzhenai.com.cn https://www.myzhenai.com https://www.haikou-china.com https://jiayu.mybabya.com # #***************************************************************************************************************************************************# #网卡接口 indx=$(ifconfig -a|grep encap|awk -F " " '{ print $1 }') OLD_IFD="$IFD" IFD=" " arr=($indx) IFD="$OLD_IFD" for d in ${arr[@]} do #echo $d na=$(echo ${d:0:3}) if [ ${na} == "eth" ]; then #name=$(ethtool -i ${d}|grep driver:|awk -F "driver:" '{ print $2 }') txqueuelen=$(ethtool ${d}|grep Speed:|awk -F "Speed:" '{ print $2 }') mac=$(ifconfig ${d} | sed -n '/HWaddr/ s/^.*HWaddr *//pg') fi ma=${arr[@]}: md=$(echo ${ma}|sed 's/:/ /g') done #ip=`ifconfig | awk -F'[ ]+|:' '/inet addr/{if($4!~/^192.168|^172.16|^10|^127|^0/) print $4}'` #服务器上的多个IP地址 ip=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|awk -F ":" '{ print $2 }') #子网掩码 mask=$(ifconfig -a|grep ${ip}|awk -F "Mask:" '{ print $2 }') #默认网关 gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}') #dns dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}') cpu=$(cat /proc/cpuinfo | grep name | awk -F ":" '{ print $2 }' | sed -n '1p;1q') name=$(lspci -v | grep Ethernet -A 8 | awk -F "use:" '{ print $2 }') crad=$(lspci -v | grep VGA -A 12 | grep VGA | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }') disk=$(lspci -v | grep IDE | grep interface | awk -F "interface:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/IDE//') board=$(lspci -v | grep bridge | grep Host | awk -F "bridge:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Host bridge//') net=$(lspci -v | grep Ethernet -A 8 |awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }') audo=$(lspci -v | grep audio | grep controller | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Ensoniq//') mon=$(dmidecode -t system | grep Product | awk -F "Name:" '{ print $2 }') echo 网络名称: ${name} echo 网卡接口: ${md} echo ip 地 址: ${ip} echo 子网掩码: ${mask} #echo 广播地址: $broadcast echo 默认网关: ${gateway} echo 首选 DNS: ${dns} echo MAC 地址: ${mac} echo 网卡速率: ${txqueuelen} echo " " echo CPU型 号: ${cpu} echo 显卡名称: ${crad} echo 硬盘接口: ${disk} echo 主板名称: ${board} echo 网卡名称: ${net} echo 声卡名称: ${audo} echo 显 示 器:${mon}
#! /bin/bash
#***************************************************************************************************************************************************#
# Author: RucLinux                                                                                             #  
# Web: https://www.myzhenai.com.cn https://www.myzhenai.com https://www.haikou-china.com https://jiayu.mybabya.com #
#***************************************************************************************************************************************************#
#网卡接口
indx=$(ifconfig -a|grep encap|awk -F " " '{ print $1 }')
OLD_IFD="$IFD"
IFD=" "
arr=($indx)
IFD="$OLD_IFD"
for d in ${arr[@]}
do
#echo $d
na=$(echo ${d:0:3})
if [ ${na} == "eth" ]; then
#name=$(ethtool -i ${d}|grep driver:|awk -F "driver:" '{ print $2 }')
txqueuelen=$(ethtool ${d}|grep Speed:|awk -F "Speed:" '{ print $2 }')
mac=$(ifconfig ${d} | sed -n '/HWaddr/ s/^.*HWaddr *//pg')
fi
ma=${arr[@]}:
md=$(echo ${ma}|sed 's/:/ /g')
done

#ip=`ifconfig | awk -F'[ ]+|:' '/inet addr/{if($4!~/^192.168|^172.16|^10|^127|^0/) print $4}'`
#服务器上的多个IP地址
ip=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|awk -F ":" '{ print $2 }')

#子网掩码
mask=$(ifconfig -a|grep ${ip}|awk -F "Mask:" '{ print $2 }')
#默认网关
gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}') 
#dns
dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
cpu=$(cat /proc/cpuinfo | grep name | awk -F ":" '{ print $2 }' | sed -n '1p;1q')
name=$(lspci -v | grep Ethernet -A 8 | awk -F "use:" '{ print $2 }')
crad=$(lspci -v | grep VGA -A 12 | grep VGA | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }')
disk=$(lspci -v | grep IDE | grep interface | awk -F "interface:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/IDE//')
board=$(lspci -v | grep bridge | grep Host | awk -F "bridge:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Host bridge//')
net=$(lspci -v | grep Ethernet -A 8 |awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }')
audo=$(lspci -v | grep audio | grep controller | awk -F "controller:" '{ print $2 }' | awk -F "(" '{ print $1 }' | sed 's/Ensoniq//')
mon=$(dmidecode -t system | grep Product | awk -F "Name:" '{ print $2 }')
echo 网络名称: ${name}
echo 网卡接口:  ${md}
echo ip 地 址: ${ip}
echo 子网掩码: ${mask}
#echo 广播地址: $broadcast
echo 默认网关: ${gateway}
echo 首选 DNS: ${dns}
echo MAC 地址: ${mac}
echo 网卡速率:  ${txqueuelen}
echo "            "
echo CPU型 号:    ${cpu}
echo 显卡名称:    ${crad}
echo 硬盘接口:    ${disk}
echo 主板名称:    ${board}
echo 网卡名称:    ${net}
echo 声卡名称:    ${audo}
echo 显 示 器:${mon}

 


sicnature ---------------------------------------------------------------------
I P 地 址: 18.119.139.161
区 域 位 置: 美国马萨诸塞
系 统 信 息: 美国
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://myzhenai.com/post/2898.html

没有评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注