ElasticSearch操作之重置密码脚本

ElasticSearch操作之重置密码脚本

在这里插入图片描述

#!/bin/bash
# 使用样例 ./ES密码重置.sh 旧密码 新密码

# 输入旧密码
es_old_password=$1

# 设置新的密码变量
es_password=$2

# 正确响应
es_reponse='{"acknowledged":true}'

# 检查Elasticsearch是否在运行
if pgrep -f elasticsearch > /dev/null; then
    echo "Elasticsearch is running, proceeding with password setup."
else
    echo "Elasticsearch is not running. Please start Elasticsearch and try again."
    exit 1
fi
# 清除原来密码
# 发送请求并保存响应状态码
response=$(curl -X DELETE -u elastic:"$es_old_password" "http://127.0.0.1:9200/.security-7")

echo "清除原来密码"
echo "$response"

# 检查响应状态码
if [ "$response" != $es_reponse ]; then
    echo "请求失败,终止操作"
    exit 0
fi
echo "请求成功,继续下一步操作"

# 进入Elasticsearch的bin目录
cd /home/zfes/elasticsearch/bin || { echo "Elasticsearch bin directory not found."; exit 1; }

# 使用expect脚本自动输入密码
/usr/bin/expect <<EOF
# 设置永不超时
set timeout -1
# 详细内容打印
# exp_internal 1
spawn ./elasticsearch-setup-passwords interactive

# 忽略不相关的输出
expect {
    "N\]" { send "y\r"; exp_continue }
    -re "elastic\]:" { send "$es_password\r" ; exp_continue }
    -re "elastic\]:" { send "$es_password\r"; exp_continue }
    -re "kibana_system\]:" { send "$es_password\r"; exp_continue }
    -re "kibana_system\]:" { send "$es_password\r"; exp_continue }
    -re "logstash_system\]:" { send "$es_password\r"; exp_continue }
    -re "logstash_system\]:" { send "$es_password\r"; exp_continue }
    -re "beats_system\]:" { send "$es_password\r"; exp_continue }
    -re "beats_system\]:" { send "$es_password\r"; exp_continue }
    -re "apm_system\]:" { send "$es_password\r"; exp_continue }
    -re "apm_system\]:" { send "$es_password\r"; exp_continue }
    -re "remote_monitoring_user\]:" { send "$es_password\r"; exp_continue }
    -re "remote_monitoring_user\]:" { send "$es_password\r"; exp_continue }
}

expect eof
EOF

echo "Password has been updated for all users."

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低调D树苗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值