Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Created July 14, 2025 13:46
Show Gist options
  • Save wilhelm-murdoch/93ab3df03579aa18b4621fb6f0d60395 to your computer and use it in GitHub Desktop.
Save wilhelm-murdoch/93ab3df03579aa18b4621fb6f0d60395 to your computer and use it in GitHub Desktop.
auto-join for Beszel agents
#!/bin/sh
MON_ENDPOINT="https://monitoring.beszel.hub"
case "${1}" in
--help)
echo "Usage: ${0} <command> <args>"
echo
echo "Commands:"
echo " login <username> <password>"
echo " exists <token> <name> <ip> <port>"
echo " join <token> <name> <ip> <port> <user-id>"
echo " leave <token> <id>"
echo " install <port> <public-key>"
exit 0
;;
login)
login "${2}" "${3}"
;;
exists)
exists "${2}" "${3}" "${4}" "${5}"
;;
join)
join "${2}" "${3}" "${4}" "${5}" "${6}"
;;
leave)
leave "${2}" "${3}"
;;
install)
install "${2}" "${3}"
;;
*)
echo "Unknown command: ${0} ${1}"
exit 1
;;
esac
# login <username> <password>
login() {
curl \
--silent \
-X POST \
-H "Content-Type: application/json" \
--data "{\"identity\":\"${1}\",\"password\":\"${2}\"}" \
"${MON_ENDPOINT}/api/collections/users/auth-with-password" |
sed -n 's/.*"token":"\([^"]*\)".*/\1/p'
}
# exists <token> <name> <ip> <port>
exists() {
response=$(
curl \
--silent \
--get \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${1}" \
--data-urlencode "filter=name=\"${2}\" && host=\"${3}\" && port=\"${4}\"" \
"${MON_ENDPOINT}/api/collections/systems/records" |
sed -n 's/.*"totalItems":\([^,]*\).*/\1/p'
)
test "${response}" != "0"
}
# join <token> <name> <ip> <port> <user-id>
join() {
curl \
--silent \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${1}" \
--data "{\"name\":\"${2}\",\"host\":\"${3}\",\"port\":${4},\"users\":[\"${5}\"]}" \
"${MON_ENDPOINT}/api/collections/systems/records" |
sed -n 's/.*"id":\([^,]*\).*/\1/p'
}
# leave <token> <id>
leave() {
curl \
--silent \
-X DELETE \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${1}" \
"${MON_ENDPOINT}/api/collections/systems/records/${2}"
}
# install <port> <public-key>
install() {
curl \
--silent \
-L https://get.beszel.dev \
-o /tmp/install-agent.sh
chmod +x /tmp/install-agent.sh
/tmp/install-agent.sh -p "${1}" -k "${2}" --auto-update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment