实现远程服务器操作
#!/usr/bin/expect if [ $# -ne 2 ]; then echo "sh logSearch.sh serviceName "grep --color 'ERROR' service.log"" exit 1 fi declare -A serviceMap #依次添加需要的服务与对应的实例关系 serviceMap["serviceName"]="172.16.xx.xx,172.16.xx.xx" serviceName=$1 filter=$2 hostnameStr=${serviceMap[$serviceName]} hostnameArray=(${hostnameStr//,/ }) echo $hostnameStr for hostname in ${hostnameArray[@]} do echo $hostname $serviceName $filter expect login.exp $hostname $serviceName "${filter}" done
#!/usr/bin/expect set hostname [lindex $argv 0] set serviceName [lindex $argv 1] set command [lindex $argv 2]
# 非root用户登录的话,这里改为其他用户 spawn ssh root@$hostname expect { "yes/no" {send "yes\r";} #{send "yes\r"; exp_continue;} "password:" {exp_send "password\r";} #这里是你要输入的密码 } expect "#" send "cd /home/work/${serviceName}/orp/logs \r" #这里需要调整为对应的服务路径 #expect "#" send "${command}\r" #expect "#" send "exit\r" expect eof
CASE1:查询日志
sh logSearch.sh serviceName "grep --color 'ERROR' service.log"
CASE2:删除日志
sh logSearch.sh serviceName "rm service.log"
CASE3:查询磁盘使用情况
sh logSearch.sh serviceName "df -h"