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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/507432.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

实验四:进程间通信编程实验

一【实验目的】 1.理解进程间通信原理&#xff1b; 2.掌握进程中信号量、共享内存、消息队列相关的函数的使用&#xff1b; 3.支撑网络空间安全专业的专业核心能力、综合创新能力。 二【实验要求】 以下每个实验均要求&#xff1a; 1.“实验源代码”处&#xff1a;粘贴所编写…

细粒度图像分类_1.1、 图片分类

记录图像分类论文资源列表。图片分类任务可以分为&#xff1a;粗粒度图片分类、细粒度图片分类。2012 AlexNet &#xff1a;ImageNet Classification with Deep Convolutional Neural Networks。2014 GoogLeNet&#xff1a;Going Deeper with Convolutions。Rethinking the Inc…

diy服务器个人主机_Re:从零开始的服务器-微型服务器DIY手记

国际象棋测试4-Chess Benchmark.png (25.64 KB, 下载次数: 63)2017-3-22 19:43 上传RAR benchmarkWinRAR自带的基准测试7-RARbench.png (15.8 KB, 下载次数: 54)2017-3-22 19:45 上传7-zip benchmark7zip自带的基准测试8-7zipbench.png (31.2 KB, 下载次数: 70)2017-3-22 19:45…

实验五:线程编程实验

一【实验目的】 1.理解线程环境、线程的生命周期&#xff0c; 2.理解线程同步&#xff0c;掌握内核函数的基本用法。 3.支撑网络空间安全专业的专业核心能力、综合创新能力。 二【实验要求】 以下每个实验均要求&#xff1a; 1.“实验源代码”处&#xff1a;粘贴所编写的程序…

where里能用max吗_超市里四五元钱的速冻饺子能买吗?

展开全部我家里都特别喜欢吃带馅的食物&#xff0c;尤其是我儿子只要是饺子和馄饨&#xff0c;吃起来准没够。62616964757a686964616fe59b9ee7ad9431333433653938包饺子虽然不是什么力气活&#xff0c;但是有时候做起来也感觉太过繁琐&#xff0c;尤其是孩子要吃的时候&#xf…

python的迭代_迭代法 python

详解迭代器的使用 | 手把手教你入门Python之八十上一篇&#xff1a;自定义异常 | 手把手教你入门Python之七十九下一篇&#xff1a;生成器 | 手把手教你入门Python之八十一本文来自于千锋教育在阿里云开发者社区学习中心上线课程《Python入门2020最新大课》&#xff0c;主讲人姜…

实验六:套接字编程实验

一【实验目的】 1.掌握面向连接的套接字编程框架&#xff1b; 2.掌握面向无连接的套接字编程框架; 3.掌握I/O复用、套接字选项设置&#xff1b; 4.掌握非阻塞式I/O。 二【实验要求】 以下每个实验均要求&#xff1a; 1.“实验源代码”处&#xff1a;粘贴所编写的程序源码&am…

idea代码回滚_IDEA远程仓库版本回滚

使用 git 进行项目的版本控制时&#xff0c;肯定会遇到回滚版本的情况&#xff0c;回滚有两种&#xff0c;一种是本地仓库回滚&#xff0c;另外一种是远程仓库回滚。以下详细讲解两种回滚方式&#xff0c;本文主要讲解远程回滚&#xff0c;以及常见使用误区。本地仓库回滚本地回…

AppScan api登录接口 postman_【Postman】12 Postman monitor功能使用

通过前面的文章&#xff0c;我们已经能完成接口的Collection&#xff0c;添加断言&#xff0c;Data文件数据驱动&#xff0c;设置变量&#xff08;Data、Globals和Environment&#xff09;等等方法&#xff0c;对我们的接口进行个各种的测试&#xff0c;保证其正确性。但是还可…

Linux编程基础 5.1:管道

1 简介 Linux进程通信机制&#xff1a; 管道信号量消息队列共享内存socket通信 2 管道 管道其实质是由内核管理的一个缓冲区 形象地认为管道的两端连接着两个进程&#xff1a; 一个进程进行信息输出&#xff0c;将数据写入管道&#xff1b;另一个进程进行信息输入&#xff…

python pd Series 添加行_Python数据分析与挖掘的常用工具

Python语言&#xff1a;简要概括一下Python语言在数据分析、挖掘场景中常用特性&#xff1a;列表(可以被修改)&#xff0c;元组(不可以被修改)字典(结构)集合(同数学概念上的集合)函数式编程(主要由lambda()、map()、reduce()、filter()构成)Python数据分析常用库&#xff1a;P…

华为荣耀20和x10比较_荣耀X10和华为畅享20plus,到底哪个更值得入手?

今天&#xff0c;我接到了联通客服的电话&#xff0c;说我是老用户&#xff0c;直接给我升级成了5G套餐&#xff0c;资费没有太多的变化&#xff0c;比起以前每月月租只多了一块钱&#xff0c;也就是从原来的48元一个月变成了49元一个月&#xff0c;就加了一元钱就变成了5G套餐…

Linux编程基础 5.2:消息队列

3 消息队列 消息队列的本质是一个存放消息的链表&#xff0c;该链表由内核来维护。一个消息队列由一个标识符&#xff08;即队列key&#xff09;来标识。消息队列的通信机制传递的数据具有某种结构&#xff0c;而不是简单的字节流&#xff1b;向消息队列中写数据&#xff0c;实…

python tab和空格混用_Python编程常见十大错误,看完你自己都笑了!

关注并置顶【柠檬班】的小哥哥小姐姐胸有成“猪”使用python会出现各种各样的错误&#xff0c;以下是Python常见的错误以及解决方法。01 ValueErrorValueError: ‘Conv2d_1a_33’ is not a valid scope name其实这就是命名错误的问题&#xff0c;如果仔细看“”是我在中文下打的…

Linux编程基础 5.3:信号量

4 信号量 信号量是专门用来解决进程同步与互斥问题的一种通信机制&#xff0c;它与信号无关&#xff1b;不同于管道、FIFO以及消息队列&#xff0c;一般不用来传输数据&#xff1b;信号量包括&#xff1a;表示资源数量的非负整型变量、修改信号量的原子操作P和V、该信号量下等…

谷歌浏览器外贸版_针对谷歌SEO,你有哪些值得推荐的工具、插件、网站、app,或者技巧分享?...

接触并了解谷歌SEO也有3年了&#xff0c;这3年来&#xff0c;一直钻研这块技术和工具&#xff0c;今天刚好看到这个问题&#xff0c;就分享下。目前谷歌SEO主要分3大块&#xff0c;站内SEO(On page seo&#xff0c;做好站内优化)&#xff0c;站外SEO(off page seo 主要是外链)&…

hadoopsdk使用_hadoop部署使用问题及解决

在cygwin环境中填写路径信息时务必注意将“\”替换为“/”。hadoop文件夹名称不能包含“-”&#xff0c;比如“hadoop-2.4.0”会出错。在cygwin环境中尽管“C:\abc”和“/cygdrive/c/abc”都可以被正确识别&#xff0c;但某些软件会将前者识别为相对路径&#xff0c;从而出现错…

Linux编程基础 5.4:共享内存

5 共享内存 共享内存允许两个或多个进程访问给定的同一块存储区域。它是效率最高的一种进程通信方式&#xff0c;节省了不同进程间多次读写的时间&#xff1b;在写进程的操作尚未完成时&#xff0c;不应有进程从共享内存中读取数据。共享内存自身不限制对共享内存的读写次序&a…

升级浏览器_微软IE11浏览器 最后的升级机会

微软IE10浏览器将很快退出支持&#xff0c;许多Windows用户将没有任何安全或非安全更新&#xff0c;免费或付费辅助支持选项或在线技术内容等。幸运的是&#xff0c;微软正在为Windows用户提供升级到IE11的最后机会&#xff0c;目前IE11仅次于Firefox浏览器&#xff0c;这是互联…

jq如何获取选中option的值_【分享】如何获取变量token的值

一.什么是token客户端使用用户名跟密码请求登录服务端收到请求&#xff0c;去验证用户名与密码验证成功后&#xff0c;服务端会签发一个 Token&#xff0c;再把这个 Token 发送给客户端客户端收到 Token 以后可以把它存储起来&#xff0c;比如放在 Cookie 里或者 LocalStorage …