This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tpLink.py
168 lines (124 loc) · 5.68 KB
/
tpLink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#! /usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
import json, base64
except ImportError:
# Fall back to Python 2's
import urllib2, base64, json
with open('config.json') as data_file:
data = json.load(data_file)
auth = 'Basic ' + base64.b64encode(data["Login"]["User"]+':'+data["Login"]["Password"])
def makeRequest(url,heads):
request = urllib2.Request(url, None, heads)
return (urllib2.urlopen(request)).read()
def addTarget(Description,domain1,domain2,domain3,domain4):
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessTargetsRpm.htm?target_type=0&targets_lists_name='+Description+ '&dst_ip_start=&dst_ip_end=&dst_port_start=&dst_port_end=&proto=0&Commonport=0&url_0='+domain1+'&url_1='+domain2+'&url_2='+domain3+'&url_3='+domain4+'&Changed=0&SelIndex=0&fromAdd=0&Page=1&Save=Save'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessTargetsRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def addHostLan(Description,ipStart,ipEnd):
Description = Description.replace(" ", "+")
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlHostsListsRpm.htm?addr_type=1&hosts_lists_name='+Description+'&src_ip_start='+ipStart+'&src_ip_end='+ipEnd+'&mac_addr=&Changed=0&SelIndex=0&fromAdd=0&Page=1&Save=Save'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlHostsListsRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def getTargetList(number):
url = 'http://' + IpRouter + '/userRpm/AccessCtrlAccessTargetsRpm.htm?Page='+str(number)
heads = { 'Referer' : 'http://' + IpRouter + '/userRpm/AccessCtrlAccessTargetsRpm.htm',
'Authorization' : auth
}
page = makeRequest(url,heads)
target = []
#Parse out target list
page = page.split("new Array(", 1)
page = page[1].split('0,0 );', 1)
page = page[0].replace('"',"").replace(' ',"")
data = page.split("\n")
for index in range(len(data)):
if(index != 0):
target.append( data[index].split(",") )
return target
def countAllTarget():
return (len(getTargetList(1))-1)+(len(getTargetList(2))-1)
def addRule(ruleName,numberTarget):
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm?rule_name='+ruleName+'&hosts_lists=0&targets_lists='+str(numberTarget)+'&scheds_lists=255&enable=1&Changed=0&SelIndex=0&Page=1&Save=Save'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def reboot():
print ("Reboot")
url = 'http://' + data["IpRouter"] + '/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/SysRebootRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def enableAccessControl():
print ("Enable Acces Control")
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm?enableCtrl=1&defRule=0&Page=1'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def deleteAllRules():
print ("Delete all rules")
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm?doAll=DelAll&Page=1'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessRulesRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def deleteAllHosts():
print ("Delete all host")
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlHostsListsRpm.htm?doAll=DelAll&Page=1'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlHostsListsRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def deleteAllTarget():
print ("Delete all target")
url = 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessTargetsRpm.htm?doAll=DelAll&Page=1'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/AccessCtrlAccessTargetsRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def setIpDevice(ip,mask):
print ("Set ip device "+ip)
url = 'http://' + data["IpRouter"] + '/userRpm/NetworkCfgRpm.htm?lanip='+ip+'&lanmask=3&inputMask='+mask+'&igmp_proxy=0&Save=Save'
heads = { 'Referer' : 'http://' + data["IpRouter"] + '/userRpm/NetworkCfgRpm.htm',
'Authorization' : auth
}
return makeRequest(url,heads)
def getHostList():
url = 'http://' + IpRouter + '/userRpm/AccessCtrlHostsListsRpm.htm?Refresh=Refresh'
heads = { 'Referer' : 'http://' + IpRouter + '/userRpm/AccessCtrlHostsListsRpm.htm',
'Authorization' : auth
}
page = makeRequest(url,heads)
host = []
#Parse out target list
page = page.split("new Array(", 1)
page = page[1].split('0,0 );', 1)
page = page[0].replace('"',"").replace(' ',"")
data = page.split("\n")
for index in range(len(data)):
if(index != 0):
host.append( data[index].split(",") )
return host
if __name__ == "__main__":
print ("Welcome to apy TPLink")
# reboot()
# setIpDevice("192.168.0.2","255.255.255.0")
# addTarget("Description","domain1","domain2","domain3","domain4")
# deleteAllTarget()
# countAllTarget()
# getTargetList(number)
# addHostLan("Description","ipStart","ipEnd")
# deleteAllHosts()
# addRule("ruleName","numberTarget"):
# deleteAllRules()
# print countTargets()
# enableAccessControl():
# print getHostList()