日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

expect巡检服务器_Shell-批量巡检服务器脚本

發布時間:2024/8/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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-批量巡检服务器脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。