#!/bin/sh ########################################################################### ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## ## Please see: http://www.gnu.org/licenses/gpl.html for legal details, ## ## rights of fair usage, the disclaimer and warranty conditions. ## ########################################################################### ########################################################################### ## CONFIGURATION ## ########################################################################### # Connect Box router IP or hostname CONNECT_BOX_LOCAL_ADDRESS='192.168.0.1' # Connect Box router password CONNECT_BOX_PASSWORD='myplaintextsecret' # Set this to the cloudflare zone name. DOMAIN='randy.com' # Set this to the A record name. RECORD_NAME='forward.randy.com' # Set this to your account e-mail address. EMAIL='me@domain.tld' # Set this to your CloudFlare API key. API_KEY='c928dae55b8edcadbc98c079c921db77f53a8' ########################################################################### ## INTERNALS ## ########################################################################### LOCK_FILE='/var/lock/upc-connectbox-cloudflare' # Acquire a lock. if mkdir $LOCK_FILE 2>/dev/null; then trap '{ rm -rf $LOCK_FILE; }' KILL QUIT TERM EXIT INT HUP else exit 0 fi # Retrieve the external address. CONNECT_BOX_EXTERNAL_ADDRRESS=`cat << EOF | /usr/bin/env python - $CONNECT_BOX_LOCAL_ADDRESS $CONNECT_BOX_PASSWORD # -*- coding: utf-8 -*- # imports and packages from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support import expected_conditions as EC from contextlib import contextmanager import unittest, time, re, time, os, sys, random, signal # the tool takes as parameter: # - the IP address of the ZTE router # - the username # - the password if len(sys.argv) != 3: print "Syntax: " + sys.argv[0] + " " + "" + " " + "" sys.exit(1) upc = sys.argv[1] pwd = sys.argv[2] try: # initialize PhantomJS wd = webdriver.PhantomJS(service_log_path='/var/ghostdriver.log') # initialize wait wait = WebDriverWait(wd, 60) wd.get('http://' + upc) wd.find_element_by_id('loginPassword').click() wd.find_element_by_id('loginPassword').clear() wd.find_element_by_id('loginPassword').send_keys(pwd) wait.until(EC.element_to_be_clickable((By.NAME,'id_common_login'))) wd.find_element_by_name('id_common_login').click() # "Admin" menu item. wait.until(EC.element_to_be_clickable((By.XPATH,'//*[text() = "Admin"]'))) wd.find_element_by_xpath('//*[text() = "Admin"]').click() # "Admin->Info" menu item. wait.until(EC.element_to_be_clickable((By.NAME, 'common_page/Info'))) wd.find_element_by_name('common_page/Info').click(); # "WanIPv4" element contents. wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="WanIPv4" and text() !=""]'))) print wd.find_element_by_xpath('//*[@id="WanIPv4"]').text except: # Tough luck! #print "Unexpected error:", sys.exc_info()[0] pass finally: # Logout if possible. try: wd.find_element_by_id("c_mu30").click() except: # Tough luck! pass wd.close() wd.service.process.send_signal(signal.SIGTERM) wd.quit() EOF ` ### DEBUG #echo $CONNECT_BOX_EXTERNAL_ADDRRESS #exit 0 if [ -z $CONNECT_BOX_EXTERNAL_ADDRRESS ]; then echo "Unable to retrieve Connect Box external address." exit 1 fi FLARE=$(curl -s https://www.cloudflare.com/api_json.html \ -d 'a=rec_load_all' \ -d "tkn=$API_KEY" \ -d "email=$EMAIL" \ -d "z=$DOMAIN") if [ -z $FLARE ]; then echo "Unable to retrieve CloudFlare zone." echo "Please check the API key." exit 1 fi ZONE_ADDRESS=`echo $FLARE | sed -E "s/.*,\"name\":\"$RECORD_NAME\",[^}]*?,\"type\":\"A\",[^}]*?,\"content\":\"([0-9\.]+?)\",.*/\1/"` if [ -z $ZONE_ADDRESS ]; then echo "Unable to retrieve the CloudFlare zone IP address." exit 1 fi # if the Connect Box external address matches the zone address terminate if [ $CONNECT_BOX_EXTERNAL_ADDRRESS = $ZONE_ADDRESS ]; then exit 0 fi RECORD_ID=`echo $FLARE | sed -E "s/.*\"rec_id\":\"([0-9]+?)\",[^}]*?,\"name\":\"$RECORD_NAME\",[^}]*?,\"type\":\"A\",.*/\1/"` if [ -z $RECORD_ID ]; then echo "Unable to retrieve CloudFlare record ID for the zone." exit 1 fi FLARE=$(curl -s https://www.cloudflare.com/api_json.html \ -d 'a=rec_edit' \ -d "tkn=$API_KEY" \ -d "id=$RECORD_ID" \ -d "email=$EMAIL" \ -d "z=$DOMAIN" \ -d 'type=A' \ -d "name=$RECORD_NAME" \ -d "content=$CONNECT_BOX_EXTERNAL_ADDRRESS" \ -d 'service_mode=0' \ -d 'ttl=1') RESULT=`echo $FLARE | sed -E "s/.*,\"result\":\"([^\"]*)\",.*/\1/"` if [ $RESULT = "success" ]; then echo "$RECORD_NAME now points to $CONNECT_BOX_EXTERNAL_ADDRRESS" exit 0 fi exit 1