此脚本可以实现获取linux版本下,每个网卡的IP地址、MAC、掩码、速率
格式化输出,
linux主机的主机名、IP地址、内存大小、CPU型号、SN号,便于运维人员做主机信息统计。
#!/usr/bin/bash
#获取主机基本信息
#Version: v1.0
#Auth: Abel
#Date: 2020-06-16
#检查网卡管理工具
which ethtool &>/dev/null
if [ $? -ne 0 ];then
yum install ethtool -y &>/dev/null
fi
#查看网卡对应的IP地址、掩码、Mac、速率
for DEV in `cat /proc/net/dev|sed 1,2d|grep -v lo|awk -F":" '{print $1}'`
do
IP=$(ip addr show dev $DEV|grep inet|grep -v inet6|awk '{print $2}')
MAC=$(ip addr show $DEV|grep 'link/ether'|awk '{print $2}')
SPEED=$(ethtool ${DEV}|grep "Speed:"|awk -F":" '{print $2}')
printf "%-12s %-20s %-15s %-5s\n" "Net:${DEV}" "Ip:${IP}" "Mac:${MAC}" "Speed:${SPEED}"
done
#查看网关信息
GATEWAY=$(ip r|grep "default via"|awk '{print $3}')
echo Gateway:${GATEWAY}
#服务器型号、内存大小、CPU型号、IP地址、主机名、序列号
SN=$(dmidecode -t system|grep "Serial Number:"|awk -F" " '{print $3}')
#服务器型号
FWQXH=$(dmidecode -t system|grep Product|awk '{print $3}')
FWQCJ=$(dmidecode -t system|grep "Manufacturer"|awk '{print $2}')
#内存大小
MEM_SIZE=$(dmidecode -t memory|grep -A 5 "Memory Device"|grep "Size"|grep -v "No Module Installed"|sort|uniq -c|awk '{print $1*$3/1024 "GB"}')
#CPU型号
CPU_INFO=$(dmidecode -t processor|grep "Version:"|awk -F":" '{print $2}'|sort|uniq -d)
#IP地址
IP_ADDRESS=$(ip addr show|grep inet|grep -v inet6|grep -v "127.0.0.1"|awk '{print $2}'|awk -F"/" '{print $1}')
#主机名
HOST_NAME=$(hostname)
#格式化输出
printf "%-25s %-18s %-18s %-15s %-40s %s\n" "主机名" "IP地址" "服务器型号" "内存" "CPU" "SN"
printf "%-15s %-15s %-15s %-4s %-15s %s\n" "${HOST_NAME}" "${IP_ADDRESS}" "${FWQCJ} ${FWQXH}" "${MEM_SIZE}" "${CPU_INFO}" "${SN}"