ns2 0.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ns2/__init__.py +0 -0
- ns2/core.py +0 -0
- ns2/dbus/__init__.py +0 -0
- ns2/dbus/dbus.py +13 -0
- ns2/lib/__init__.py +0 -0
- ns2/lib/accounts.py +130 -0
- ns2/lib/commands.py +32 -0
- ns2/lib/firewalld.py +167 -0
- ns2/lib/introspection/org.fedoraproject.FirewallD1.config.xml +3 -0
- ns2/lib/introspection/org.fedoraproject.FirewallD1.xml +763 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.AccessPoint.xml +106 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.AgentManager.xml +43 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Checkpoint.xml +36 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Connection.Active.xml +185 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.DHCP4Config.xml +21 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.DHCP6Config.xml +20 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Adsl.xml +21 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Bluetooth.xml +36 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Bond.xml +40 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Bridge.xml +41 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Dummy.xml +20 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Generic.xml +27 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Hsr.xml +51 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.IPTunnel.xml +107 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Infiniband.xml +31 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Ipvlan.xml +38 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Loopback.xml +8 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Lowpan.xml +27 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Macsec.xml +109 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Macvlan.xml +39 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Modem.xml +62 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.OlpcMesh.xml +34 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.OvsBridge.xml +21 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.OvsInterface.xml +11 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.OvsPort.xml +21 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Ppp.xml +11 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Statistics.xml +35 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Team.xml +48 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Tun.xml +65 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Veth.xml +18 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Vlan.xml +45 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Vrf.xml +18 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Vxlan.xml +139 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.WiMax.xml +109 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.WifiP2P.xml +76 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.WireGuard.xml +38 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Wired.xml +53 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Wireless.xml +131 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.Wpan.xml +20 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Device.xml +407 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.DnsManager.xml +40 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.IP4Config.xml +125 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.IP6Config.xml +95 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.PPP.xml +34 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.SecretAgent.xml +94 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Settings.Connection.xml +224 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.Settings.xml +233 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.VPN.Connection.xml +42 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.VPN.Plugin.xml +204 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.WiMax.Nsp.xml +35 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.WifiP2PPeer.xml +91 -0
- ns2/lib/introspection/org.freedesktop.NetworkManager.xml +598 -0
- ns2/lib/lib.py +0 -0
- ns2/lib/network_delay.py +92 -0
- ns2/lib/networking.py +528 -0
- ns2/lib/ntl.py +188 -0
- ns2/lib/pam_client.py +37 -0
- ns2/lib/ping_data_collector.py +37 -0
- ns2/lib/snmp.py +511 -0
- ns2/lib/socket.py +132 -0
- ns2/lib/socket_client.py +62 -0
- ns2/lib/systemd.py +151 -0
- ns2/lib/test.py +374 -0
- ns2/lib/udp_client.py +227 -0
- ns2/lib/udp_server.py +167 -0
- ns2/snmp/__init__.py +0 -0
- ns2/snmp/ns_dbus_service.py +38 -0
- ns2/snmp/pam_interface.py +19 -0
- ns2/snmp/snmp_interface.py +66 -0
- ns2/ui/__init__.py +0 -0
- ns2/ui/assets/NOVUS_LOGO.svg +105 -0
- ns2/ui/assets/favicon.png +0 -0
- ns2/ui/firewalld_page.py +375 -0
- ns2/ui/fpga_page.py +24 -0
- ns2/ui/login.py +65 -0
- ns2/ui/main.py +200 -0
- ns2/ui/networking_page.py +406 -0
- ns2/ui/ntp.py +105 -0
- ns2/ui/root.py +31 -0
- ns2/ui/snmp_page.py +353 -0
- ns2/ui/terminal.py +65 -0
- ns2/ui/tests_page.py +116 -0
- ns2/ui/theme.py +25 -0
- ns2/utils.py +5 -0
- ns2-0.2.6.dist-info/METADATA +78 -0
- ns2-0.2.6.dist-info/RECORD +98 -0
- ns2-0.2.6.dist-info/WHEEL +4 -0
- ns2-0.2.6.dist-info/entry_points.txt +3 -0
ns2/lib/snmp.py
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
from dataclasses import asdict, dataclass
|
|
5
|
+
from ns.lib.commands import runCmd
|
|
6
|
+
from typing import Optional
|
|
7
|
+
import aiofiles
|
|
8
|
+
|
|
9
|
+
from ns.lib.systemd import *
|
|
10
|
+
|
|
11
|
+
snmp_config_file = "/etc/snmp/snmpd.conf"
|
|
12
|
+
default_persistent_dir_path = "/var/lib/snmp"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
USM_OID_MAP = {
|
|
18
|
+
# Authentication Protocols (RFC 3414)
|
|
19
|
+
"1.3.6.1.6.3.10.1.1.1": "NoAuth",
|
|
20
|
+
".1.3.6.1.6.3.10.1.1.2": "MD5",
|
|
21
|
+
".1.3.6.1.6.3.10.1.1.3": "SHA",
|
|
22
|
+
"1.3.6.1.6.3.10.1.1.4": "HMAC-SHA2-224",
|
|
23
|
+
"1.3.6.1.6.3.10.1.1.5": "HMAC-SHA2-256",
|
|
24
|
+
|
|
25
|
+
# Privacy Protocols (RFC 3414 + 3826)
|
|
26
|
+
"1.3.6.1.6.3.10.1.2.1": "NoPriv",
|
|
27
|
+
".1.3.6.1.6.3.10.1.2.2": "DES",
|
|
28
|
+
".1.3.6.1.6.3.10.1.2.4": "AES",
|
|
29
|
+
"1.3.6.1.6.3.10.1.2.5": "AES-192",
|
|
30
|
+
"1.3.6.1.6.3.10.1.2.6": "AES-256"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class Group:
|
|
36
|
+
Permissions: Optional[str] = None
|
|
37
|
+
Version: Optional[str] = None
|
|
38
|
+
SecName: Optional[str] = None
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class V3User:
|
|
42
|
+
UserName: Optional[str] = ''
|
|
43
|
+
Version: Optional[str] = 'usm'
|
|
44
|
+
AuthType: Optional[str] = 'SHA'
|
|
45
|
+
AuthPassphrase: Optional[str] = ''
|
|
46
|
+
PrivType: Optional[str] = 'AES'
|
|
47
|
+
PrivPassphrase: Optional[str] = ''
|
|
48
|
+
Permissions: Optional[str] = 'rwprivgroup'
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def from_dict(userDict :dict):
|
|
53
|
+
user = V3User(
|
|
54
|
+
UserName = userDict.get('UserName'),
|
|
55
|
+
Version = userDict.get('Version'),
|
|
56
|
+
AuthType = userDict.get('AuthType'),
|
|
57
|
+
AuthPassphrase = userDict.get('AuthPassphrase'),
|
|
58
|
+
PrivType = userDict.get('PrivType'),
|
|
59
|
+
PrivPassphrase = userDict.get('PrivPassphrase'),
|
|
60
|
+
Permissions = userDict.get('Permissions')
|
|
61
|
+
)
|
|
62
|
+
return user
|
|
63
|
+
|
|
64
|
+
@dataclass
|
|
65
|
+
class V2User:
|
|
66
|
+
Community:Optional[str] = ''
|
|
67
|
+
Version:Optional[str] = ''
|
|
68
|
+
Permissions:Optional[str] = ''
|
|
69
|
+
Source:Optional[str] = ''
|
|
70
|
+
SecName:Optional[str] = ''
|
|
71
|
+
|
|
72
|
+
def from_dict(userDict :dict):
|
|
73
|
+
user = V2User(
|
|
74
|
+
Community = userDict.get('Community'),
|
|
75
|
+
Version = userDict.get('Version'),
|
|
76
|
+
Permissions = userDict.get('Permissions'),
|
|
77
|
+
Source = userDict.get('Source'),
|
|
78
|
+
SecName = userDict.get('SecName')
|
|
79
|
+
)
|
|
80
|
+
return user
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# ====================================================================
|
|
87
|
+
# SNMP Files and Directories
|
|
88
|
+
# ====================================================================
|
|
89
|
+
async def _readSnmpGroupsFromFile() -> list[Group]:
|
|
90
|
+
groups = []
|
|
91
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
92
|
+
async for line in f:
|
|
93
|
+
line = line.strip("\n")
|
|
94
|
+
if line.startswith("group"):
|
|
95
|
+
g = Group()
|
|
96
|
+
fields = line.split(" ")
|
|
97
|
+
if len(fields) == 4:
|
|
98
|
+
g.Permissions = fields[1]
|
|
99
|
+
g.Version = fields[2]
|
|
100
|
+
g.SecName = fields[3]
|
|
101
|
+
groups.append(g)
|
|
102
|
+
pass #endfor
|
|
103
|
+
return groups
|
|
104
|
+
|
|
105
|
+
async def _readV2UsersFromFile() -> list[V2User]:
|
|
106
|
+
v2s = []
|
|
107
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
108
|
+
async for line in f:
|
|
109
|
+
line = line.strip("\n")
|
|
110
|
+
if line.startswith("com2sec"):
|
|
111
|
+
v2 = V2User()
|
|
112
|
+
fields = line.split(" ")
|
|
113
|
+
if len(fields) == 4:
|
|
114
|
+
v2.SecName = fields[1]
|
|
115
|
+
v2.Source = fields[2]
|
|
116
|
+
v2.Community = fields[3]
|
|
117
|
+
v2s.append(v2)
|
|
118
|
+
pass #endfor
|
|
119
|
+
return v2s
|
|
120
|
+
|
|
121
|
+
async def _readV3UsersFromFile() -> list[V3User]:
|
|
122
|
+
v3s = []
|
|
123
|
+
try:
|
|
124
|
+
async with aiofiles.open(await _getPersistentConfPath(), "r") as f:
|
|
125
|
+
async for line in f:
|
|
126
|
+
line = line.strip("\n")
|
|
127
|
+
if line.startswith("usmUser"):
|
|
128
|
+
v3 = V3User()
|
|
129
|
+
fields = line.split(" ")
|
|
130
|
+
if len(fields) == 12:
|
|
131
|
+
|
|
132
|
+
v3.UserName = fields[4].strip('"')
|
|
133
|
+
|
|
134
|
+
v3.AuthType = USM_OID_MAP.get(fields[7], f"Unknown")
|
|
135
|
+
#v3.AuthPassphrase = fields[3]
|
|
136
|
+
v3.PrivType = USM_OID_MAP.get(fields[9], f"Unknown")
|
|
137
|
+
#v3.PrivPassphrase = fields[5]
|
|
138
|
+
v3s.append(v3)
|
|
139
|
+
pass # endfor
|
|
140
|
+
except FileNotFoundError:
|
|
141
|
+
return v3s
|
|
142
|
+
return v3s
|
|
143
|
+
|
|
144
|
+
async def _writeV2User(user :V2User):
|
|
145
|
+
'''add v2 user to file'''
|
|
146
|
+
print("_writeV2User")
|
|
147
|
+
lineCount = 0
|
|
148
|
+
userIndex = -1
|
|
149
|
+
groupIndex = -1
|
|
150
|
+
|
|
151
|
+
comNumber = len(await ReadV2Users())
|
|
152
|
+
|
|
153
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
154
|
+
content = await f.readlines()
|
|
155
|
+
|
|
156
|
+
for line in content:
|
|
157
|
+
line = line.strip("\n")
|
|
158
|
+
if line.startswith("#com2sec"):
|
|
159
|
+
userIndex = lineCount + 2
|
|
160
|
+
if line.startswith("#group"):
|
|
161
|
+
groupIndex = lineCount + 3
|
|
162
|
+
lineCount = lineCount + 1
|
|
163
|
+
pass # endfor
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
newUserLine = f"com2sec comuser_{comNumber} {user.Source} {user.Community}\n"
|
|
167
|
+
newGroupLine = f"group {user.Permissions} {user.Version} comuser_{comNumber}\n"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
if userIndex < 0:
|
|
171
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
172
|
+
content.append("#com2sec sec.name source community")
|
|
173
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
174
|
+
content.append(newUserLine)
|
|
175
|
+
else:
|
|
176
|
+
content.insert(userIndex, newUserLine)
|
|
177
|
+
|
|
178
|
+
if groupIndex < 0:
|
|
179
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
180
|
+
content.append("#group group name sec.model sec.name")
|
|
181
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
182
|
+
content.append(newGroupLine)
|
|
183
|
+
else:
|
|
184
|
+
content.insert(groupIndex, newGroupLine)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
188
|
+
await f.writelines(content)
|
|
189
|
+
#content = f.readlines()
|
|
190
|
+
|
|
191
|
+
async def _writeV3UserCreateDirective(user: V3User):
|
|
192
|
+
'''add v3 user to file'''
|
|
193
|
+
print("write v3 user")
|
|
194
|
+
|
|
195
|
+
lineCount = 0
|
|
196
|
+
createUserIndex = -1
|
|
197
|
+
groupIndex = -1
|
|
198
|
+
|
|
199
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
200
|
+
content = await f.readlines()
|
|
201
|
+
|
|
202
|
+
for i, line in enumerate(content):
|
|
203
|
+
line = line.strip("\n")
|
|
204
|
+
if line.startswith("#createUser"):
|
|
205
|
+
createUserIndex = lineCount + 2
|
|
206
|
+
if line.startswith("#group"):
|
|
207
|
+
groupIndex = lineCount + 3
|
|
208
|
+
|
|
209
|
+
lineCount = lineCount + 1
|
|
210
|
+
pass # endfor
|
|
211
|
+
|
|
212
|
+
newUserLine = f"createUser {user.UserName} {user.AuthType} {user.AuthPassphrase} {user.PrivType} {user.PrivPassphrase}\n"
|
|
213
|
+
newGroupLine = f"group {user.Permissions} {user.Version} {user.UserName}\n"
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
if createUserIndex < 0:
|
|
217
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
218
|
+
content.append("#createUser username [MD5|SHA] [passphrase] [DES] [passphrase]")
|
|
219
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
220
|
+
content.append(newUserLine)
|
|
221
|
+
else:
|
|
222
|
+
content.insert(createUserIndex, newUserLine)
|
|
223
|
+
|
|
224
|
+
if groupIndex < 0:
|
|
225
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
226
|
+
content.append("#group group name sec.model sec.name")
|
|
227
|
+
content.append("#-------------------------------------------------------------------------------")
|
|
228
|
+
content.append(newGroupLine)
|
|
229
|
+
else:
|
|
230
|
+
content.insert(groupIndex, newGroupLine)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
234
|
+
await f.writelines(content)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
async def _deleteV3UserFromStorage(user: V3User):
|
|
238
|
+
'''delete v3 user from persistent storgage'''
|
|
239
|
+
|
|
240
|
+
async with aiofiles.open(await _getPersistentConfPath()) as f:
|
|
241
|
+
content = await f.readlines()
|
|
242
|
+
|
|
243
|
+
for i, line in enumerate(content):
|
|
244
|
+
if line.startswith("usmUser"):
|
|
245
|
+
|
|
246
|
+
fields = line.split(" ")
|
|
247
|
+
temp_auth_type = USM_OID_MAP.get(fields[7], f"Unknown")
|
|
248
|
+
temp_priv_type = USM_OID_MAP.get(fields[9], f"Unknown")
|
|
249
|
+
|
|
250
|
+
if user.UserName in line and temp_auth_type == user.AuthType and temp_priv_type == user.PrivType:
|
|
251
|
+
content.remove(line)
|
|
252
|
+
|
|
253
|
+
async with aiofiles.open(await _getPersistentConfPath(), "w") as f:
|
|
254
|
+
await f.writelines(content)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
async def _deleteV3UserCreateDirective(user: V3User):
|
|
258
|
+
_props = [user.UserName, user.AuthType, user.AuthPassphrase, user.PrivType, user.PrivPassphrase]
|
|
259
|
+
|
|
260
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
261
|
+
content = await f.readlines()
|
|
262
|
+
|
|
263
|
+
for i, line in enumerate(content):
|
|
264
|
+
if line.startswith("createUser") and all(p in line for p in _props):
|
|
265
|
+
content.remove(line)
|
|
266
|
+
|
|
267
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
268
|
+
await f.writelines(content)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
async def _deleteV3UserFromConfig(user: V3User):
|
|
272
|
+
'''delete v3 user from /etc/snmp/snmpd.conf'''
|
|
273
|
+
|
|
274
|
+
_props = [user.Permissions, user.Version, user.UserName]
|
|
275
|
+
|
|
276
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
277
|
+
content = await f.readlines()
|
|
278
|
+
|
|
279
|
+
for i, line in enumerate(content):
|
|
280
|
+
if line.startswith("group") and all(p in line for p in _props):
|
|
281
|
+
content.remove(line)
|
|
282
|
+
|
|
283
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
284
|
+
await f.writelines(content)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
async def _getPersistentDir() -> str:
|
|
288
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
289
|
+
async for line in f:
|
|
290
|
+
if line.startswith("persistentDir"):
|
|
291
|
+
fields = line.split(" ")
|
|
292
|
+
if len(fields) == 2:
|
|
293
|
+
return fields[1].strip("\n")
|
|
294
|
+
pass #endfor
|
|
295
|
+
return None
|
|
296
|
+
|
|
297
|
+
async def _setPersistentDir(path):
|
|
298
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
299
|
+
content = await f.readlines()
|
|
300
|
+
|
|
301
|
+
for i, line in enumerate(content):
|
|
302
|
+
if line.startswith("persistentDir"):
|
|
303
|
+
content[i] = f"persistentDir {path}\n"
|
|
304
|
+
break
|
|
305
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
306
|
+
await f.writelines(content)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
async def _getPersistentConfPath() -> str:
|
|
310
|
+
persistentDir = await _getPersistentDir()
|
|
311
|
+
return os.path.join(persistentDir, "snmpd.conf")
|
|
312
|
+
|
|
313
|
+
async def _deletePersistentDir():
|
|
314
|
+
persistentDir = await _getPersistentDir()
|
|
315
|
+
await runCmd(['rm', '-rf', persistentDir])
|
|
316
|
+
|
|
317
|
+
async def _overWriteWithDefaultSnmpConf():
|
|
318
|
+
await runCmd(['cp', './configs/snmpd.conf', '/etc/snmp/snmpd.conf'])
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
async def ResetSnmpd(bus :MessageBus) -> str:
|
|
326
|
+
# 1. Stop Snmp
|
|
327
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
328
|
+
# 2. Remove Persistent Dir
|
|
329
|
+
await _deletePersistentDir()
|
|
330
|
+
# 3. Reset Main Config
|
|
331
|
+
await _overWriteWithDefaultSnmpConf()
|
|
332
|
+
# 4. Set Tmp Path for Persistent Dir
|
|
333
|
+
await _setPersistentDir("/var/lib/tmp")
|
|
334
|
+
# 5. Start Snmp
|
|
335
|
+
await systemd_start(bus, 'snmpd.service')
|
|
336
|
+
# 6. Stop Snmp
|
|
337
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
338
|
+
# 7. Remove Temp Persistent Dir
|
|
339
|
+
await _deletePersistentDir()
|
|
340
|
+
# 8. Set Real Path for Persistent Dir
|
|
341
|
+
await _setPersistentDir("/var/lib/snmp")
|
|
342
|
+
# 9. Start Snmp
|
|
343
|
+
await systemd_start(bus, 'snmpd.service')
|
|
344
|
+
|
|
345
|
+
return "snmpd reset"
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
# ====================================================================
|
|
352
|
+
# V3 USERS
|
|
353
|
+
# ====================================================================
|
|
354
|
+
async def AddV3User(bus :MessageBus, user: V3User):
|
|
355
|
+
'''add v3 user'''
|
|
356
|
+
print('add v3 dude')
|
|
357
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
358
|
+
await _writeV3UserCreateDirective(user)
|
|
359
|
+
await systemd_start(bus, 'snmpd.service') # real user created
|
|
360
|
+
await _deleteV3UserCreateDirective(user)
|
|
361
|
+
|
|
362
|
+
async def ReadV3UserByUsername(username: str) -> V3User:
|
|
363
|
+
u :V3User
|
|
364
|
+
for u in (await ReadV3Users()):
|
|
365
|
+
if u.UserName == username:
|
|
366
|
+
return u
|
|
367
|
+
return None
|
|
368
|
+
|
|
369
|
+
async def ReadV3Users() -> list[V3User]:
|
|
370
|
+
print('read v3 users')
|
|
371
|
+
groups = await _readSnmpGroupsFromFile()
|
|
372
|
+
v3s = await _readV3UsersFromFile()
|
|
373
|
+
g: Group
|
|
374
|
+
for g in groups:
|
|
375
|
+
v3: V3User
|
|
376
|
+
for v3 in v3s:
|
|
377
|
+
if g.SecName == v3.UserName:
|
|
378
|
+
print(g.SecName)
|
|
379
|
+
v3.Permissions = g.Permissions
|
|
380
|
+
v3.Version = g.Version
|
|
381
|
+
pass #endfor
|
|
382
|
+
pass #endfor
|
|
383
|
+
print(v3s)
|
|
384
|
+
return v3s
|
|
385
|
+
|
|
386
|
+
async def EditV3User(bus: MessageBus, inituser :V3User, finaluser: V3User):
|
|
387
|
+
'''edit v3 user'''
|
|
388
|
+
print("EditV3User")
|
|
389
|
+
|
|
390
|
+
if not inituser:
|
|
391
|
+
print("v3 USER NOT FOUND")
|
|
392
|
+
#sys.exit()
|
|
393
|
+
|
|
394
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
395
|
+
await _deleteV3UserFromStorage(inituser) # remove actual
|
|
396
|
+
await _deleteV3UserFromConfig(inituser) # remove group
|
|
397
|
+
await _writeV3UserCreateDirective(finaluser) # add grup and create
|
|
398
|
+
await systemd_start(bus, 'snmpd.service')
|
|
399
|
+
await _deleteV3UserCreateDirective(finaluser) # remove create dir
|
|
400
|
+
|
|
401
|
+
async def DeleteV3User(bus: MessageBus, user: V3User):
|
|
402
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
403
|
+
await _deleteV3UserFromConfig(user)
|
|
404
|
+
await _deleteV3UserFromStorage(user)
|
|
405
|
+
await systemd_start(bus, 'snmpd.service')
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
# ====================================================================
|
|
409
|
+
# V2 USERS
|
|
410
|
+
# ====================================================================
|
|
411
|
+
async def AddV2User(bus: MessageBus, user: V2User):
|
|
412
|
+
'''add v2 user'''
|
|
413
|
+
print("add a v2 user")
|
|
414
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
415
|
+
await _writeV2User(user)
|
|
416
|
+
await systemd_start(bus, 'snmpd.service')
|
|
417
|
+
|
|
418
|
+
async def ReadV2UserByCommunity(community: str) -> V2User:
|
|
419
|
+
u :V2User
|
|
420
|
+
for u in await ReadV2Users():
|
|
421
|
+
if u.Community == community:
|
|
422
|
+
return u
|
|
423
|
+
return None
|
|
424
|
+
|
|
425
|
+
async def ReadV2UserBySecurityName(secName: str) -> V2User:
|
|
426
|
+
u :V2User
|
|
427
|
+
for u in await ReadV2Users():
|
|
428
|
+
if u.SecName == secName:
|
|
429
|
+
return u
|
|
430
|
+
return None
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
async def ReadV2Users() -> list[V2User]:
|
|
434
|
+
groups = await _readSnmpGroupsFromFile()
|
|
435
|
+
v2s = await _readV2UsersFromFile()
|
|
436
|
+
g: Group
|
|
437
|
+
for g in groups:
|
|
438
|
+
v2: V2User
|
|
439
|
+
for v2 in v2s:
|
|
440
|
+
if g.SecName == v2.SecName:
|
|
441
|
+
v2.SecName = g.SecName
|
|
442
|
+
v2.Permissions = g.Permissions
|
|
443
|
+
v2.Version = g.Version
|
|
444
|
+
pass #endfor
|
|
445
|
+
pass #endfor
|
|
446
|
+
return v2s
|
|
447
|
+
|
|
448
|
+
async def EditV2User(bus : MessageBus, user: V2User):
|
|
449
|
+
'''edit v2 user'''
|
|
450
|
+
print("EditV2User")
|
|
451
|
+
existingUser = await ReadV2UserBySecurityName(user.SecName)
|
|
452
|
+
|
|
453
|
+
if not existingUser:
|
|
454
|
+
print("USER NOT FOUND")
|
|
455
|
+
sys.exit()
|
|
456
|
+
|
|
457
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
458
|
+
await DeleteV2User(existingUser)
|
|
459
|
+
await _writeV2User(user)
|
|
460
|
+
await systemd_start(bus, 'snmpd.service')
|
|
461
|
+
|
|
462
|
+
async def DeleteV2User(bus : MessageBus, user: V2User):
|
|
463
|
+
'''delete v2 user'''
|
|
464
|
+
print('delete v2')
|
|
465
|
+
|
|
466
|
+
await systemd_stop(bus, 'snmpd.service')
|
|
467
|
+
|
|
468
|
+
_user = [ user.SecName, user.Source, user.Community]
|
|
469
|
+
_group = [ user.Permissions, user.Version, user.SecName]
|
|
470
|
+
|
|
471
|
+
async with aiofiles.open(snmp_config_file, "r") as f:
|
|
472
|
+
content = await f.readlines()
|
|
473
|
+
|
|
474
|
+
for line in content:
|
|
475
|
+
if line.startswith("com2sec") and all(p in line for p in _user):
|
|
476
|
+
content.remove(line)
|
|
477
|
+
if line.startswith("group") and all(p in line for p in _group):
|
|
478
|
+
content.remove(line)
|
|
479
|
+
|
|
480
|
+
async with aiofiles.open(snmp_config_file, "w") as f:
|
|
481
|
+
await f.writelines(content)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
await systemd_start(bus, 'snmpd.service')
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
# ====================================================================
|
|
488
|
+
# dbus
|
|
489
|
+
# ====================================================================
|
|
490
|
+
|
|
491
|
+
async def GetSnmp(bus: MessageBus):
|
|
492
|
+
introspection = await bus.introspect('com.novus.ns', '/com/novus/ns')
|
|
493
|
+
obj = bus.get_proxy_object('com.novus.ns', '/com/novus/ns', introspection)
|
|
494
|
+
return obj.get_interface('com.novus.ns.snmp')
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
async def snmp_call(bus: MessageBus, member: str, signature:str, body):
|
|
498
|
+
|
|
499
|
+
rsp = await bus.call(
|
|
500
|
+
Message(
|
|
501
|
+
destination='com.novus.ns',
|
|
502
|
+
path='/com/novus/ns',
|
|
503
|
+
interface='com.novus.ns.snmp',
|
|
504
|
+
member=member,
|
|
505
|
+
signature=signature,
|
|
506
|
+
body=[body]
|
|
507
|
+
)
|
|
508
|
+
)
|
|
509
|
+
|
|
510
|
+
if rsp.body:
|
|
511
|
+
return rsp.body[0]
|
ns2/lib/socket.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
from dataclasses import dataclass, asdict
|
|
3
|
+
import asyncio
|
|
4
|
+
from nicegui import Event, app
|
|
5
|
+
|
|
6
|
+
from dbus_next.service import ServiceInterface, method, signal
|
|
7
|
+
from dbus_next.aio import MessageBus
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
socket_receive = Event()
|
|
11
|
+
async def socket_stream():
|
|
12
|
+
try:
|
|
13
|
+
reader, writer = await asyncio.open_unix_connection("/tmp/serial.sock")
|
|
14
|
+
print("SOCKET OPENED")
|
|
15
|
+
while True:
|
|
16
|
+
line = (await reader.readline()).decode('utf-8', errors='ignore')
|
|
17
|
+
if line:
|
|
18
|
+
#yield line
|
|
19
|
+
socket_receive.emit(line)
|
|
20
|
+
record_line(line)
|
|
21
|
+
else:
|
|
22
|
+
break
|
|
23
|
+
except FileNotFoundError:
|
|
24
|
+
print("SOCKET NOT AVAILABLE")
|
|
25
|
+
#self.socket_received.emit("Socket Not Available")
|
|
26
|
+
raise
|
|
27
|
+
except asyncio.CancelledError:
|
|
28
|
+
print("SOCKET LISTENER CANCELLED")
|
|
29
|
+
if writer:
|
|
30
|
+
writer.close()
|
|
31
|
+
await writer.wait_closed()
|
|
32
|
+
raise
|
|
33
|
+
finally:
|
|
34
|
+
print("SOCKET LISTENER CLOSED")
|
|
35
|
+
if writer:
|
|
36
|
+
writer.close()
|
|
37
|
+
await writer.wait_closed()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def sendCommands(commands: dict, get_responses :bool = False) -> list[str]:
|
|
42
|
+
rx = asyncio.Event()
|
|
43
|
+
reader, writer = await asyncio.open_unix_connection("/tmp/serial.sock")
|
|
44
|
+
responses = {}
|
|
45
|
+
for name, command in commands.items():
|
|
46
|
+
print("sending: ", command)
|
|
47
|
+
command = command+"\r\n"
|
|
48
|
+
writer.write(command.encode())
|
|
49
|
+
await writer.drain()
|
|
50
|
+
#print("finsihed drain")
|
|
51
|
+
if get_responses:
|
|
52
|
+
try:
|
|
53
|
+
#await asyncio.wait_for(rx, 2.0)
|
|
54
|
+
|
|
55
|
+
while True:
|
|
56
|
+
#print("awaiting for response")
|
|
57
|
+
line = (await reader.readline()).decode('utf-8', errors='ignore')
|
|
58
|
+
if line:
|
|
59
|
+
if any(line.startswith(marker) for marker in ["$ER", "$RR", "$WR", "$GPNTL", "$BAUD"]):
|
|
60
|
+
print(f'got: {line}')
|
|
61
|
+
responses[name] = ParseNtlResponse(line)
|
|
62
|
+
#responses.append(line)
|
|
63
|
+
break
|
|
64
|
+
#return line
|
|
65
|
+
#rx.set()
|
|
66
|
+
|
|
67
|
+
#await rx.wait()
|
|
68
|
+
|
|
69
|
+
except TimeoutError:
|
|
70
|
+
responses[name] = "TimeoutError: no response?"
|
|
71
|
+
|
|
72
|
+
writer.close()
|
|
73
|
+
await writer.wait_closed()
|
|
74
|
+
|
|
75
|
+
return responses
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def record_line(line):
|
|
80
|
+
with open("data.txt", "a") as f:
|
|
81
|
+
f.writelines(line)
|
|
82
|
+
|
|
83
|
+
with open("data.txt", "r+") as f:
|
|
84
|
+
lines = f.readlines()
|
|
85
|
+
n = len(lines)
|
|
86
|
+
if n >= 10000:
|
|
87
|
+
lines = lines[n-10000:]
|
|
88
|
+
f.seek(0) # go to start of file
|
|
89
|
+
f.truncate()
|
|
90
|
+
f.writelines(lines)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
async def ReadNtlProperties(module :int, props: dict):
|
|
96
|
+
cmds = {}
|
|
97
|
+
|
|
98
|
+
for propName, propInt in props.items():
|
|
99
|
+
cmds[propName] = f"$GPNTL,{module},{propInt},?"
|
|
100
|
+
|
|
101
|
+
return await sendCommands(cmds, True)
|
|
102
|
+
#for r in rsp:
|
|
103
|
+
# a["module"] = ParseNtlResponse(r)
|
|
104
|
+
#return a
|
|
105
|
+
|
|
106
|
+
async def ReadNtlProperty(module: int, property :int) -> list[str]:
|
|
107
|
+
rsp = await sendCommands([f"$GPNTL,{module},{property},?"], True)
|
|
108
|
+
|
|
109
|
+
return ParseNtlResponse(rsp)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
async def WriteNtlProperty(module: int, property :int, value :str):
|
|
113
|
+
return await sendCommands([f"$GPNTL,{module},{property},{value}"], True)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def ParseNtlResponse(response :str)->str:
|
|
118
|
+
fields = response.split(",")
|
|
119
|
+
if len(fields) == 4:
|
|
120
|
+
module = fields[1]
|
|
121
|
+
property = fields[2]
|
|
122
|
+
value = fields[3]
|
|
123
|
+
return value.strip('\r\n')
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
async def WriteConfig(content :str):
|
|
127
|
+
commands = {i: line for i, line in enumerate(content.splitlines()) if line.startswith('$WC')}
|
|
128
|
+
await sendCommands(commands)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
ns2/lib/socket_client.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
'''import asyncio
|
|
4
|
+
#sock = Socket()
|
|
5
|
+
async def test():
|
|
6
|
+
await sock.setup()
|
|
7
|
+
|
|
8
|
+
print(await sock.writeRead("$BAUDNV"))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
#asyncio.run(test())
|
|
13
|
+
|
|
14
|
+
#sock.cleanup()
|
|
15
|
+
'''
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
from pprint import pprint
|
|
20
|
+
from dbus_next.aio import MessageBus
|
|
21
|
+
|
|
22
|
+
import asyncio
|
|
23
|
+
import time
|
|
24
|
+
from dbus_next.constants import BusType
|
|
25
|
+
|
|
26
|
+
from src.ns.lib.socket import *
|
|
27
|
+
|
|
28
|
+
from ntl import NtpServerProperties
|
|
29
|
+
|
|
30
|
+
async def test_snmp_client():
|
|
31
|
+
|
|
32
|
+
#with open('configs/PtpGmNtpServer.ucm') as f:
|
|
33
|
+
# content = f.read()
|
|
34
|
+
#await WriteConfig(content)
|
|
35
|
+
|
|
36
|
+
cmds = {}
|
|
37
|
+
|
|
38
|
+
for propName, i in NtpServerProperties.items():
|
|
39
|
+
cmds[propName] = f'$GPNTL,22,{i},?'
|
|
40
|
+
|
|
41
|
+
#for i in range(len(NtpServerProperties)):
|
|
42
|
+
# cmds[str(i)] = f'$BAUDNV'
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
start = time.time_ns()
|
|
46
|
+
print(await sendCommands(cmds, True))
|
|
47
|
+
print((time.time_ns()-start)/(10**9))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
#def rx_callback(msg):
|
|
51
|
+
# print(msg)
|
|
52
|
+
|
|
53
|
+
#sock.on_rx(rx_callback)
|
|
54
|
+
|
|
55
|
+
#while True:
|
|
56
|
+
# await asyncio.sleep(0.1)
|
|
57
|
+
|
|
58
|
+
#await asyncio.wait()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
asyncio.run(test_snmp_client())
|