-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdasan_python.py
More file actions
158 lines (133 loc) · 5.87 KB
/
dasan_python.py
File metadata and controls
158 lines (133 loc) · 5.87 KB
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
import requests
import logging
import time
from pprint import pprint
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DasanClient:
"""
Python Client to interact with Dasan WiFi Routers
"""
def __init__(self, username='', password='', router_url="192.168.1.1", bearer_token=""):
self.username = username
self.password = password
self.router_url = router_url
self.bearer_token = bearer_token
self.session = requests.Session()
self.base_url = f"http://{router_url}"
self.HEADER = {
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36',
'Connection': 'keep-alive',
'DNT': '1',
'Origin': self.base_url,
'Referer': f'{self.base_url}/wifi/settings',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
'Authorization': f'Bearer {self.token}',
}
def _get_csrf_token(self, response):
"""
Extract CSRF token from the response headers
"""
return response.headers.get('csrf', None)
def update_wifi_ssid_password(self, wifi_ssid = None, wifi_password = None, payload=None):
"""
Fetch router details.
:return: dict object containing router details or error message.
"""
url = f"{self.base_url}/dm/tr98/?objs=WLANConfiguration&page=WifiSetupPage-WirelessSetting"
if not wifi_ssid and not wifi_password:
logger.error("Neither WiFi Ssid nor WiFi Password Found")
return None
try:
# Initial GET request to retrieve CSRF token
response = self.session.get(url, headers=self.AUTH_HEADER)
response.raise_for_status()
csrf_token = self._get_csrf_token(response)
if not csrf_token:
logger.error("CSRF token not found in headers.")
return None
# Update headers with CSRF token
self.HEADER['x-csrf-token'] = csrf_token
self.HEADER['Content-Type'] = 'application/json'
# Example payload - Modify as needed
# Change the RadiusServer and MACAddress
if not payload:
payload = {
"WLANConfiguration": {
"data": [{
"iid": 1,
"RadioEnabled": False,
"SSID": wifi_ssid,
"KeyPassphrase": wifi_password,
"SSIDAdvertisementEnabled": False,
"MaxStaNum": 32,
"Security": "WpaPskWpa2Psk",
"RadiusPort": 1812,
"RadiusServer": "1.2.3.4",
"RadiusKey": "airtelbroadband",
"WPAEncryptionModes": "TKIPandAESEncryption",
"UseWps": False,
"WpsState": "Configured",
"BandSteering": True,
"MacAclPolicy": 0,
"RatelimitUpstream": 0,
"RatelimitDownstream": 0,
"MACAddress": "yy:yy:yy:yy:yy:yy"
}]
}
}
# POST request to configure the router
response = self.session.post(url, headers=self.HEADER, json=payload)
response.raise_for_status()
logger.info("Router configured successfully.")
return response.json
except requests.RequestException as e:
logger.error(f"Failed to fetch router details: {e}")
return None
def fetch_wifi_details(self):
"""
Fetch the updated router details.
:return: dict object containing updated router details or error message.
"""
url = f"{self.base_url}/dm/tr98/?objs=WLANConfiguration&page=WifiSetupPage-WirelessSetting"
try:
response = self.session.get(url, headers=self.AUTH_HEADER)
response.raise_for_status()
logger.info("Fetched updated router details successfully.")
return response.json
except requests.RequestException as e:
logger.error(f"Failed to fetch updated router details: {e}")
return None
def fetch_updated_details(self):
"""
Fetch the updated router details.
:return: dict object containing updated router details or error message.
"""
return self.fetch_wifi_details()
class DasanClientRun:
"""
Runner Class for DasanClient
"""
@staticmethod
def change_basic_wifi_settings():
client = DasanClient(router_url="192.168.1.1", bearer_token="your_auth_bearer_token_here")
# Test: Fetch router details
logger.info("<---------- Running: Fetch router details ---------->")
router_details = client.fetch_wifi_details()
pprint(router_details)
wifi_ssid = "your_new_wifi_name_here"
wifi_password = "your_new_wifi_password_here"
# Can modify the payload from above response and send that as well
payload = None
# Test: Fetch router details
logger.info("<---------- Running: Update Basic Wifi Settings ---------->")
response = client.update_wifi_ssid_password(wifi_ssid, wifi_password, payload)
pprint(response)
# Test: Fetch updated router details
logger.info("<---------- Testing: Fetch updated router details ---------->")
updated_details = client.fetch_updated_details()
pprint(updated_details)
if __name__ == "__main__":
DasanClientRun.change_basic_wifi_settings()