欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux批处理工具脚本

程序员文章站 2022-05-27 11:37:36
...
一、remote_command.sh

#cat remote_command.sh
#!/bin/bash
configuration_file='/root/tools/hosts.txt'
passwd='qazwsx123456'

ssh_f(){
command=$1

##read hosts.txt
for line in `cat "$configuration_file"|grep ^[^#]`
do
#string=(`echo $line | tr ',' ' '`)

##epect
IP=$line

expect -c "
spawn ssh root@$IP $command;
expect {
*yes/no* { send \"yes\r\"; exp_continue }
*password:* { send \"$passwd\r\" } 
}
interact
"

done
}

scp_f(){
file=$1
dir=$2

        ##read hosts.txt
        for line in `cat "$configuration_file"|grep ^[^#]`
        do
        #string=(`echo $line | tr ',' ' '`)

        ##epect
        #IP=${string[0]}
        #passwd=${string[1]}
IP=$line

        expect -c "
        spawn scp $file root@$IP:$dir;
        expect {
        *yes/no* { send \"yes\r\"; exp_continue }
        *password:* { send \"$passwd\r\" }
        }
        interact
        "

        done
}

while [ -n "$1" ]
do
  case $1 in
## introduction
  --help)  echo "USAGE: remote_commands.sh [-command_type|-command]"
           echo
           echo "  -scp"
           echo "                 show the information of buffer pool"
           echo "  -ssh"
           echo "                 show the information of change buffer pool"
           echo "  --help"
           echo "                 display this help and exit"
           echo "END"
           echo
;;

## scp
  -scp)  scp_f "$2" "$3";;

## ssh
  -ssh)  ssh_f "$2";;

#   *)  echo "Please execute 'remote_commands.sh --help'";;
  esac
  shift
done

二、hosts.txt

#cat hosts.txt
11.15.132.131
11.15.132.126
11.15.132.127
11.15.132.125
11.15.132.128
11.15.132.130
11.15.132.129
11.15.132.122


二、filebeat_deployment.txt

#cat filebeat_deployment.txt
sh remote_commands.sh -ssh 'systemctl status filebeat|grep Active:'
sh remote_commands.sh -ssh 'ls -l /opt/filebeat*'
sh remote_commands.sh -scp /root/ELK/filebeat-7.11.2-x86_64.rpm /opt
sh remote_commands.sh -ssh 'rpm -ivh /opt/filebeat-7.11.2-x86_64.rpm'

sh remote_commands.sh -scp /root/ELK/filebeat.yml /etc/filebeat

sh remote_commands.sh -ssh 'systemctl restart filebeat'
sh remote_commands.sh -ssh 'systemctl enable filebeat'
sh remote_commands.sh -ssh 'systemctl status filebeat|grep Active:'