Skip to content

Instantly share code, notes, and snippets.

@bdurrow
Created March 19, 2018 04:13
Show Gist options
  • Save bdurrow/ee98a53bc8191142e75101438e3059a5 to your computer and use it in GitHub Desktop.
Save bdurrow/ee98a53bc8191142e75101438e3059a5 to your computer and use it in GitHub Desktop.
Use dnsmasq to trigger miner pool configuration
#!/bin/bash
#set -x
#This script is made to be used with dnsmasq using the dhcp-script option
#I use it on a ASUS AC66U with the Merlin Firmware and entware; I don't
#Believe that entware is requied; enternal dependencies are sed and curl
#Which are available in the standard firmware.
#Put this script in /jffs/custom_scripts/miner-setup-after-dhcp.sh (chmod 755)
#and create a file called /jffs/configs/dnsmasq.conf.add with the following line (no preceeding #):
#dhcp-script=/jffs/custom_scripts/miner-setup-after-dhcp.sh
#If you find it helpful please consider making a donation
#BTC: 1eYSRdRcQesXsZanRQZFgSFYxEFhZFUxR
#BCH: 1JWJmxswTG4Ycrig3yJSot8DBoiF1em47o
#/jffs/custom_scripts/dhcp-script.sh old 02:57:42:0c:00:00 192.168.1.118 antMiner
DNSMASQ_COMMAND=$1
DNSMASQ_MAC=$2
DNSMASQ_IP=$3
DNSMASQ_HOSTNAME=$4
#If this isn't set we won't log to a file; logging is currently minimal
LOG='/jffs/logs/dns-script.log'
#Add mock stuff here
#For truncated exponential backoff
RETRY_INITIAL_SLEEP=1
RETRY_MAX_SLEEP=30
RETRY_MAX_RETRIES=10
#If either of these aren't set pushover isn't used
PUSHOVER_USER_KEY='<<REDACTED>>'
PUSHOVER_APP_TOKEN='<<REDACTED>>'
#Username and password to use web gui
AUTH_USER=root
AUTH_PASS=root
#I make my worker out of the MAC address; Via doesn't respect colons
MAC_NO_COLONS=$(echo "${DNSMASQ_CLIENT_ID}" | sed -e 's/://g')
pool_user="<<REDACTED>>"
pool_worker="${MAC_NO_COLONS}"
#Configure these to however you want; I don't know what I'm doing here
pool1url=bch.viabtc.com:3333
pool1user=${pool_user}.${MAC_NO_COLONS}
pool1pw=bogus5
pool2url=smart.viabtc.com:3333
pool2user=${pool1user}
pool2pw=${pool1pw}
pool3url=btc.viabtc.com:3333
pool3user=${pool1user}
pool3pw=${pool1pw}
# Retries a command a with backoff.
#
# The retry count is given by ATTEMPTS (default 5), the
# initial backoff timeout is given by RETRY_INITIAL_SLEEP
# in seconds (default 1.)
#
# Successive backoffs double the timeout without exceeding
# RETRY_MAX_SLEEP (default 30 seconds).
#
# Beware of set -e killing your whole script!
#Ripped off from https://coderwall.com/p/--eiqg/exponential-backoff-in-bash
with_truncated_backoff() {
local max_attempts=${RETRY_MAX_RETRIES-5}
local sleep_time=${RETRY_INITIAL_SLEEP-1}
local max_sleep_time=${RETRY_MAX_SLEEP-30}
local attempt=0
local exitCode=0
while [[ $attempt -lt $max_attempts ]]; do
response=$("$@")
exitCode=$?
if [[ $exitCode == 0 ]]; then
break
fi
echo "Failure! Retrying in $sleep_time.." 1>&2
sleep $sleep_time
attempt=$(( attempt + 1 ))
sleep_time=$(( sleep_time * 2 ))
if [ ${sleep_time} -gt ${max_sleep_time} ]; then
sleep_time=${max_sleep_time}
fi
done
#echo "Exited loop with ${attempt} of ${max_attempts}"
if [[ $exitCode != 0 ]]; then
echo "You've failed me for the last time! ($@)" 1>&2
fi
return $exitCode
}
if [ "${DNSMASQ_SUPPLIED_HOSTNAME}" != "antMiner" ]; then
exit 0
fi
# --header 'X-Requested-With: XMLHttpRequest' \
with_truncated_backoff curl --request POST --fail --silent \
--digest --user ${AUTH_USER}:${AUTH_PASS} \
--data-urlencode "_ant_pool1url=${pool1url}" \
--data-urlencode "_ant_pool1user=${pool1user}" \
--data-urlencode "_ant_pool1pw=${pool1pw}" \
--data-urlencode "_ant_pool2url=${pool2url}" \
--data-urlencode "_ant_pool2user=${pool2user}" \
--data-urlencode "_ant_pool2pw=${pool2pw}" \
--data-urlencode "_ant_pool3url=${pool3url}" \
--data-urlencode "_ant_pool3user=${pool3user}" \
--data-urlencode "_ant_pool3pw=${pool3pw}" \
--data-urlencode "_ant_nobeeper=false" \
--data-urlencode "_ant_notempoverctrl=false" \
--data-urlencode "_ant_fan_customize_switch=false" \
--data-urlencode "_ant_fan_customize_value=" \
--data-urlencode "_ant_freq=" \
--data-urlencode "_ant_voltage=0706" \
http://${DNSMASQ_IP}/cgi-bin/set_miner_conf.cgi
RC=$?
if [ ${RC} -eq 0 ]; then
LOG_MESSAGE="SUCCESS: ${DNSMASQ_COMMAND} ${MAC_NO_COLONS} at ${DNSMASQ_IP}"
else
LOG_MESSAGE="FAILURE: ${DNSMASQ_COMMAND} ${MAC_NO_COLONS} at ${DNSMASQ_IP}"
fi
if [ -n "${LOG}" ]; then
TIME_STAMP=$(date "+%F %T %z")
echo "${TIME_STAMP}: ${LOG_MESSAGE}" >> ${LOG}
fi
if [ -n "${PUSHOVER_APP_TOKEN}" -a -n "${PUSHOVER_USER_KEY}" -a -n "${LOG_MESSAGE}" ]; then
with_truncated_backoff curl --fail --silent \
--form-string "token=${PUSHOVER_APP_TOKEN}" \
--form-string "user=${PUSHOVER_USER_KEY}" \
--form-string "message=${LOG_MESSAGE}" \
https://api.pushover.net/1/messages.json
fi
exit 0
#If you want to test with this you need to move up to where indicated
mock(){
DNSMASQ_COMMAND='old'
DNSMASQ_MAC='02:57:42:0c:00:00'
DNSMASQ_IP='192.168.1.118'
DNSMASQ_HOSTNAME='antMiner'
DNSMASQ_CLIENT_ID='02:57:42:0c:00:00'
DNSMASQ_INTERFACE='br0'
DNSMASQ_LEASE_LENGTH='86400'
DNSMASQ_REQUESTED_OPTIONS='1,3,6,12,15,28,42'
DNSMASQ_SUPPLIED_HOSTNAME='antMiner'
DNSMASQ_TAGS='lan br0'
DNSMASQ_TIME_REMAINING='86400'
DNSMASQ_VENDOR_CLASS='udhcp 1.21.1'
HOME='/'
HOSTNAME='RT-AC66U-4BE0'
IFS='
'
LOG='/tmp/log'
PATH='/sbin:/bin:/usr/sbin:/usr/bin:/opt/sbin:/opt/bin'
#PPID='291'
PS1='\w \$ '
PS2='> '
PS4='+ '
PWD='/'
SHELL='/bin/sh'
SHLVL='1'
TERM='vt100'
TZ='MST7DST,M3.2.0/2,M10.2.0/2'
USER='root'
}
mock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment