Skip to content

Instantly share code, notes, and snippets.

@stypr
Created February 13, 2018 07:12
Show Gist options
  • Select an option

  • Save stypr/97a25600ef2f30f14792213d2807a260 to your computer and use it in GitHub Desktop.

Select an option

Save stypr/97a25600ef2f30f14792213d2807a260 to your computer and use it in GitHub Desktop.

Revisions

  1. stypr created this gist Feb 13, 2018.
    84 changes: 84 additions & 0 deletions iptime_wol.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #!/usr/bin/python -u
    #-*- coding: utf-8 -*-
    # Developer: Harold Kim([email protected])

    import os
    import sys
    import re
    import requests

    class iptime:
    ''' iptime(A8004NS-M) class
    session(dict): cookie for requests
    host, username, password: required for iptime management
    '''
    session = {}
    host = ''
    username = ''
    password = ''

    def __init__(self, host):
    # check if connections are stable
    self.host = host
    r = requests.get(host+'sess-bin/login_session.cgi')
    assert "A8004NS-M" in r.text

    def login(self, id, pw):
    ''' typical login method '''
    d = {'init_status': 1, 'captcha_on': 0, 'captcha_file': '',
    'username': id, 'passwd': pw, 'default_passwd': '',
    'captcha_code': ''}
    h = {'Referer': self.host + 'sess-bin/login_session.cgi',
    'User-Agent': 'Mozilla/5.0'}
    r = requests.post(self.host + '/sess-bin/login_handler.cgi', d, h)
    r = r.text
    if 'efm_session_id' in r:
    sess = r.split("setCookie('")[1].split("')")[0]
    self.session['efm_session_id'] = sess
    return True
    else:
    raise Exception('Incorrect Credentials!')

    def list(self):
    ''' list wol '''
    if not self.session['efm_session_id']:
    raise Exception('Not Authenticated')
    r = requests.get(self.host + 'sess-bin/timepro.cgi?tmenu=iframe' +
    '&smenu=expertconfwollist', cookies=self.session)
    r = r.text
    r = r.split('name="remotepc_wollist" style="padding:0; margin:0;"')[1]
    r = r.split('<tr ')[1:]

    # clean up useless stuff
    clean_tag = re.compile('<.*?>')
    for i in xrange(len(r)):
    r[i] = r[i].split("<td ")
    for j in xrange(len(r[i])):
    r[i][j] = re.sub(clean_tag, '', '<td ' + r[i][j]).strip()
    if len(r[i]) <= 2:
    r[i] = []
    r[i] = [k for k in r[i] if k]
    r = r[1:]
    r = [k for k in r if k]

    # return format: [[no, mac, id], ...]
    return r

    def wake(self, mac):
    ''' wake wol '''
    if not self.session['efm_session_id']:
    raise Exception('Not Authenticated')
    d = {'tmenu': 'iframe', 'smenu': 'expertconfwollist', 'nomore': '0',
    'wakeupchk': mac, 'act': 'wake'}
    r = requests.post(self.host + 'sess-bin/timepro.cgi',
    d, cookies=self.session)
    assert mac in r.text
    return True

    if __name__ == "__main__":
    i = iptime('http://192.168.0.1/')
    i.login('stypr', 'stypr')
    wol_list = i.list()
    print(wol_list)
    print i.wake('12-34-56-78-AA-BB')