Linux服务器批量巡检脚本使用说明.docx

上传人:b****5 文档编号:7629351 上传时间:2023-01-25 格式:DOCX 页数:10 大小:61.10KB
下载 相关 举报
Linux服务器批量巡检脚本使用说明.docx_第1页
第1页 / 共10页
Linux服务器批量巡检脚本使用说明.docx_第2页
第2页 / 共10页
Linux服务器批量巡检脚本使用说明.docx_第3页
第3页 / 共10页
Linux服务器批量巡检脚本使用说明.docx_第4页
第4页 / 共10页
Linux服务器批量巡检脚本使用说明.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

Linux服务器批量巡检脚本使用说明.docx

《Linux服务器批量巡检脚本使用说明.docx》由会员分享,可在线阅读,更多相关《Linux服务器批量巡检脚本使用说明.docx(10页珍藏版)》请在冰豆网上搜索。

Linux服务器批量巡检脚本使用说明.docx

Linux服务器批量巡检脚本使用说明

文件说明

该Shell脚本旨在针对大量Linux服务器的巡检提供一种相对自动化的解决方案。

脚本组成有三部分:

shellsh.sh、checksh.sh、file.txt;这三个文件需放在一个文件夹下以root权限执行,缺一不可。

脚本用法

将要巡检的服务器的IP地址和对应的密码全部放入file.txt中保存,每行一个IP对应一个密码即可。

然后用如下命令运行:

#./shellsh.shfile.txt192.168.182.143123456

其中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="/tmp/GatherLogDirectory"

CheckScriptPath="/tmp/CheckScript"

if[$#-ne3];then

echo-e"Parametersiffault!

\n"

echo-e"Pleaseusing:

$0login_infogather_server_ip\n"

echo-e"Forexample:

$0IpAndPassword.txt$grep_ip\n"

exit;

fi

if[!

-x"$GatherPath"];then

mkdir"$GatherPath"

echo-e"Thelog'spathis:

$GatherPath"

fi

cat$login_info|whilereadline

do

server_ip=`echo$line|awk'{print$1}'`

server_password=`echo$line|awk'{print$2}'`

login_server_command="ssh-oStrictHostKeyChecking=noroot@$server_ip"

scp_gather_server_checksh="scpchecksh.shroot@$server_ip:

$CheckScriptPath"

/usr/bin/expect<

settimeout20

spawn$login_server_command

expect{

"*yes/no"{send"yes\r";exp_continue}

"*password:

"{send"$server_password\r"}

}

expect"Permissiondenied,pleasetryagain."{exit}

expect"#"{send"mkdir$CheckScriptPath\r"}

expecteof

exit

EOF

/usr/bin/expect<

settimeout20

spawn$scp_gather_server_checksh

expect{

"*yes/no"{send"yes\r";exp_continue}

"*password:

"{send"$server_password\r"}

}

expect"Permissiondenied,pleasetryagain."{exit}

expect"Connectionrefused"{exit}

expect"100%"

expecteof

exit

EOF

/usr/bin/expect<

settimeout60

spawn$login_server_command

expect{

"*yes/no"{send"yes\r";exp_continue}

"*password:

"{send"$server_password\r"}

}

expect"Permissiondenied,pleasetryagain."{exit}

expect"#"{send"cd$CheckScriptPath;./checksh.sh$gather_server_ip$gather_server_password\r"}

expecteof

exit

EOF

done

checksh.sh

#!

/bin/bash

########################################################################################

#Function:

#Thisscriptchecksthesystem'sinformation,disks'sinformation,performance,etc...ofthe

#server

#

#Author:

#ByJackWang

#

#Company:

#ShaanXiGreatWallInformationCo.,Ltd.

########################################################################################

########################################################################################

#

#GatherServerIpAddressistheserver'sIPaddressthatgatherthecheckinglog

#GatherServerPasswordistheserver'sIPaddressthatgatherthecheckinglog

#

########################################################################################

GatherServerIpAddress=$1

GatherServerPassword=$2

########################################################################################

#GetTheIpCommandisacommandthatyoucangettheIPaddress

########################################################################################

GetTheIpCommand=`ifconfig|grep'\([[:

digit:

]]\{1,3\}\.\)\{3\}[[:

digit:

]]\{1,3\}'--color=auto-o|sed-e'2,5d'`

########################################################################################

#LogNameisacommandthatYourlogs'name

########################################################################################

LogName=`ifconfig|grep'\([[:

digit:

]]\{1,3\}\.\)\{3\}[[:

digit:

]]\{1,3\}'--color=auto-o|sed-e'2,5d'``echo"-"``date+%Y%M%d`

########################################################################################

#

#GatherLogPathisapaththatcollectinglogpath

#LocalServerLogPathislocallogpath

#

########################################################################################

GatherServerLogPath="/tmp/GatherLogDirectory"

LocalServerLogPath="/tmp/LocalServerLogDirectory"

########################################################################################

#LinuxOsInformationisfunctionthatusegetocollectOS'sinformation

########################################################################################

LinuxOsInformation(){

Hostname=`hostname`

UnameA=`uname-a`

OsVersion=`cat/etc/issue|sed'2,4d'`

Uptime=`uptime|awk'{print$3}'|awk-F","'{print$1}'`

ServerIp=`ifconfig|grep"inet"|sed'2,4d'|awk-F":

"'{print$2}'|awk'{print$1}'`

ServerNetMask=`ifconfig|grep"inet"|sed'2,4d'|awk-F":

"'{print$4}'|awk'{print$1}'`

ServerGateWay=`netstat-r|grep"default"|awk'{print$2}'`

SigleMemoryCapacity=`dmidecode|grep-P-A5"Memory\s+Device"|grep"Size"|grep-v"Range"|grep'[0-9]'|awk-F":

"'{print$2}'|sed's/^[\t]*//g'`

MaximumMemoryCapacity=`dmidecode-t16|grep"MaximumCapacity"|awk-F":

"'{print$2}'|sed's/^[\t]*//g'`

NumberOfMemorySlots=`dmidecode-t16|grep"NumberOfDevices"|awk-F":

"'{print$2}'|sed's/^[\t]*//g'`

MemoryTotal=`cat/proc/meminfo|grep"MemTotal"|awk'{printf("MemTotal:

%1.0fGB\n",$2/1024/1024)}'|awk-F":

"'{print$2}'`

PhysicalMemoryNumber=`dmidecode|grep-A16"MemoryDevice"|grep"Size:

"|grep-v"NoModuleInstalled"|grep-v"RangeSize:

"|wc-l`

ProductName=`dmidecode|grep-A10"SystemInformation"|grep"ProductName"|awk-F":

"'{print$2}'|sed's/^[\t]*//g'`

SystemCPUInfomation=`cat/proc/cpuinfo|grep"name"|cut-d:

-f2|awk'{print"*"$1,$2,$3,$4}'|uniq-c|sed's/^[\t]*//g'`

echo-e"Hostname|$Hostname\nUnamea|$UnameA\nOsVersion|$OsVersion\nUptime|$Uptime\nServerIp|$ServerIp\nServerNetMask|$ServerNetMask\nServerGateWay|$ServerGateWay\nSigleMemoryCapacity|$SigleMemoryCapacity\nMaximumMemoryCapacity|$MaximumMemoryCapacity\nNumberOfMemorySlots|$NumberOfMemorySlots\nMemoryTotal|$MemoryTotal\nPhysicalMemoryNumber|$PhysicalMemoryNumber\nProductName|$ProductName\nSystemCPUInformation|$SystemCPUInfomation"

}

PerformanceInfomation(){

CPUIdle=`top-d2-n1-b|grepC[Pp][Uu]|grepid|awk'{print$5}'|awk-F"%"'{print$1}'`

CPUloadAverage=`top-d2-n1-b|grep"loadaverage:

"|awk-F":

"'{print$5}'|sed's/^[\t]*//g'`

ProcessNumbers=`top-d2-n1-b|grep"Tasks"|awk-F"[:

]"'{print$3}'`

ProcessRunning=`top-d2-n1-b|grep"Tasks"|awk-F"[:

]"'{print$8}'`

ProcessSleeping=`top-d2-n1-b|grep"Tasks"|awk-F"[:

]"'{print$11}'`

ProcessStoping=`top-d2-n1-b|grep"Tasks"|awk-F"[:

]"'{print$16}'`

ProcessZombie=`top-d2-n1-b|grep"Tasks"|awk-F"[:

]"'{print$21}'`

UserSpaceCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$4}'`

SystemSpaceCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$8}'`

ChangePriorityCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$12}'`

WaitingCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$19}'`

HardwareIRQCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$23}'`

SoftwareIRQCPU=`top-d2-n1-b|grep'C[Pp][Uu]'|head-1|awk-F"[:

%]"'{print$27}'`

MemUsed=`top-d2-n1-b|grep"Mem"|awk-F"[:

]"'{print$11}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

MemFreeP=`top-d2-n1-b|grep"Mem"|awk-F"[:

]"'{print$16}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

MemBuffersP=`top-d2-n1-b|grep"Mem"|awk-F"[:

]"'{print$22}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

CacheCachedP=`top-d2-n1-b|grep"Swap"|awk-F"[:

]"'{print$24}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

CacheTotal=`top-d2-n1-b|grep"Swap"|awk-F"[:

]"'{print$4}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

CacheUsed=`top-d2-n1-b|grep"Swap"|awk-F"[:

]"'{print$14}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

CacheFree=`top-d2-n1-b|grep"Swap"|awk-F"[:

]"'{print$18}'|tr-d"a-zA-Z"|awk'{printf("%dM\n",$1/1024)}'`

echo-e"CPUIdle|$CPUIdle\nCPUloadAverage|$CPUloadAverage\nProcessNumbers|$ProcessNumbers\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|greperror

echo'======================dmesgFail======================'

dmesg|grepFail

echo'======================BootLog========================'

more/var/log/boot.log|grep-V"OK"|sed'1,6d'

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==================

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1