暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

expect 简单使用

原创 戚少彪 2021-03-11
495

一、接触expect
expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信。

expect自动交互流程:

spawn启动指定进程—expect获取指定关键字—send向指定程序发送指定字符—执行完成退出.

二、安装
yum install -y expect

三、使用expect批量巡检系统messages
对于多数监控系统来说,都会提供监控系统日志的功能,这里说下怎么使用expect怎么批量实现
A.提取脚本
#!/usr/bin/expect

If {$argc < 3} {
Send_user “usage: $argv0 <remore_user> <remote_host> <remote_pwd>”
Exit
}

Set timeout -1
Set remote_user [lindex $argv 0]
Set remote_host [lindex $argv 1]
Set remote_pwd [lindex $argv 2]

Spawn ssh ${remote_user}@ {remote_host} Expect “*assword:” {send “{remote_pwd}\r”}
Expect “Last login:”
Send “su \r”
Send “/bin/cp –rf /var/log/messages /tmp/messages_{remote_host} \r” Send “chown –R dwzq:wheel /tmp/messages_{remote_host}\r”
Send “exit\r”
Send “exit\r”
Expect eof

Spawn scp –r ${remote_user}@ {remote_host}:/tmp/messages* ./log Expect “*assword:” {send “{remote_pwd}\r”}
Send “exit\r”
Expect eof

实现方式:通过先拷贝到远端的本地,然后在拉去到集中监控机器进行分析。

B.分析脚本(脚本会每天覆盖)
#!/bin/bash
For data_name in ls –l /root/os_log/log/|grep ^[^d] | awk ‘{print $9}’
Do
error_num=egrep -i 'error|warn|fail' /root/os_log/log/$data_name|wc -l
If [error_num –gt 1 ];then Echo “data_name mess_error_num is warning max_value is error_num” Else echo “data_name is ok”
Fi
Done
说明:这里只要集中监控机器可以连接互联网,然后配合邮件来实现每天自动巡检

C.脚本调用方式、
sh check_messages.sh user ip_address user_pwd

四、其他有特殊监控需求的也可以参考上面脚本。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论