Skip to content

Instantly share code, notes, and snippets.

@IIPoliII
Created September 15, 2020 11:53
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 IIPoliII/41144a6a26e6aea8d1920daaa978e3f1 to your computer and use it in GitHub Desktop.
Save IIPoliII/41144a6a26e6aea8d1920daaa978e3f1 to your computer and use it in GitHub Desktop.
Get daily used bandwidth Hetzner Robot API
#!/bin/bash
#Created by Poli
user="YourUserHere"
password="YourPasswordHere"
currentDate=`date "+%Y-%m-%d"`
UsedBandwidth=0
ips=`curl -su "${user}:${password}" https://robot-ws.your-server.de/ip.json | jq '.[].ip.ip' | sed -e 's/^"//' -e 's/"$//'`
subnets=`curl -su "${user}:${password}" https://robot-ws.your-server.de/subnet.json | jq '.[].subnet.ip' | sed -e 's/^"//' -e 's/"$//'`
command="curl -su \"${user}:${password}\" https://robot-ws.your-server.de/traffic --data-urlencode 'type=day' --data-urlencode \"from=${currentDate}T00\" --data-urlencode \"to=${currentDate}T24\""
while IFS= read -r line ;
do
command="$command --data-urlencode \"ip[]=${line}\""
done <<< "$ips"
while IFS= read -r line ;
do
command="$command --data-urlencode \"subnet[]=${line}\""
done <<< "$subnets"
CommandOutput=`eval ${command} | jq '.[].data[].sum'`
while IFS= read -r line ;
do
UsedBandwidth=`echo "$UsedBandwidth + $line" | bc`
done <<< "$CommandOutput"
echo "Total used bandwidth today : $(printf %0.2f $UsedBandwidth) GB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment