Skip to content

Instantly share code, notes, and snippets.

@luginbash
Last active November 23, 2016 07:55
Show Gist options
  • Save luginbash/6226ed151504efc4353fa0135152bba5 to your computer and use it in GitHub Desktop.
Save luginbash/6226ed151504efc4353fa0135152bba5 to your computer and use it in GitHub Desktop.
Handling SNMP traps from another server
function cfPushRecord() {
local cfZoneId=$1
local cfRecordId=$2
local recName=$3
local recVal=$4
t_exec=$(date -u +%s)
validateStr=$t_exec\ \#$(sha1sum <<< $t_exec|cut -c-8)
curl -sX "PUT" "https://api.cloudflare.com/client/v4/zones/${cfZoneId}/dns_records/${cfRecordId}" \
-H "X-Auth-Key: ${cfSecret}" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: ${cfLogin}" \
-d "{\"type\":\"A\",\"name\":\"$recName\",\"content\":\"$recVal\",\"proxied\":false,\"ttl\":120}" \
-o $TEMP_DIR/result.json
echo $validateStr
}
#!/bin/bash
SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Functions that are needed for this script to run, in this case only 1 fun needed
source ${SCRIPT_HOME}/functions
# Secrets, Passwords
source ${SCRIPT_HOME}/.passwd
# Settigns - Zone IDs and Domain record id here
source ${SCRIPT_HOME}/settings
while read opts; do
#echo "------------------------" $opts
if [[ "$opts" == "IF-MIB::"* ]];then
key="`grep -Po '(?<=::)\w+' <<< ${opts}`"
value="$(grep -Po '(?<=\ )\w+' <<< "${opts}")"
#echo ">>>>>>>>>>>>>" $key=$value
eval $key=$value
fi
done
if [[ $ifAdminStatus == "up" ]] && [[ $ifOperStatus == "up" ]]; then
ifName="$(snmpget -Oqs -c $COMMUNITY -v2c $HOST ifName.$ifIndex | awk '{print $2}')"
ifAddr="$(snmpwalk -Oqs -c $COMMUNITY -v2c $HOST IpAdEntIfIndex|awk -v "ifIndex=$ifIndex" '$2 ~ ifIndex {print $1}'|cut -d. -f2-)"
# update dns record
ifRecId=id_${ifName}
ifRecId=${!ifRecId}
cfPushRecord ${zoneId} ${ifRecId} ${ifName} ${ifAddr}
fi
COMMUNITY="private"
HOST=192.168.88.1
zoneId="abcdef000..."
id_pppoe-out1="abcdef000..."
id_pppoe-out2="abcdef000..."
id_pppoe-out3="abcdef000..."
cfSecret=
cfLogin=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment