Skip to content

Instantly share code, notes, and snippets.

@making
Last active July 10, 2020 19:27
Show Gist options
  • Save making/34e71cb1a56e1280545ade7c9a5032f1 to your computer and use it in GitHub Desktop.
Save making/34e71cb1a56e1280545ade7c9a5032f1 to your computer and use it in GitHub Desktop.
How to change admin password of Nexus 3 via API
source change_admin_password.sh
change_admin_password http://localhost:8081 admin123 hogehoge
change_admin_password https://localhost:8443 admin123 hogehoge -k
#!/bin/bash
function change_admin_password() {
local NEXUS_URL=$1
local NEXUS_OLD_PWD=$2
local NEXUS_NEW_PWD=$3
shift 3
local CURL_OPTS="$@"
local SCRIPT_NAME=change_admin_password
read -r -d '' SCRIPT_JSON << EOF
{
"name": "${SCRIPT_NAME}",
"type": "groovy",
"content": "security.securitySystem.changePassword('admin', args)"
}
EOF
CHECK_SCRIPT_STATUS=`curl ${CURL_OPTS} -s -o /dev/null -I -w "%{http_code}" -u "admin:${NEXUS_OLD_PWD}" "${NEXUS_URL}/service/siesta/rest/v1/script/${SCRIPT_NAME}"`
if [ "${CHECK_SCRIPT_STATUS}" == "404" ];then
echo "> ${SCRIPT_NAME} is not found (${CHECK_SCRIPT_STATUS})"
echo "> creating script (${SCRIPT_NAME}) ..."
curl ${CURL_OPTS} -H "Accept: application/json" -H "Content-Type: application/json" -d "${SCRIPT_JSON}" -u "admin:${NEXUS_OLD_PWD}" "${NEXUS_URL}/service/siesta/rest/v1/script/"
elif [ "${CHECK_SCRIPT_STATUS}" == "401" ];then
echo "> Unauthorized (${CHECK_SCRIPT_STATUS})"
return
else
echo "> ${SCRIPT_NAME} is found (${CHECK_SCRIPT_STATUS})"
echo "> updating script (${SCRIPT_NAME}) ..."
curl ${CURL_OPTS} -XPUT -H "Accept: application/json" -H "Content-Type: application/json" -d "${SCRIPT_JSON}" -u "admin:${NEXUS_OLD_PWD}" "${NEXUS_URL}/service/siesta/rest/v1/script/${SCRIPT_NAME}"
fi
echo "> updating password ..."
CHECK_RUN_STATUS=`curl ${CURL_OPTS} -s -o /dev/null -w "%{http_code}" -H "Content-Type: text/plain" -u "admin:${NEXUS_OLD_PWD}" "${NEXUS_URL}/service/siesta/rest/v1/script/${SCRIPT_NAME}/run" -d "${NEXUS_NEW_PWD}"`
if [ "${CHECK_RUN_STATUS}" == "200" ];then
echo "> succeeded!"
else
echo "> failed! (${CHECK_RUN_STATUS})"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment