Advertisement
Guest User

edge-dhcp-update

a guest
Apr 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.54 KB | None | 0 0
  1. COOKIES="/tmp/jar.txt"
  2. STATIC="/tmp/edge-static.json"
  3. DYNAMIC="/tmp/edge-dynamic.json"
  4. OUTPUT="/tmp/dhcp"
  5. USERNAME="<USERNAME>"
  6. PASSWORD="<PASSWORD>"
  7.  
  8.  
  9. if [[ -f "$STATIC" ]]; then
  10.   rm $STATIC
  11. fi
  12.  
  13. if [[ -f "$DYNAMIC" ]]; then
  14.   rm $DYNAMIC
  15. fi
  16.  
  17. if [[ -f "$OUTPUT" ]]; then
  18.   MD5OLD=`md5sum $OUTPUT | awk '{print $1}'`
  19.   rm $OUTPUT
  20. fi
  21.  
  22. /usr/bin/curl -s -X POST -H "Content-Type:  application/x-www-form-urlencoded" -d "username=$USERNAME&password=$PASSWORD" https://10.2.0.1 --insecure -c $COOKIES
  23. /usr/bin/curl -s "https://10.2.0.1/api/edge/data.json?data=dhcp_leases" --insecure -b $COOKIES > $DYNAMIC
  24. /usr/bin/curl -s "https://10.2.0.1/api/edge/get.json" --insecure -b $COOKIES > $STATIC
  25.  
  26. # for host in `jq -r '.GET.service."dhcp-server"."shared-network-name"'.\"$server\"'.subnet'.\"$subnet\"'."static-mapping" | to_entries[] | [.key,.value."ip-address"] | @csv' $STATIC 2> /dev/null` ; do
  27.  
  28. for server in `/usr/bin/jq --raw-output '.GET.service."dhcp-server"."shared-network-name" | to_entries[] | .key ' $STATIC`; do
  29.   if [[ ! -z "$server" ]]; then
  30.     for subnet in `/usr/bin/jq -r '.GET.service."dhcp-server"."shared-network-name"'.\"$server\"'.subnet | to_entries[] | .key' $STATIC 2> /dev/null`; do
  31.       HOSTNAME=`/usr/bin/jq -r '.GET.service."dhcp-server"."shared-network-name"'.\"$server\"'.subnet'.\"$subnet\"'."static-mapping"| to_entries[] | .key' $STATIC 2> /dev/null`
  32.       for hostname in $HOSTNAME; do
  33.         IP=`/usr/bin/jq -r '.GET.service."dhcp-server"."shared-network-name"'.\"$server\"'.subnet.'\"$subnet\"'."static-mapping"'.\"$hostname\"'."ip-address"' $STATIC`
  34.         echo "\"$IP\",\"static\",\"$server\",\"$hostname\"" >> $OUTPUT
  35.       done
  36.     done
  37.     for ip in `/usr/bin/jq -r '.output."dhcp-server-leases"'.\"$server\"' | to_entries[] | .key' $DYNAMIC 2> /dev/null` ; do
  38.       HOSTNAME=`/usr/bin/jq -r '.output."dhcp-server-leases"'.\"$server\".\"$ip\"'."client-hostname"' $DYNAMIC 2> /dev/null`
  39.       EXPIRATION=`/usr/bin/jq -r '.output."dhcp-server-leases"'.\"$server\".\"$ip\"'."expiration"' $DYNAMIC 2> /dev/null | date '+%Y-%m-%d %H:%M:%S'`
  40.       echo "\"$ip\",\"dynamic\",\"$server\",\"$HOSTNAME\"" >> $OUTPUT
  41.     done
  42.   fi
  43. done
  44.  
  45. MD5NEW="`md5sum $OUTPUT | awk '{print $1}'`"
  46.  
  47. if [ ! "$MD5OLD" == "$MD5NEW" ]; then
  48.   /usr/bin/mysql --host=10.2.0.10 --port=3307 -u dhcp-update grafana -e "truncate table dhcp"
  49.   /usr/bin/mysqlimport -s --local --fields-optionally-enclosed-by="\"" --fields-terminated-by=, --host 10.2.0.10 --port 3307 -u dhcp-update grafana /tmp/dhcp
  50.   echo "UPDATED `date`" >> /tmp/dhcp-update
  51. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement