Linux服务器批量巡检脚本使用说明
更新时间:2024-04-20 07:13:01 阅读量: 综合文库 文档下载
文件说明
该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,123456为该服务器的密码。
运行结果
运行完后会在192.168.182.143服务器的/tmp目录下升成一个目录,即:GatherLogDirectory这个目录下即存放的是被巡检的服务器的巡检日志,这些日志以被巡检的服务器的IP命名,形如:192.168.182.146.log。在被巡检的服务器上会升成两个目录即:CheckScript、 LocalServerLogDirectory;其中CheckScript中是
checksh.sh脚本,LocalServerLogDirectory中存放的是checksh.sh在该服务器上运行后升成的日志。
测试结果
我只在虚拟机上的三台Linux系统上测试过,分别是Ubuntu、RedHat、Kali。运行正常,平均巡检一个服务器花费3分钟。
脚本代码
shellsh.sh
#!/bin/bash login_info=$1 gather_server_ip=$2 gather_server_password=$3
grep_ip=`ifconfig | grep '\\([[:digit:]]\\{1,3\\}\\.\\)\\{3\\}[[:digit:]]\\{1,3\\}' --color=auto -o | sed -e '2,5d'`
GatherPath=\CheckScriptPath=\
if [ $# -ne 3 ]; then
echo -e \
echo -e \ echo -e \ exit; fi
if [ ! -x \
mkdir \
echo -e \fi
cat $login_info | while read line do
server_ip=`echo $line|awk '{print $1}'` server_password=`echo $line|awk '{print $2}'`
login_server_command=\ scp_gather_server_checksh=\
/usr/bin/expect<
spawn $login_server_command expect {
\ \ }
expect \
expect \ expect eof exit EOF
/usr/bin/expect<
spawn $scp_gather_server_checksh expect {
\ \ }
expect \expect \
expect \ expect eof exit EOF
/usr/bin/expect<
spawn $login_server_command expect {
\ \ }
expect \
expect \{ send \$CheckScriptPath;./checksh.sh $gather_server_ip $gather_server_password\\r\ expect eof exit EOF done
checksh.sh
#!/bin/bash
#######################################################################
################# #Function:
#This script checks the system's information,disks's information,performance,etc...of the #server # #Author: #By Jack Wang #
#Company:
#ShaanXi Great Wall Information Co.,Ltd.
########################################################################################
######################################################################################## #
#GatherServerIpAddress is the server's IP address that gather the checking log #GatherServerPassword is the server's IP address that gather the checking log #
######################################################################################## GatherServerIpAddress=$1
GatherServerPassword=$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 is a command that Your logs'name
########################################################################################
LogName=`ifconfig|grep '\\([[:digit:]]\\{1,3\\}\\.\\)\\{3\\}[[:digit:]]\\{1,3\\}' --color=auto -o|sed -e '2,5d'``echo \
######################################################################################## #
#GatherLogPath is a path that collecting log path
#LocalServerLogPath is local log path #
########################################################################################
GatherServerLogPath=\LocalServerLogPath=\
########################################################################################
#LinuxOsInformation is function that usege to collect OS's information
######################################################################################## LinuxOsInformation(){ Hostname=`hostname` UnameA=`uname -a`
OsVersion=`cat /etc/issue | sed '2,4d'`
Uptime=`uptime|awk '{print $3}'|awk -F \
ServerIp=`ifconfig|grep \ServerNetMask=`ifconfig|grep \ServerGateWay=`netstat -r|grep \
SigleMemoryCapacity=`dmidecode|grep -P -A5 \\-v
\
MaximumMemoryCapacity=`dmidecode -t 16|grep \$2}'|sed 's/^[ \\t]*//g'`
NumberOfMemorySlots=`dmidecode -t 16|grep \Of Devices\-F \'{print $2}'|sed 's/^[ \\t]*//g'` MemoryTotal=`cat
/proc/meminfo|grep
\
'{printf(\
PhysicalMemoryNumber=`dmidecode|grep -A16 \Device\\-v \
ProductName=`dmidecode|grep -A10 \\
SystemCPUInfomation=`cat /proc/cpuinfo|grep \ -d: -f2|awk '{print \ echo
-e
\ServerIp|$ServerIp\\nServerNetMask|$ServerNetMask\\nServerGateWay|$ServerGateWay\\nSigleMemoryCapacity|$SigleMemoryCapacity\\nMaximumMemoryCapacity|$MaximumMemoryCapacity\\nNumberOfMemorySlots|$NumberOfMemorySlots\\nMemoryTotal|$MemoryTotal\\nPhysicalMemoryNumber|$PhysicalMemoryNumber\\nProductName|$ProductName\\nSystemCPUInformation|$SystemCPUInfomation\
}
PerformanceInfomation (){
CPUIdle=`top -d 2 -n 1 -b|grep C[Pp][Uu]|grep id|awk '{print $5}'|awk -F \CPUloadAverage=`top -d 2 -n 1 -b|grep \average:\-F \'{print $5}'|sed 's/^[ \\t]*//g'`
ProcessNumbers=`top -d 2 -n 1 -b|grep \ProcessRunning=`top -d 2 -n 1 -b|grep \ProcessSleeping=`top -d 2 -n 1 -b|grep \ProcessStoping=`top -d 2 -n 1 -b|grep \ProcessZombie=`top -d 2 -n 1 -b|grep \
UserSpaceCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \SystemSpaceCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \ChangePriorityCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \WaitingCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \HardwareIRQCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \SoftwareIRQCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F \MemUsed=`top -d 2 -n 1 -b|grep \-F \,]\'{print $11}'|tr -d \'{printf(\
MemFreeP=`top -d 2 -n 1 -b|grep \-F \,]\'{print $16}'|tr -d \'{printf(\
MemBuffersP=` top -d 2 -n 1 -b|grep \
'{printf(\
CacheCachedP=`top -d 2 -n 1 -b|grep \'{printf(\
CacheTotal=`top -d 2 -n 1 -b|grep \-F \,]\'{print $4}'|tr -d \'{printf(\
CacheUsed=`top -d 2 -n 1 -b|grep \-F \,]\'{print $14}'|tr -d \'{printf(\
CacheFree=`top -d 2 -n 1 -b|grep \-F \,]\'{print $18}'|tr -d \'{printf(\ echo
-e
%umbers\\nProcessRunning|$ProcessRunning\\nProcessSleeping|$ProcessSleeping\\nProcessStoping|$ProcessStoping\\nProcessZombie|$ProcessZombie\\nUserSpaceCPU|$UserSpaceCPU\\nSystemSpaceCPU|$SystemSpaceCPU\\nChangePriorityCPU|$ChangePriorityCPU\\nWaitingCPU|$WaitingCPU\\nHardwareIRQCPU|$HardwareIRQCPU\\nSoftwareIRQCPU|$SoftwareIRQCPU\\nMemUsed|$MemUsed\\nMemFreeP|$MemFreeP\\nMemBuffersP|$MemBuffersP\\nCacheCachedP|$CacheCachedP\\nCacheTotal|$CacheTotal\\nCacheUsed|$CacheUsed\\nCacheFree|$CacheFree\\n\}
OprateSystemSec () {
echo '======================UserLogin======================' w
echo '======================FileUsed=======================' df -ah
echo '======================dmesgError=====================' dmesg | grep error
echo '======================dmesgFail======================' dmesg | grep Fail
echo '======================BootLog========================' more /var/log/boot.log | grep -V \
echo '======================route -n=======================' route -n
echo '======================iptables -L====================' iptables -L
echo '======================netstat -lntp==================' netstat -lntp
echo '======================netstat -antp=================='
netstat -antp
echo '======================BootLog========================' netstat -s
echo '======================netstat -s=====================' last
echo '======================du -sh /etc/===================' du -sh /etc/
echo '======================du -sh /boot/==================' du -sh /boot/
echo '======================du -sh /dev/===================' du -sh /dev/
echo '======================df -h==========================' df -h
echo '======================mount | column -t==============' mount | column -t }
TopAndVmstat(){ top -d 2 -n 1 -b vmstat 1 10
}
CheckGatherLog(){
if [ -f \ rm -rf $LocalServerLogPath/$GetTheIpCommand.log fi
if [ ! -x \ mkdir \fi
if [ ! -f \ touch $LocalServerLogPath/$GetTheIpCommand.log
LinuxOsInformation>>$LocalServerLogPath/$GetTheIpCommand.log PerformanceInfomation>>$LocalServerLogPath/$GetTheIpCommand.log OprateSystemSec>>$LocalServerLogPath/$GetTheIpCommand.log TopAndVmstat>>$LocalServerLogPath/$GetTheIpCommand.log fi }
CheckGatherLog
SCP_LOG_TO_GATHER_SERVER=\root@$GatherServerIpAddress:$GatherServerLogPath\
/usr/bin/expect<
spawn $SCP_LOG_TO_GATHER_SERVER expect {
\ {
send \
\ }
\ {
send \ } }
expect \ { send \ expect \ expect eof
EOF






正在阅读:
Linux服务器批量巡检脚本使用说明04-20
2015会计继续教育网培(东奥)06-21
2016浙江大学社会工作考研真题03-23
WIPO中期战略规划03-20
叔丁基过氧化氢项目可行性研究报告01-26
在校大学生兼职情况的调查与分析05-27
山中取宝作文400字07-07
牛顿第二定律教学设计05-13
5000亩商品蔬菜基地项目可行性研究报告06-22
- 天大砼方案 - 图文
- 农业科技网络书屋能力提升_玉米错题选
- DNS习题
- 浅议检察官对罪犯谈话的技巧与效果
- 高考语文文言文翻译专题训练
- AB类学科竞赛目录(2015)
- 建筑面积计算新规定(2015最新)
- Revit2012初级工程师题集一
- 十三五项目米线可行性报告
- 2013体育学院党组织建设工作总结
- 2014Revit工程师题库
- 高中数学如何实施研究性学习
- 茶艺表演 中英互译
- 小学音乐湘文艺版 四年级下册 第十一课《(歌表演)脚印》优质课公
- 山西省农村合作经济承包合同管理条例
- 2015年镇江市中考化学一模试题参考答案及评分标准(定稿)
- 统计 题集
- 批评意见清单
- 8潞安集团蒲县黑龙关煤矿矿业公司2
- 鄂教版四年级语文上册复习精要(光谷四小)
- 巡检
- 使用说明
- 批量
- 脚本
- 服务器
- Linux
- 大课间活动总结
- 体育场馆管理办法
- 实验室事故案例
- 小学二年级钟表时间练习题 - 图文
- 防火墙透明模式配置(二层模式)
- 免费文档 VFP(单选题 题库1)
- 寄生虫学检验题目
- GIS在农村土地确权中的应用
- 数据挖掘技术客户端毕业设计
- 人工挖孔桩安全专项方案
- 6101综采工作面安装作业规程
- 江西省南昌市第十九中学2015届高三上学期第二次月考化学试题
- (小溪流的歌)的教学设计 doc
- 2016届人教版化学河北中考考点学案 滚动小专题(一) 气体的制备
- 聚苯胺超级电容器材料
- 1000亩药材种植基地扩建项目可行性研究报告
- 关爱五生工作方案
- 桂林山水评课稿
- 2018年第1学期《管理学原理》平时作业题2018.1
- 德州市庆云县 七年级地理下学期第一次月考试卷3月含解析新人教版