Skip to content

Instantly share code, notes, and snippets.

@PackeTsar
Created February 18, 2023 17:35
Show Gist options
  • Save PackeTsar/a4a791744c195c06f3a13c2bedb66d3f to your computer and use it in GitHub Desktop.
Save PackeTsar/a4a791744c195c06f3a13c2bedb66d3f to your computer and use it in GitHub Desktop.
Add Netbox User via Script
#!/usr/bin/env bash
# Set API key env variable like:
# export NETBOX_API_KEY="blahblahblah"
# Usage: ./Add_Netbox_User NETBOX_HOST USERNAME FIRST_NAME LAST_NAME EMAIL GROUP_ID PASSWORD
# Example: ./Add_Netbox_User netbox.example.com will.riker William Riker [email protected] 1 TroiFan1
if [[ -z "$NETBOX_API_KEY" ]]; then
echo "Must provide NETBOX_API_KEY in environment" 1>&2
echo " Example: export NETBOX_API_KEY='blahblahblah'" 1>&2
exit 1
fi
if [[ $# -lt 7 ]] ; then
programname=$0
echo 'ERROR: You must provide proper arguments'
echo " Example Usage: $programname NETBOX_HOST USERNAME FIRST_NAME LAST_NAME EMAIL GROUP_ID PASSWORD"
exit 0
fi
set -u # Set nounset so non-existent variables will cause an error
netbox_api_key=$NETBOX_API_KEY
netbox_host=$1
username=$2
first_name=$3
last_name=$4
email=$5
group_id=$6
password=$7
curl -X POST https://$netbox_host/api/users/users/ \
-H "Content-Type: application/json" \
-H "Authorization: Token $NETBOX_API_KEY" \
--data \
"
{
\"username\": \"$username\",
\"first_name\": \"$first_name\",
\"last_name\": \"$last_name\",
\"email\": \"$email\",
\"groups\": [
$group_id
],
\"password\": \"$password\"
}
"
echo ""
echo ""
echo ""
echo "https://$netbox_host/"
echo "Username: $username"
echo "Password: $password"
echo ""
echo "Please log in and change your password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment