expect巡检服务器_Shell-批量巡检服务器脚本
文件说明
该Shell脚本旨在针对大量Linux服务器的巡检提供一种相对自动化的解决方案。脚本组成有三部分:shellsh.sh、checksh.sh、file.txt;这三个文件需放在一个文件夹下以root权限执行,缺一不可。
脚本用法
将要巡检的服务器的IP地址和对应的密码全部放入file.txt中保存,每行一个IP对应一个密码即可。然后用如下命令运行:
#./ shellsh.sh file.txt192.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="/tmp/GatherLogDirectory"
CheckScriptPath="/tmp/CheckScript"
if [ $# -ne 3 ]; then
echo -e "Parameters if fault!\n"
echo -e "Please using:$0 login_info gather_server_ip\n"
echo -e "For example: $0 IpAndPassword.txt $grep_ip\n"
exit;
fi
if [ ! -x "$GatherPath" ];then
mkdir "$GatherPath"
echo -e "The log"s path is: $GatherPath"
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="ssh -o StrictHostKeyChecking=no root@$server_ip"
scp_gather_server_checksh="scp checksh.sh root@$server_ip:$CheckScriptPath"
/usr/bin/expect<
set timeout 20
spawn $login_server_command
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$server_password\r" }
}
expect "Permission denied, please try again." {exit}
expect "#" { send "mkdir $CheckScriptPath\r"}
expect eof
exit
EOF
/usr/bin/expect<
set timeout 20
spawn $scp_gather_server_checksh
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$server_password\r" }
}
expect "Permission denied, please try again." {exit}
expect "Connection refused" {exit}
expect "100%"
expect eof
exit
EOF
/usr/bin/expect<
set timeout 60
spawn $login_server_command
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$server_password\r" }
}
expect "Permission denied, please try again." {exit}
expect "#" { send "cd $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 "-"``date +%Y%M%d`
########################################################################################
#
#GatherLogPath is a path that collecting log path
#LocalServerLogPath is local log path
#
########################################################################################
GatherServerLogPath="/tmp/GatherLogDirectory"
LocalServerLogPath="/tmp/LocalServerLogDirectory"
########################################################################################
#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 "," "{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 -t 16|grep "Maximum Capacity"|awk -F ":" "{print $2}"|sed "s/^[ \t]*//g"`
NumberOfMemorySlots=`dmidecode -t 16|grep "Number Of Devices"|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 "Memory Device"|grep "Size:"|grep -v "No Module Installed"|grep -v "Range Size:"|wc -l`
ProductName=`dmidecode|grep -A10 "System Information"|grep "Product Name"|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 -d 2 -n 1 -b|grep C[Pp][Uu]|grep id|awk "{print $5}"|awk -F "%" "{print $1}"`
CPUloadAverage=`top -d 2 -n 1 -b|grep "load average:"|awk -F ":" "{print $5}"|sed "s/^[ \t]*//g"`
ProcessNumbers=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" "{print $3}"`
ProcessRunning=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" "{print $8}"`
ProcessSleeping=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" "{print $11}"`
ProcessStoping=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" "{print $16}"`
ProcessZombie=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" "{print $21}"`
UserSpaceCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $4}"`
SystemSpaceCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $8}"`
ChangePriorityCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $12}"`
WaitingCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $19}"`
HardwareIRQCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $23}"`
SoftwareIRQCPU=`top -d 2 -n 1 -b|grep "C[Pp][Uu]"|head -1|awk -F "[: ,%]" "{print $27}"`
MemUsed=`top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" "{print $11}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
MemFreeP=`top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" "{print $16}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
MemBuffersP=` top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" "{print $22}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
CacheCachedP=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" "{print $24}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
CacheTotal=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" "{print $4}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
CacheUsed=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" "{print $14}"|tr -d "a-zA-Z"|awk "{printf("%dM\n",$1/1024)}"`
CacheFree=`top -d 2 -n 1 -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 | grep error
echo "======================dmesgFail======================"
dmesg | grep Fail
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=========================="
df -h
echo "======================mount | column -t=============="
mount | column -t
}
TopAndVmstat(){
top -d 2 -n 1 -b
vmstat 1 10
}
CheckGatherLog(){
if [ -f "$LocalServerLogPath/$GetTheIpCommand.log" ];then
rm -rf $LocalServerLogPath/$GetTheIpCommand.log
fi
if [ ! -x "$LocalServerLogPath" ];then
mkdir "$LocalServerLogPath"
fi
if [ ! -f "$LocalServerLogPath/$GetTheIpCommand.log" ];then
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="scp $LocalServerLogPath/$GetTheIpCommand.log root@$GatherServerIpAddress:$GatherServerLogPath"
/usr/bin/expect<
set timeout 50
spawn $SCP_LOG_TO_GATHER_SERVER
expect {
"*yes/no)?"
{
send "yes\n"
"*password:*" {send "GatherServerPassword\n"}
}
"*password:"
{
send "$GatherServerPassword\n"
}
}
expect "*password:" { send "$GatherServerPassword\n" }
expect "100%"
expect eof
EOF
file.txt内容形式192.168.182.143 123456
192.168.182.129 123456
192.168.182.146 123456
注:192.168.182.143是被巡检的服务器ip,123456是被巡检的服务器密码。
声明:此脚本将巡检结果用awk和sed以及grep进行了过滤,以便格式化显示和excel进行处理。由于水平有限,如果有大神认为还有更好的解决方案或者这个脚本还有改进的地方,请指出。我们大家共同学习,共同进步。
个人QQ:1299039410
expect巡检服务器_Shell-批量巡检服务器脚本相关推荐
- Linux服务器Shell批量巡检
关键词:Linux shell 批量巡检 1.批量巡检流程 通过Linux shell脚本实现批量服务器巡检方案,通常脚本由三部分组成,包括: 巡检脚本(实现对性能信息或指标的采集与回传).巡检脚本下 ...
- 服务器ibmc无法加载js文件,华为服务器IBMC批量巡检代码
selenium需要下载Chrome驱动webdriver,具体下载的版本根据自己的谷歌浏览器版本进行下载,然后 将下载好的驱动webdriver放到自己python解释器同级目录中即可,下载地址ht ...
- 通过DJANGO实现从WEB端对服务器的批量或单机巡检,打印到EXCEL表格并下载
后台的实现和前面写的LINUX服务器批量巡检的PYTHON脚本,是一样的,只不过增加了单机检查和EXCEL下载的功能,并且可以从WEB端巡检,不用登陆服务器就能实现 urls.py url(r'^in ...
- linux服务器运维巡检脚本,linux服务器巡检脚本
巡检的基本步骤: 1.在每台服务器上部署巡检的脚本,查询相应的日志. 2.将每台服务器上的日志发送到ftp服务器的指定目录下. 3.遍历ftp服务器指定目录,并且将各个的文件信息整理到一个文件中. 4 ...
- Expect的安装与应用,及实现自动检测另外一台服务器运行状态并重启,和使用expect脚本远程批量管理服务器与日志分析
学习Expect Expect是什么? Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预. Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为 ...
- python批量巡检服务器_python批量服务器巡检
{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...
- Shell脚本之H3C网络设备批量巡检
一.需求说明 网络工程师每天都需要巡检网络设备,或者临时接到一些任务,需要统计一些信息,比如每台设备的运行时间.如果是几台,十几台设备,熟练的工程师可能几分钟就完成了统计,但如果是成百上千台呢?此 ...
- 小白之-----------shell脚本批量管理服务器
简单思路 (1)创建一个文件file用于存放username,hostIp ,password (2)编写expect,用于登录服务器 (3)编写一个shell脚本,在脚本中循环调用expect程序进 ...
- hp服务器ilo批量配置脚本
hp 服务器ilo批量配置脚本 hp的ilo是一个非常方便的带外管理工具,在平时使用的时候,经常有大量的hp服务器需要配置ilo,下面是我下的一个脚本,可以批量的完成hp服务器ilo配置. 脚本功能: ...
最新文章
- 为何把日志打印到控制台很慢?
- 在C#中操作XM II
- Shiro学习(15)单点登录
- android xutils数据库操作,XUtils3.0之本地数据库操作
- python 使用 with open() as 读写文件-给Python学习者的文件读写指南(含基础与进阶)...
- 浅谈UWB室内定位(二)
- java工程师占比_25岁零基础转行学Java,他如今月入16k
- oracle 1亿条数据,如何在十分钟内插入1亿条记录到Oracle数据库?
- linux如何秀爱swap大小,老毛桃PE启动U盘集合CDlinux,BT5可保存设置,wifislax,sliatz中文定制版的方法[存档......
- NB-IOT(BC95模组)对接华为中国电信物联网平台上行数据与下行数据总流程
- 【企业网络】我在51cto技术门诊的提问以及专家的解答汇总
- apifox设置全局header
- python三维图形注释_python – Matplotlib:注释3D散点图
- TI DSP各种库文件
- 6.计算机系统能力创新实践,计算机考核要重实践能力、培养创新精神
- vue开发的音乐小播放器
- 康托展开及其逆运算 详解
- CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
- html5 直线的两头弯曲,html5教程 (二)(canvas绘制简单的直线)
- QT之QLineEdit——实现鼠标点击事件
热门文章
- 听说你盗图都盗绿了?
- 小甲鱼 OllyDbg 教程系列 (十) : Windows 逆向常用 api 以及 XOFTSPY 逆向
- 22条API设计的实践
- 有效单元测试之可读性
- python注册系统_Python制作简易注册登录系统
- C++学习之路 | PTA乙级—— 1017 A除以B (20分)(精简)
- opencv感兴趣通道COI的使用
- JAVA入门级教学之(类之间究竟有哪几种关系呢)
- m.2接口和nvme区别_m.2 nvme和m.2有区别吗?
- if test 多条件_秒懂Python编程中的if __name__ == #39;main#39; 作用和原理