使用Linux expect批量巡检Linux Aix Solaris磁盘使用率脚本

脚本分三个文件,前两个是包含ip、连接用户、ssh端口、平台以及、密码文件,后期自动发现只需要添加主机相关信息以及密码即可,脚本部分无需更改。
脚本测试平台为Redhat 7.6,通过一台RHEL机器巡检其他机器。

1. ssh连接信息文件

/* 编辑ip列表,按照ip platform user sshport格式编辑即可 */
#vi iplist.txt
192.168.100.101 Linux root 22
192.168.100.102 Linux oracle 22

2. 密码文件

/ 编辑密码,与上述主机信息列表需要对应 /

# vi pass.txt
111111
111111

3. 脚本正文

#!/bin/sh
# author by czh
# created 20210511
# This script used to execute on remote os.
# global variables
v_cmd=$1
v_curtime=$(date +%Y%m%d%H%M%S)
v_curdate=$(date +%F'  '%T)
v_num=$(cat iplist.txt|wc -l)
# must use argument to use this script
if [ $# = 0 ]
then
    echo "Error! usage:sh $0 "{command}""
    exit
elif [ $# -gt 1 ]
then
    echo "Error! This script only accept one argument!"
else
    echo "This script will continue!"
fi
# Linux Aix df function
func_linux ()
{
echo -e "\nCheck Time is ${v_curdate}"
echo -e "Host is ${v_ip} ${v_os}\n"
cat df_${v_curtime}.log|grep "^/dev"|awk 'sub("%","",$5)'|awk '{if($5 > 80) print "FS warnings!!!:  "$5"%  "$6}'
}
# solaris df function
func_sunos ()
{
echo "\nCheck Time is ${v_curdate}"
echo "Host is ${v_ip} ${v_os}\n"
cat df_${v_curtime}.log|grep pool/|nawk 'sub("%","",$5)'|nawk '{if($5 > 80) print "FS warnings!!!:  "$5"%  "$6}'
}
# script begin
for v_i in $(seq 1 ${v_num})
do
# variables define area
    v_ip=$(sed -n "${v_i}p" iplist.txt|awk '{print $1}')
    v_os=$(sed -n "${v_i}p" iplist.txt|awk '{print $2}')
    v_user=$(sed -n "${v_i}p" iplist.txt|awk '{print $3}')
    v_pass=$(sed -n "${v_i}p" pass.txt|awk '{print $1}')
    v_port=$(sed -n "${v_i}p" iplist.txt|awk '{print $4}')  
# expect get filesystem usage
# expect module begin
/usr/bin/expect >df_${v_curtime}.log <

4. 使用示例

/* 脚本由于后期处理了df -P输出,所以只能接收df -P,如果想自定义输入命令,则删除case循环部分即可。*/
# sh expect.sh "df -P"
This script will continue!
Check Time is 2021-05-12  12:28:04
Host is 192.168.100.101 Linux
FS warnings!!!:  83%  /
FS warnings!!!:  100%  /mnt
Check Time is 2021-05-12  12:28:04
Host is 192.168.100.101 Linux