【shell脚本】系统硬件信息数据库收集(普通版和导入Excel版)

自动获取服务器系统信息:

创建数据库表(这里没有写入数据库中):

[root@rhel8 shell]# vim audit_audit_system.sql
CREATE TABLE `audit_audit_system`(
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `ip_info` varchar(50) NOT NULL,
        `serv_info` varchar(50) NOT NULL,
        `cpu_info` varchar(50) NOT NULL,
        `disk_info` varchar(50) NOT NULL,
        `mem_info` varchar(50) NOT NULL,
        `load_info` varchar(50) NOT NULL,
        `mark_info` varchar(50) NOT NULL,
        PRIMARY KEY(`id`),
        UNIQUE KEY `ip_info`(`ip_info`),
        UNIQUE key `ip_info_2`(`ip_info`)`
);

脚本:

[root@rhel8 shell]# cat auto_system_collect.sh
#!/bin/bash
# auto get system info
# by author tanbaobao 2020/06/11

echo -e "\033[34m \033[1m"

cat<<EOF
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++ Welcome to use system Collect +++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF

ip_info=`ifconfig | grep -w "inet" | sed -n '1p' | awk '{print $2}' | sed 's/ $//g'`
# $NF打印最后一个字段
cpu_info1=`cat /proc/cpuinfo | grep "model name" | sed -n '1p' | awk -F: '{print $2}' | sed 's/^ //g' |awk '{
print $1,$3,$4,$NF}'`
cpu_info2=`cat /proc/cpuinfo | grep "physical id" | sort | uniq -c | wc -l` serv_info=`hostname |tail -1` disk_info=`fdisk -l | grep "Disk" | sed -n '1p' | awk '{print $2,$3,$4,$5}' | sed 's/,.*//g'` mem_info=`free -m |grep "Mem" | awk '{print "Total",$1,$2"M"}'` load_info=`uptime |awk '{print "Current Load:"$(NF-2)}' | sed 's/\,//g'` mark_info='Tanbaobao' echo -e "\033[32m--------------------------------------------------------------\033[1m" echo IPADDR:${ip_info} echo HOSTNAME:$serv_info echo CPU_INFO:${cpu_info1} X${cpu_info2} echo DISK_INFO:$disk_info echo MEN_INFO:$mem_info echo LOAD_INFO:$load_info echo -e "\033[32m--------------------------------------------------------------\033[0m" echo -e -n "\033[36m You Want To Write The Data To The Databases?\033[1m";read ensure if [ "$ensure" == "yes" -o "$ensure" == "y" -o "$ensure" == "Y" -o "ensure" == "YES" ];then echo "------------------------------------------------------------------" echo -e "\033[31mmysql -uaudit -p123456 -D audit -e \ insert into audit_audit_system values \ ( \ '','${ip_info}','$serv_info','${cpu_info1}X${cpu_info2}','$disk_info','$mem_info','$load_info','$mark_info')\ 033[0m" else echo "wait exit..." exit fi

 

写入到Excel中:

这里可以配置下sz和rz命令:https://www.cnblogs.com/HeiDi-BoKe/p/13093987.html

[root@rhel8 shell]# cat auto_system_collect.sh 
#!/bin/bash
# auto get system info
# by author tanbaobao 2020/06/11

echo -e "\033[34m \033[1m"

cat<<EOF
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++ Welcome to use system Collect +++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF

ip_info=`ifconfig | grep -w "inet" | sed -n '1p' | awk '{print $2}' | sed 's/ $//g'`
# $NF打印最后一个字段
cpu_info1=`cat /proc/cpuinfo | grep "model name" | sed -n '1p' | awk -F: '{print $2}' | sed 's/^ //g' |awk '{print $1,$3,$4,$NF}'`
cpu_info2=`cat /proc/cpuinfo | grep "physical id" | sort | uniq -c | wc -l`
serv_info=`hostname |tail -1`
disk_info=`fdisk -l | grep "Disk" | sed -n '1p' | awk '{print $2,$3,$4,$5}' | sed 's/,.*//g'`
mem_info=`free -m |grep "Mem" | awk '{print "Total",$1,$2"M"}'`
load_info=`uptime |awk '{print "Current Load:"$(NF-2)}' | sed 's/\,//g'`
mark_info='Tanbaobao'
F_RESULT_DIR=`pwd`
F_NAME=get_system_info.csv

echo -e "\033[32m--------------------------------------------------------------\033[1m"
# echo "" > $F_NAME
# 标题
if [ ! -f "$F_NAME" ];then
    echo "IPADDR,HOSTNAME,CPU_INFO,DISK_INFO:,MEN_INFO,LOAD_INFO" >>$F_NAME
# 输出到文件
# echo "IPADDR:${ip_info},HOSTNAME:$serv_info,CPU_INFO:${cpu_info1} X${cpu_info2},DISK_INFO:$disk_info,MEN_INFO:$mem_info,LOAD_INFO:$load_info" >>$F_NAME
    echo "${ip_info},$serv_info,${cpu_info1}X${cpu_info2},$disk_info,$mem_info,:$load_info" >>$F_NAME
else
    echo "${ip_info},$serv_info,${cpu_info1}X${cpu_info2},$disk_info,$mem_info,:$load_info" >>$F_NAME

fi
echo "System information has been output to the $F_RESULT_DIR/$F_NAME, please check"
echo -e "\033[32m--------------------------------------------------------------\033[0m"

echo -e -n "\033[36mYou Want To Write The Data To The Databases?\033[1m";read ensure

if [ "$ensure" == "yes" -o "$ensure" == "y" -o "$ensure" == "Y" -o "ensure" == "YES" ];then
    echo "------------------------------------------------------------------"
    echo -e "\033[31mmysql -uaudit -p123456 -D audit -e \
insert into audit_audit_system values \
( \
'','${ip_info}','$serv_info','${cpu_info1}X${cpu_info2}','$disk_info','$mem_info','$load_info','$mark_info')\033[0m"

else
    echo "wait exit..."
    exit
fi

 

将导出的csv文件下载到本地,就可以看到Excel格式的文件了

[root@rhel8 shell]# sz get_system_info.csv 

【shell脚本】系统硬件信息数据库收集(普通版和导入Excel版)

 

上一篇:天擎_越权访问&天擎_前台sql注入复现


下一篇:利用shell编程,提取注释文件中指定范围内的行