ElasticSearch操作之重置密码脚本
#!/bin/bash
es_old_password=$1
es_password=$2
es_reponse='{"acknowledged":true}'
if pgrep -f elasticsearch > /dev/null; thenecho "Elasticsearch is running, proceeding with password setup."
elseecho "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 ]; thenecho "请求失败,终止操作"exit 0
fi
echo "请求成功,继续下一步操作"
cd /home/zfes/elasticsearch/bin || { echo "Elasticsearch bin directory not found."; exit 1; }
/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
EOFecho "Password has been updated for all users."