使用Shell脚本获取CentOS 7服务器软硬件信息

学向勤中得,萤窗万卷书。这篇文章主要讲述使用Shell脚本获取CentOS 7服务器软硬件信息相关的知识,希望能为你提供帮助。
【使用Shell脚本获取CentOS 7服务器软硬件信息】1、自己编写的Shell脚本中会用到的一些命令:

# which ifconfig



# rpm -qf /usr/sbin/ifconfig



# which wget



# rpm -qf /usr/bin/wget



# which iostat



# rpm -qf /usr/bin/iostat



说明:
(1)安装sysstat软件包过程中需要安装依赖软件包lm_sensors-libs;
(2)某些特定环境下可能无法直接联网使用yum或挂盘搭建yum仓库安装软件包,提前准备好上述net-tools-2.0-0.25.20131004git.el7.x86_64.rpm、wget-1.14-18.el7_6.1.x86_64.rpm、lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm、sysstat-10.1.5-19.el7.x86_64.rpm软件包,并上传至服务器的/tmp目录下;
(3)为了演示自动安装软件包的效果,提前先将这些软件包删除:# yum -y remove net-tools wget sysstat。
 
2、编写Shell脚本:
# mkdir -pv /scripts
# cd /scripts
# vim qiuyue.sh




#!/bin/bash
#
# Author:QiuYue
# Blog:https://blog.51cto.com/qiuyue
# Date:2022-06-02

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

# 定义echo命令输出后的字体颜色
colour_title=\\033[33; 1m
colour_info_key=\\033[36; 1m
colour_info_value=https://www.songbingjia.com//033[0m
colour_warning=\\033[31; 1m

# 定义函数
# 打印脚本标题
function print_title()
echo -e "==============================================================================="
echo -e "Tool From QiuYue"
echo -e "Blog:https://blog.51cto.com/qiuyue"
echo -e "==============================================================================="


# 判断软件包是否安装,软件包名称由脚本传入,如果软件包没有安装,则执行安装操作,软件包提前上传至服务器的/tmp目录下
install_packages()
if ! rpm -q $1 & > /dev/null; then
echo -e "$colour_warningWarning:$1 is not installed, installing now."
yum -y localinstall /tmp/$1*.rpm & > /dev/null & & echo -e "$colour_info_keyMessage:$colour_info_value$1 is installed successfully."
else
echo -e "$colour_info_keyMessage:$colour_info_value$1 has been installed."
fi


# 检查防火墙状态
check_firewall()
firewall-cmd --state & > /dev/null
if [ $? -ne 0 ]; then
echo -e "$colour_warningFirewall:disabled"
else
echo -e "$colour_info_keyFirewall:$colour_info_valueenabled"
fi


# 检查SELinux状态
check_selinux()
selinux_status=`getenforce`
if [ $selinux_status == "Disabled" ]; then
echo -e "$colour_warningSELinux:disabled"
elif [ $selinux_status == "Permissive" ]; then
echo -e "$colour_info_keySELinux:$colour_info_valuepermissive"
else
echo -e "$colour_info_keySELinux:$colour_info_valueenforcing"
fi


# 检查路由转发是否启用
check_ip_forward()
res=`cat /proc/sys/net/ipv4/ip_forward`
if [ $res -eq 1 ]; then
echo -e "$colour_info_keyIP Forward:$colour_info_valueenabled"
else
echo -e "$colour_info_keyIP Forward:$colour_warningdisabled"
fi


# 显示能够登录服务器的用户、UID和它的计划任务数量
list_users()
for shell in `grep -v "/sbin/nologin" /etc/shells`; do
for user in $(grep "$shell" /etc/passwd | awk -F: print $1); do
uid=`id -u "$user"`
crontabnum=0
crontab -u $user -l & > /dev/null
if [ $? -eq 0 ]; then
let crontabnum+=`crontab -u $user -l | wc -l`
fi
echo "$user:$uid:$crontabnum"
done
done


# 显示服务器所有网卡名称、内网IP、MAC地址和对应网关
list_interfaces()
# 获取类似eth0、ens33网卡名称,获取到的网卡名称可能不止一个
interface=`ifconfig | grep "^e" | awk -F: print $1`
# 采用遍历方式获取网卡对应IP
for i in $interface; do
private_ip=`ifconfig $i | grep -w inet | awk print $2`
mac=`ifconfig $i | grep -w ether | awk print $2`
gateway=`ip route | grep default | grep $i | awk print $3`
echo "$i#$private_ip#$mac#$gateway"
done


# 检查服务器是否能通公网
check_internet()
if wget -q --spider https://blog.51cto.com/; then
echo -e "$colour_info_keyInternet:$colour_info_valueconnected"
else
echo -e "$colour_info_keyInternet:$colour_warningdisconnected"
fi


# 如果服务器能通公网,显示公网IP,也可使用curl -s https://api.myip.la实现类似功能
list_publicip()
publicip=`wget -qO- -t2 -T2 ipinfo.io/ip`
[ -z $publicip ] & & publicip=$(wget -qO- -t2 -T2 ipv4.icanhazip.com)
[ -n $publicip ] & & echo -e "$colour_info_keyPublic IP:$colour_info_value$publicip" || echo -e "$colour_info_keyPublic IP:$colour_warningunknown"


# 定义变量
operation_system=`cat /etc/centos-release | tr A-Z a-z

    推荐阅读