#!/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