#!/bin/bash # curl --silent http://pvserver:password@pvwr01.example/index.fhtml # | html2text | grep --max-count=1 'aktuell.*Gesamtenergie' # aktuell 50   WGesamtenergie 23257   kWh   # catch errors set -u set -e # preliminaries HOSTNAME="$1" shift REPEAT="$1" shift READINGS="$*" USERNAME="pvserver" PASSWORD="password" URL="http://${USERNAME}:${PASSWORD}@${HOSTNAME}/index.fhtml" REGEXP="aktuell.*Gesamtenergie" # SC2034 for repeat in $(seq 1 "${REPEAT}"); do NOW="$(date +%s)" CURLOUT="$(curl --silent "${URL}" | html2text | grep --max-count=1 "${REGEXP}")" # pull requested readings from curl output for READING in $READINGS; do case "$READING" in "Gesamtenergie"|"aktuell") OUTPUT="$(printf "%s\\n" "${CURLOUT}" | sed -n "/.*${READING}[[:space:]]\\+\\([[:digit:]]\\+\\).*/{s//\\1/;p;q;}")" # PVWR prints "x x x" in the dark, that is parsed # into the empty string by the sed regexp, make # that zero here printf "%d: %s = %d\\n" "${NOW}" "${READING}" "${OUTPUT:-0}" ;; *) # we don't have what the user asked for, print error message printf >&2 "no reading %s\\n" "${READING}" exit 1 ;; esac done if [ "${repeat}" != "${REPEAT}" ]; then sleep 30 fi done