Skip to content

Instantly share code, notes, and snippets.

@socram8888
Created March 1, 2019 10:20
Show Gist options
  • Save socram8888/27ff154f69c18c7372d380d7ed07719a to your computer and use it in GitHub Desktop.
Save socram8888/27ff154f69c18c7372d380d7ed07719a to your computer and use it in GitHub Desktop.
Cisco EPC3825 retriever for ddclient
#!/usr/bin/env python3
import argparse
import requests
import sys
from lxml import html
parser = argparse.ArgumentParser(description='Retrieves IP from EPC3825 routers.')
parser.add_argument('--base', help='Base router prefix', default='http://192.168.0.1')
parser.add_argument('username', help='Administration user name')
parser.add_argument('password', help='Administration password')
args = parser.parse_args()
base = args.base.rstrip('/')
sess = requests.Session()
data = {
'username_login': args.username,
'password_login': args.password,
'LanguageSelect': 'en',
'Language_Submit': '0',
'login': 'Log in'
}
reply = sess.post(base + '/goform/Docsis_system', data, allow_redirects=False)
if reply.status_code != 200:
if reply.status_code != 302:
print('Expected 200 or 302, got %d' % reply.status.code, file=sys.stderr)
sys.exit(1)
redirloc = reply.headers['location']
if not redirloc.endswith('/Quick_setup.asp'):
if redirloc.endswith('/Docsis_system.asp'):
print('Invalid user or password', file=sys.stderr)
else:
print('Unexpected redirection to %s' % redirloc, file=sys.stderr)
sys.exit(1)
reply = sess.get(base + '/Status.asp')
dom = html.fromstring(reply.text)
ipTd = dom.get_element_by_id('InternetIPAddress')
if ipTd is None:
print('Cannot find IP field', file=sys.stderr)
sys.exit(1)
print(ipTd.find('../td[2]').text_content())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment