1、Linux服务器批量巡检脚本使用说明文件说明该Shell脚本旨在针对大量Linux服务器的巡检提供一种相对自动化的解决方案。脚本组成有三部分:shellsh.sh、checksh.sh、file.txt;这三个文件需放在一个文件夹下以root权限执行,缺一不可。脚本用法将要巡检的服务器的IP地址和对应的密码全部放入file.txt中保存,每行一个IP对应一个密码即可。然后用如下命令运行:#./ shellsh.sh file.txt 192.168.182.143 123456其中file.txt可以更换文件名,192.168.182.143为你想保存巡检日志的到哪个服务器的服务器IP,123
2、456为该服务器的密码。运行结果运行完后会在192.168.182.143服务器的/tmp目录下升成一个目录,即:GatherLogDirectory这个目录下即存放的是被巡检的服务器的巡检日志,这些日志以被巡检的服务器的IP命名,形如:192.168.182.146.log。在被巡检的服务器上会升成两个目录即:CheckScript、 LocalServerLogDirectory;其中CheckScript中是checksh.sh脚本,LocalServerLogDirectory中存放的是checksh.sh在该服务器上运行后升成的日志。测试结果我只在虚拟机上的三台Linux系统上测试过
3、,分别是Ubuntu、RedHat、Kali。运行正常,平均巡检一个服务器花费3分钟。脚本代码shellsh.sh#!/bin/bashlogin_info=$1gather_server_ip=$2gather_server_password=$3grep_ip=ifconfig | grep (:digit:1,3.)3:digit:1,3 -color=auto -o | sed -e 2,5dGatherPath=/tmp/GatherLogDirectoryCheckScriptPath=/tmp/CheckScriptif $# -ne 3 ; then echo -e Param
4、eters if fault!n echo -e Please using:$0 login_info gather_server_ipn echo -e For example: $0 IpAndPassword.txt $grep_ipn exit;fiif ! -x $GatherPath ;then mkdir $GatherPath echo -e The logs path is: $GatherPathficat $login_info | while read linedo server_ip=echo $line|awk print $1 server_password=ec
5、ho $line|awk print $2 login_server_command=ssh -o StrictHostKeyChecking=no root$server_ip scp_gather_server_checksh=scp checksh.sh root$server_ip:$CheckScriptPath/usr/bin/expectEOF set timeout 20 spawn $login_server_command expect *yes/no send yesr; exp_continue *password: send $server_passwordr exp
6、ect Permission denied, please try again. exit expect # send mkdir $CheckScriptPathr expect eof exitEOF /usr/bin/expectEOF set timeout 20 spawn $scp_gather_server_checksh expect *yes/no send yesr; exp_continue *password: send $server_passwordr expect Permission denied, please try again. exit expect C
7、onnection refused exit expect 100% expect eof exitEOF /usr/bin/expectEOF set timeout 60 spawn $login_server_command expect *yes/no send yesr; exp_continue *password: send $server_passwordr expect Permission denied, please try again. exit expect # send cd $CheckScriptPath;./checksh.sh $gather_server_
8、ip $gather_server_passwordr expect eof exitEOFdonechecksh.sh#!/bin/bash#Function:#This script checks the systems information,diskss information,performance,etc.of the#server#Author:#By Jack Wang#Company:#ShaanXi Great Wall Information Co.,Ltd.#GatherServerIpAddress is the servers IP address that gat
9、her the checking log#GatherServerPassword is the servers IP address that gather the checking log#GatherServerIpAddress=$1GatherServerPassword=$2#GetTheIpCommand is a command that you can get the IP address#GetTheIpCommand=ifconfig | grep (:digit:1,3.)3:digit:1,3 -color=auto -o | sed -e 2,5d#LogName
10、is a command that Your logsname#LogName=ifconfig|grep (:digit:1,3.)3:digit:1,3 -color=auto -o|sed -e 2,5decho -date +%Y%M%d#GatherLogPath is a path that collecting log path#LocalServerLogPath is local log path#GatherServerLogPath=/tmp/GatherLogDirectoryLocalServerLogPath=/tmp/LocalServerLogDirectory
11、#LinuxOsInformation is function that usege to collect OSs information#LinuxOsInformation()Hostname=hostnameUnameA=uname -aOsVersion=cat /etc/issue | sed 2,4dUptime=uptime|awk print $3|awk -F , print $1ServerIp=ifconfig|grep inet|sed 2,4d|awk -F : print $2|awk print $1ServerNetMask=ifconfig|grep inet
12、|sed 2,4d|awk -F : print $4|awk print $1ServerGateWay=netstat -r|grep default|awk print $2SigleMemoryCapacity=dmidecode|grep -P -A5 Memorys+Device|grep Size|grep -v Range|grep 0-9|awk -F : print $2|sed s/ t*/gMaximumMemoryCapacity=dmidecode -t 16|grep Maximum Capacity|awk -F : print $2|sed s/ t*/gNu
13、mberOfMemorySlots=dmidecode -t 16|grep Number Of Devices|awk -F : print $2|sed s/ t*/gMemoryTotal=cat /proc/meminfo|grep MemTotal|awk printf(MemTotal:%1.0fGBn,$2/1024/1024)|awk -F : print $2PhysicalMemoryNumber=dmidecode|grep -A16 Memory Device|grep Size:|grep -v No Module Installed|grep -v Range Si
14、ze:|wc -lProductName=dmidecode|grep -A10 System Information|grep Product Name|awk -F : print $2|sed s/ t*/gSystemCPUInfomation=cat /proc/cpuinfo|grep name|cut -d: -f2|awk print *$1,$2,$3,$4|uniq -c|sed s/ t*/gecho -e Hostname|$HostnamenUnamea|$UnameAnOsVersion|$OsVersionnUptime|$UptimenServerIp|$Ser
15、verIpnServerNetMask|$ServerNetMasknServerGateWay|$ServerGateWaynSigleMemoryCapacity|$SigleMemoryCapacitynMaximumMemoryCapacity|$MaximumMemoryCapacitynNumberOfMemorySlots|$NumberOfMemorySlotsnMemoryTotal|$MemoryTotalnPhysicalMemoryNumber|$PhysicalMemoryNumbernProductName|$ProductNamenSystemCPUInforma
16、tion|$SystemCPUInfomationPerformanceInfomation ()CPUIdle=top -d 2 -n 1 -b|grep CPpUu|grep id|awk print $5|awk -F % print $1CPUloadAverage=top -d 2 -n 1 -b|grep load average:|awk -F : print $5|sed s/ t*/gProcessNumbers=top -d 2 -n 1 -b|grep Tasks|awk -F : , print $3ProcessRunning=top -d 2 -n 1 -b|gre
17、p Tasks|awk -F : , print $8ProcessSleeping=top -d 2 -n 1 -b|grep Tasks|awk -F : , print $11ProcessStoping=top -d 2 -n 1 -b|grep Tasks|awk -F : , print $16ProcessZombie=top -d 2 -n 1 -b|grep Tasks|awk -F : , print $21UserSpaceCPU=top -d 2 -n 1 -b|grep CPpUu|head -1|awk -F : ,% print $4SystemSpaceCPU=
18、top -d 2 -n 1 -b|grep CPpUu|head -1|awk -F : ,% print $8ChangePriorityCPU=top -d 2 -n 1 -b|grep CPpUu|head -1|awk -F : ,% print $12WaitingCPU=top -d 2 -n 1 -b|grep CPpUu|head -1|awk -F : ,% print $19HardwareIRQCPU=top -d 2 -n 1 -b|grep CPpUu|head -1|awk -F : ,% print $23SoftwareIRQCPU=top -d 2 -n 1
19、-b|grep CPpUu|head -1|awk -F : ,% print $27MemUsed=top -d 2 -n 1 -b|grep Mem|awk -F : , print $11|tr -d a-zA-Z|awk printf(%dMn,$1/1024)MemFreeP=top -d 2 -n 1 -b|grep Mem|awk -F : , print $16|tr -d a-zA-Z|awk printf(%dMn,$1/1024)MemBuffersP= top -d 2 -n 1 -b|grep Mem|awk -F : , print $22|tr -d a-zA-Z
20、|awk printf(%dMn,$1/1024)CacheCachedP=top -d 2 -n 1 -b|grep Swap|awk -F : , print $24|tr -d a-zA-Z|awk printf(%dMn,$1/1024)CacheTotal=top -d 2 -n 1 -b|grep Swap|awk -F : , print $4|tr -d a-zA-Z|awk printf(%dMn,$1/1024)CacheUsed=top -d 2 -n 1 -b|grep Swap|awk -F : , print $14|tr -d a-zA-Z|awk printf(
21、%dMn,$1/1024)CacheFree=top -d 2 -n 1 -b|grep Swap|awk -F : , print $18|tr -d a-zA-Z|awk printf(%dMn,$1/1024)echo -e CPUIdle|$CPUIdlenCPUloadAverage|$CPUloadAveragenProcessNumbers|$ProcessNumbersnProcessRunning|$ProcessRunningnProcessSleeping|$ProcessSleepingnProcessStoping|$ProcessStopingnProcessZom
22、bie|$ProcessZombienUserSpaceCPU|$UserSpaceCPUnSystemSpaceCPU|$SystemSpaceCPUnChangePriorityCPU|$ChangePriorityCPUnWaitingCPU|$WaitingCPUnHardwareIRQCPU|$HardwareIRQCPUnSoftwareIRQCPU|$SoftwareIRQCPUnMemUsed|$MemUsednMemFreeP|$MemFreePnMemBuffersP|$MemBuffersPnCacheCachedP|$CacheCachedPnCacheTotal|$C
23、acheTotalnCacheUsed|$CacheUsednCacheFree|$CacheFreenOprateSystemSec () echo =UserLogin=wecho =FileUsed=df -ahecho =dmesgError=dmesg | grep errorecho =dmesgFail=dmesg | grep Failecho =BootLog=more /var/log/boot.log | grep -V OK | sed 1,6decho =route -n=route -necho =iptables -L=iptables -Lecho =netstat -lntp=netstat -lntpecho =netstat -antp=netstat -antpecho =BootLog=netstat -secho =netstat -s=lastecho =du -sh /etc/=du -sh /etc/echo =du -sh /boot/=du -sh /boot/echo =du -sh /dev/=du -sh /dev/echo =df -h=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1