Skip to content

Instantly share code, notes, and snippets.

@rajephon
Created August 20, 2017 09:45
Show Gist options
  • Select an option

  • Save rajephon/f825f1055e909b24f655a75b15d575bb to your computer and use it in GitHub Desktop.

Select an option

Save rajephon/f825f1055e909b24f655a75b15d575bb to your computer and use it in GitHub Desktop.

Revisions

  1. rajephon created this gist Aug 20, 2017.
    97 changes: 97 additions & 0 deletions wol_for_d_link_router.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    #!/usr/bin/env python
    # -*- coding: utf8 -*-

    # The MIT License (MIT)
    # Copyright (C) 2016 rajephon<rajephon@gmail.com>

    # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


    import urllib
    import urllib2, base64, re

    username = "AUTHUSER"
    password = "AUTHPASSWORD"
    url = "http://ROUTER_URL/"
    wolPath = "/main_details/wol.html"

    print "Python D-Link 804 WOL Servcie"
    print "- rajephon(at)gmail.com"
    print ""
    print "Connecting... "
    print "url : " + url
    print "username : " + username

    def authRequest(path):
    request = urllib2.Request(url + path)
    base64string = base64.standard_b64encode('%s:%s' % (username, password)).replace('\n','')
    request.add_header('Authorization', b'Basic ' + base64.standard_b64encode(username + b':' + password))
    return request

    def booting(idx):
    actionPath = "/boafrm/form_wol"
    # strIdx = str(idx)
    # print(strIdx)
    selectNo = "select%d" % (idx)
    values = {'type' : 'booting',
    'index' : idx,
    'enabled' : '1',
    'submit-url' : wolPath,
    'mac' : '',
    'comment' : '',
    selectNo : 'ON'}
    data = urllib.urlencode(values)
    request = urllib2.Request(url + actionPath, data)
    base64string = base64.standard_b64encode('%s:%s' % (username, password)).replace('\n','')
    request.add_header('Authorization', b'Basic ' + base64.standard_b64encode(username + b':' + password))
    return request
    # print(the_page)

    request = authRequest(wolPath)
    try:
    response = urllib2.urlopen(request)
    the_page = response.read()
    # print the_page
    macAddr = re.search(r"^var str = '([^']+)';$", the_page, re.MULTILINE)
    macCount = len(macAddr.groups())
    if macCount == 0:
    print "Sorry. Can not find the WOL Mac Address"
    exit()
    print "found mac address list"
    macAddrList = macAddr.group(1)
    macAddrSplit = macAddrList.split('&')
    # print macAddrSplit
    print "--------------------------"
    if len(macAddrSplit) < 1:
    print("Can't find MAC Address for WOL")
    exit()
    for idx, macAddr in enumerate(macAddrSplit):
    if idx != (len(macAddrSplit)-1):
    # print(idx, macAddr)
    print "%d. %s" % (idx+1, macAddr)
    while True:
    try:
    target = int(input("Select MAC address : "))
    if target < 1 or len(macAddrSplit)-1 < target:
    raise
    break
    except:
    print("That's not a valid option!")
    print("Send request wol %s..." % (macAddrSplit[target-1]))
    request = booting(target)
    response = urllib2.urlopen(request)
    the_page = response.read()
    # print(the_page)
    print("request success")
    except urllib2.HTTPError, e:
    print e
    response_code = e.getcode()
    if response_code == 401:
    print "Please check user account"
    elif response_code == 404:
    print "Please check user url"
    exit()