Skip to content

Instantly share code, notes, and snippets.

@Ashark
Created June 7, 2021 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashark/9a6a0c2af5b7c78e4acad7a07e96eb7e to your computer and use it in GitHub Desktop.
Save Ashark/9a6a0c2af5b7c78e4acad7a07e96eb7e to your computer and use it in GitHub Desktop.
#!/bin/bash
# A crutch way to extract vlans from librenms, because currently (librenms 1.63)
# the api cannot provide all vlans, however the web interface can.
# See https://github.com/librenms/librenms/issues/12607
pw="123456"
user="someuser"
url_to_login="https://yourdomain.com/login"
cookie_file="/tmp/yourdomain.com_cookies.txt"
function update_login() {
curl -k -s "$url_to_login" -b "$cookie_file" -c "$cookie_file" > /tmp/out_1.html # We need to get _token value from there
form_token=$(cat /tmp/out_1.html | grep "_token" | cut -f6 -d '"')
# echo "Extracted token: $form_token"
curl -k -s "$url_to_login" -L -b "$cookie_file" -c "$cookie_file" --data "_token=${form_token}&username=${user}&password=${pw}" > /dev/null # &remember
}
function extract_vlans() {
url="$1"
curl -k -s -L -b "$cookie_file" -c "$cookie_file" "$url" > /tmp/out.html
string=$(cat /tmp/out.html | grep -E "VLAN:|VLANs:")
# echo "$string"
if echo "$string" | grep "VLAN:" > /dev/null; then
# echo single
vlan_extracted=$(echo "$string" | sed 's/.*VLAN: \([0-9]*\)<.*/\1/' )
elif echo "$string" | grep "VLANs:" > /dev/null; then
# echo multiple
vlan_extracted=$(echo "$string" | sed 's/.*title="\([0-9 ,]*\)".*/\1/')
fi
echo "$vlan_extracted"
}
if [[ "$1" == "-l" ]]; then
update_login
else
extract_vlans "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment