企业日常巡检shell脚本
Linux内部查看系统信息
$INTERNET192忽略
1 [root@ mysql-master ~]# ip a|grep eth0|grep inet|cut -d' ' -f6|cut -d'/' -f1 2 10.0.0.21 3 4 [root@ mysql-master ~]# ifconfig eth0 |grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1 5 10.0.0.21
1 [root@localhost ~]# ifconfig |grep -w inet |awk '{print $2}'|sed -n '1p'
日常巡检完善脚本:
1 #!/bin/bash 2 3 function system(){ 4 echo "#########################系统信息#########################" 5 OS_TYPE=`uname` 6 OS_VER=`cat /etc/redhat-release` 7 OS_KER=`uname -a|awk '{print $3}'` 8 OS_TIME=`date +%F_%T` 9 OS_RUN_TIME=`uptime |awk '{print $3}'|awk -F, '{print $1}'` 10 OS_LAST_REBOOT_TIME=`who -b|awk '{print $2,$3}'` 11 OS_HOSTNAME=`hostname` 12 13 echo " 系统类型:$OS_TYPE" 14 echo " 系统版本:$OS_VER" 15 echo " 系统内核:$OS_KER" 16 echo " 当前时间:$OS_TIME" 17 echo " 运行时间:$OS_RUN_TIME" 18 echo "最后重启时间:$OS_LAST_REBOOT_TIME" 19 echo " 本机名称:$OS_HOSTNAME" 20 } 21 function network(){ 22 23 echo "#########################网络信息#########################" 24 INTERNET=(`ifconfig|grep ens|awk -F: '{print $1}'`) # centos6 ifconfig | grep Bcast | awk '{print $2}' | awk -F : '{print $2}' 25 for((i=0;i<`echo ${#INTERNET[*]}`;i++)) 26 do 27 OS_IP=`ifconfig ${INTERNET[$i]}|head -2|grep inet|awk '{print $2}'` 28 echo " 本机IP:${INTERNET[$i]}:$OS_IP" 29 done 30 curl -I http://www.baidu.com &>/dev/null # 测试 31 if [ $? -eq 0 ] 32 then echo " 访问外网:成功" 33 else 34 echo " 访问外网:失败" 35 fi 36 } 37 38 function hardware(){ 39 40 echo "#########################硬件信息#########################" 41 CPUID=`grep "physical id" /proc/cpuinfo |sort|uniq|wc -l` 42 CPUCORES=`grep "cores" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'` 43 CPUMODE=`grep "model name" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'` 44 45 echo " CPU数量: $CPUID" 46 echo " CPU核心:$CPUCORES" 47 echo " CPU型号:$CPUMODE" 48 49 MEMTOTAL=`free -m|grep Mem|awk '{print $2}'` 50 MEMFREE=`free -m|grep Mem|awk '{print $7}'` 51 52 echo " 内存总容量: ${MEMTOTAL}MB" 53 echo "剩余内存容量: ${MEMFREE}MB" 54 55 disksize=0 56 swapsize=`free|grep Swap|awk {'print $2'}` 57 partitionsize=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`) 58 for ((i=0;i<`echo ${#partitionsize[*]}`;i++)) 59 do 60 disksize=`expr $disksize + ${partitionsize[$i]}` 61 done 62 ((disktotal=\($disksize+$swapsize\)/1024/1024)) 63 64 echo " 磁盘总容量: ${disktotal}GB" 65 66 diskfree=0 67 swapfree=`free|grep Swap|awk '{print $4}'` 68 partitionfree=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk '{print $5}'`) 69 for ((i=0;i<`echo ${#partitionfree[*]}`;i++)) 70 do 71 diskfree=`expr $diskfree + ${partitionfree[$i]}` 72 done 73 74 ((freetotal=\($diskfree+$swapfree\)/1024/1024)) 75 76 echo "剩余磁盘容量:${freetotal}GB" 77 } 78 79 80 function secure(){ 81 echo "#########################安全信息#########################" 82 83 countuser=(`last|grep "still logged in"|awk '{print $1}'|sort|uniq`) 84 for ((i=0;i<`echo ${#countuser[*]}`;i++)) 85 do echo "当前登录用户:${countuser[$i]}" 86 done 87 88 md5sum -c --quiet /opt/passwd.db &>/dev/null 89 if [ $? -eq 0 ] 90 then echo " 用户异常:否" 91 else echo " 用户异常:是" 92 fi 93 } 94 95 function chksys(){ 96 system 97 network 98 hardware 99 secure 100 }
chksys
总结:
uname: 系统类型
cat /etc/redhat-relese: 系统版本
uname -a: 系统内核
date +%F_%T: 当前时间
uptime: 运行开机时间
who -b: 最后重启时间
hostname: 本机名称
curl -I http://www.baidu.com &>/dev/null
echo $? 如果返回值为0,则说明访问成功,返回为1,则访问外网失败
grep "physical id" /proc/cpuinfo CPU数量
grep "cores" /proc/cpuinfo CPU核心
model name" /proc/cpuinfo CPU型号
free -m|grep Mem 内存容量
df -T 磁盘容量
last 最后登录用户
last|grep "still logged in" 同上