文件说明
该Shell脚本旨在针对大量Linux服务器的巡检提供一种相对自动化的解决方案。脚本组成有三部分:shellsh.sh、checksh.sh、file.txt;这三个文件需放在一个文件夹下以root权限执行,缺一不可。
脚本用法
将要巡检的服务器的IP地址和对应的密码全部放入file.txt中保存,每行一个IP对应一个密码即可。然后用如下命令运行:#./ shellsh.sh file.txt 123456
其中file.txt可以更换文件名,,123456为该服务器的密码。
运行结果
运行完后会在,即:GatherLogDirectory这个目录下即存放的是被巡检的服务器的巡检日志,这些日志以被巡检的服务器的IP命名,形如: 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