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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# kpov_util should be imported by add_assignment.py
# TODO: finish this.
instructions = {
'si':"""
Naloga: Postavi tri navidezne racunalnike:
- SimpleArbiter s sliko diska simpleArbiterDhcpGW,
- SNMPServer in
- SNMPClient.
Napiši program upminutes, ki bo izpisal v minutah koliko casa je racunalnik
vklopljen.
Postavi ga na SNMPClient v domaci imenik uporabnika test z geslom test.
Poskrbi, da bo SNMP strežnik prek SNMP pod
OID {SNMP_UPTIME_OID} sporočal, koliko casa je vklopljen
v sekundah.
Napiši skripto, poimenovano beri.sh, ki prek SNMP prebere vrednost s
simpleArbiterDhcpGWSNMP na OID {SNMP_CLIENT_OID} kot član skupnosti
"testers".
Postavi jo na SNMP klienta, v domaci imenik uporabnika test z geslom test.
Poskrbi, da bodo podatki na SNMPServer dostopni za skupino (community) studentje.
""",
'en':"""
Set up three virtual computers, SimpleArbiter with the disk image simpleArbiterDhcp,
SNMPServer and SNMPClient. Write a program called upminutes. This program should output
the uptime of the computer in minutes. Set it up on SNMPClient in the home directory
of the user test with the password test.
Make sure that the SNMP server reports it's uptime in seconds over SNMP under
OID {SNMP_UPTIME_OID}.
Write a script called beri.sh that reads the value from the OID
{SNMP_CLIENT_OID} on simpleArbiterDhcpGWSNMP as a member of the
community "testers".
Set it up on SNMPClient in the home directory of the user test.
Make all the data available over SNMP readable by the "studentje" community.
"""
}
computers = {
'SNMPClient': {
'disks': [
{ 'name': 'student-SNMPClient',
},
#{ 'name': 'CDROM',
# 'options':{'readonly': True},
# 'parts': [],# no parts, no mounting.
#}
],
'network_interfaces': [{'network': 'net1'}],
'flavor': 'm1.tiny',
'config_drive': False
},
'SNMPServer': {
'disks': [
{ 'name': 'student-SNMPServer',
},
#{ 'name': 'CDROM',
# 'options':{'readonly': True},
# 'parts': [],# no parts, no mounting.
#}
],
'network_interfaces': [{'network': 'net1'}],
'flavor': 'm1.tiny',
'config_drive': False
},
'SimpleArbiter': {
'disks': [
{ 'name': 'simpleArbiterDhcpGWSNMP',
# attempt automount
},
#{ 'name': 'CDROM',
# 'options': {'readonly': True},
# 'parts': [{'dev': 'b1', 'path': '/cdrom'}],
#},
],
'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}],
'flavor': 'm1.tiny',
'config_drive': False
}
}
networks = { 'net1': {'public': False}, 'test-net': {'public': True} }
params_meta = {
'SNMP_VALUE': {'descriptions': {'si': 'Vrednost, dostopna prek SNMP', 'en': 'The value available over SNMP'}, 'w': False, 'public':False, 'type': 'short', 'generated': True},
'SNMP_UPTIME_OID': {'descriptions': {'si': 'SNMP_UPTIME_OID (za uptime)', 'en': 'SNMP_UPTIME_OID (for the uptime)'}, 'w': False, 'public':True, 'type': 'str', 'generated': True},
'SNMP_CLIENT_OID': {'descriptions': {'si': 'SNMP_CLIENT_OID, ki naj ga klient bere', 'en':'The OID that the client should read'}, 'w': False, 'public':True, 'type': 'OID', 'generated': True},
'SERVER_IP': {'descriptions': {'si': 'IP SNMP strežnika', 'en':'IP of the SNMP server'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False},
'CLIENT_IP': {'descriptions': {'si': 'IP SNMP klienta', 'en': 'IP of the SNMP client'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False},
}
def task(SERVER_IP, CLIENT_IP, SNMP_UPTIME_OID, SNMP_CLIENT_OID):
#<== Aleksander Fujs 6310020 ==>
# TODO popravi IPje
import netsnmp
import paramiko
from paramiko import SSHClient
return_results = {}
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(SERVER_IP, username='student', password='vaje')
stdin, stdout, stderr = client.exec_command('uptime=$(</proc/uptime); uptime=${uptime%%.*}; echo $uptime')
return_results['server_uptime'] = stdout.read()
try:
session = netsnmp.Session(DestHost=SERVER_IP, Version=2, Community='studentje')
# results_objs = netsnmp.VarList(netsnmp.Varbind(SNMP_UPTIME_OID)) #117.112.116.105.109.101 <-uptime
results_objs = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.4.1.8072.1.3.2.4.1.2')) #117.112.116.105.109.101 <-uptime
session.walk(results_objs)
result_list = []
for result in results_objs:
result_list.append('{}.{}: {}'.format(
result.tag, result.iid, result.val))
return_results['server_OID'] = "\n".join(result_list)
except Exception as exception_error:
# Check for errors and print out results
print(('ERROR: Occurred during SNMPget for OID %s from %s: '
'(%s)') % (SNMP_UPTIME_OID, CLIENT_IP, exception_error))
sys.exit(2)
client.connect(CLIENT_IP, username='test', password='test')
stdin, stdout, stderr = client.exec_command('uptime=$(</proc/uptime); uptime=${uptime%%.*}; echo $(( uptime ))')
return_results['client_uptime'] = stdout.read()
# zakaj bi morala biti ravno bash skripta?
stdin, stdout, stderr = client.exec_command('/home/test/upminutes')
#TODO preverit da ni v skripti hardcodan
return_results['client_script'] = stdout.read()
stdin, stdout, stderr = client.exec_command('/home/test/beri.sh')
return_results['client_script2'] = stdout.read()
#TODO add 3 part of assigement
return return_results
#<== Aleksander Fujs 6310020 ==>
def gen_params(user_id, params_meta):
import random
params = dict()
r = random.Random(user_id)
# You can also create an OID creation function in kpov_util.
# this should probably return params_meta
#<== Aleksander Fujs 6310020 ==>
params['SNMP_VALUE'] = kpov_util.alnum_gen(r, 64)
params['SNMP_UPTIME_OID'] = 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."{}".1'.format(
kpov_util.hostname_gen(r))
params['SNMP_CLIENT_OID'] = '1.3.6.1.4.1.8072.2.9999.9999.{}'.format(
r.randint(0, 255))
#<== Aleksander Fujs 6310020 ==>
return params
def task_check(results, params):
#TODO improve regex
import re
score = 0
hints = []
client_script_uptime = int(results['client_script'].strip())
client_uptime = int(results['client_uptime'].strip())
d = client_uptime - client_script_uptime*60
if d >= 0 and d < 62:
score += 3
else:
hints += ["client uptime script output wrong."]
server_uptime = int(results['server_uptime'].strip())
lines = results['server_OID'].split('\n')
unique_part_start = params['SNMP_UPTIME_OID'].find('"')
unique_part_end = params['SNMP_UPTIME_OID'].find('"', unique_part_start+1)
unique_part = params['SNMP_UPTIME_OID'][unique_part_start+1:unique_part_end]
server_oid = 'iso.3.6.1.4.1.8072.1.3.2.4.1.2.{}.'.format(len(unique_part))
server_oid += '.'.join([str(ord(i)) for i in unique_part]) + '.1.'
found_uptime = False
for line in lines:
try:
oid, uptime_s = line.split(':')
d = server_uptime - int(uptime_s.strip())
if oid.strip() == server_oid and d >= -2 and d <= 5:
found_uptime = True
break
except Exception as e:
pass
if len(lines) <= 50 and found_uptime:
score += 3
else:
hints += ["Uptime not found in server's MDB"]
if results['client_script2'].find(params['SNMP_VALUE']) >= 0:
score += 4
else:
hints += ["beri.sh not working properly"]
return score, hints
def prepare_disks(templates, task_params, global_params):
# d = templates['simpleArbiterDhcp']
prog = """#!/usr/bin/python
import sys
action = sys.argv[1]
oid = sys.argv[2]
foo_oid = ".{oid}"
foo_oid_start = foo_oid[:foo_oid.rfind('.')]
foo_oid_end = foo_oid[len(foo_oid_start)+1:]
oid_end = oid[len(foo_oid_start)+1:]
oid_start = oid[:len(foo_oid_start)]
if action == '-n' and (
(oid_start == foo_oid_start) and (
(len(oid_end) == 0) or (int(oid_end) < int(foo_oid_end))
)
):
oid = foo_oid
if action != '-s' and oid == foo_oid:
print foo_oid
print "string"
print "{val}"
""".format(oid = task_params['SNMP_CLIENT_OID'], val = task_params['SNMP_VALUE'])
templates['simpleArbiterDhcpGWSNMP'].write('/usr/local/bin/snmpext.py', prog)
templates['simpleArbiterDhcpGWSNMP'].chmod(0o755, '/usr/local/bin/snmpext.py')
write_default_config(templates['simpleArbiterDhcpGWSNMP'], global_params)
|