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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# SPDX-License-Identifier: AGPL-3.0-or-later
# kpov_util should be imported by add_assignment.py
instructions = {
'si': '''\
<p>
Prijavi se na sistem kot uporabnik <code>student</code> z geslom <code>vaje</code>.
<p>
V domači mapi najdeš imenik <code>{{file_rename_dirname}}</code> z 20 datotekami. Preimenuj vse datoteke tako, da zamenjaš minuse s podčrtaji.
<p>
Napiši najkrajši ukaz v <code>bash</code>, ki vse datoteke iz imenika <code>{{mv_src_dir}}</code> premakne v imenik <code>{{mv_dst_dir}}</code>. Ukaz spravi v <code>/home/student/mv_ukaz</code>. Pazite, da bodo ob testiranju v izvornem imeniku <em>iste</em> datoteke, kot so bile, ko je bila virtualka nova, ciljni imenik pa bo prazen. To pomeni, da boste verjetno morali po vsakem testiranju datoteke premakniti nazaj. Za krajšanje ukaza lahko izkoristite imeni imenikov.
<p>
Napiši ukaz, ki s pomočjo ukaza <code>grep</code> v datoteko <code>~/mama.txt</code> izpiše imena vseh navadnih (ne skritih) datotek v trenutnem imeniku, ki vsebujejo niz <code>mama</code>, v datoteko <code>~/napake.txt</code> pa izpiše vse morebitne napake (npr. to, da so nekateri objekti v trenutnem imeniku dejansko imeniki ali napačne simbolične povezave). Ukaz spravite v datoteko <code>/home/student/mama_ukaz</code>.
<p>
Napiši program (lahko v <code>bash</code>), ki bo pet sekund sledil vsebini <code>/var/log/syslog</code>. V primeru, da se v syslogu pojavi niz <code>zmeda</code>, naj program izpiše <code>imam ga</code>. Poleg tega naj program med sledenjem syslog-u odšteva od 5 do 1 (vsako sekundo naj se izpiše naslednja številka). Program naj bo v <code>/home/student/syslog_ukaz.sh</code>.
<p>
Namesti paket <code>cowsay</code> in ga preizkusi. 😊
<p>
S pomočjo programa <code>curl</code> preštej število pojavitev niza <code>images</code> v spletni strani, ki jo dobiš na naslovu <code>http://localhost/{{curl_fname}}</code> na <em>malishell</em> in število zapiši v spremenljivko <code>$images</code> na <em>simpleArbiterDhcpGW</em> v lupini, kjer poganjaš <code>task_check.py</code>.
<p>
V imeniku <code>{{wc_dirname}}</code> najdeš datoteko <code>count.txt</code>. Preštej število prehodov v novo vrstico v <code>count.txt</code> in rezultat zapiši v novo datoteko <code>lines.txt</code> v istem imeniku.
''',
'en': '''\
<p>
Log into the system as <code>student</code> using <code>vaje</code> as your password.
<p>
In your home folder you will find a directory called <code>{{file_rename_dirname}}</code> containing 20 files. Rename all files in that directory replacing all dashes with underscores
<p>
Find the shortest command for copying all the files located in <code>{{mv_src_dir}}</code> into the folder <code>{{mv_dst_dir}}</code>. Create a shell script <code>/home/student/mv_ukaz</code> containing this command. Before running <code>test_task.py</code>, make sure <code>{{mv_src_dir}}</code> contains the same files as when you downloaded your virtual disk. This probably means that you will have to move the files back from <code>{{mv_dst_dir}}</code> to <code>{{mv_src_dir}}</code> after each test.
<p>
Come up with a command or sequence of commands in bash which will, using <code>grep</code>, write the names all normal (not hidden) files which contain the string <code>mama</code> and are located in the working directory (<code>pwd</code>) into the file <code>~/mama.txt</code> while writing errors such as files in current directory being folders or symbolic links in the file called <code>napake.txt</code>. Write this command to <code>/home/student/mama_ukaz</code>.
<p>
Write a program (which may be a shell script) that will track the contents of <code>/var/log/syslog</code> for five seconds. Every time an entry containing the string <code>zmeda</code> appears in the syslog, the script should output <code>imam ga</code>. In addition, the script should count down from 5 to 1 with a one-second interval. Store the script in <code>/home/student/syslog_ukaz.sh</code>.
<p>
Install the package <code>cowsay</code> and test it. 😊
<p>
Using the <code>curl</code> command count the number of occurences of the string <code>images</code> on the web page accessible from <em>malishell</em> at <code>http://localhost/{{curl_fname}}</code>. On <em>simpleArbiterDhcpGW</em> within the shell where you are running <code>test_task.py</code>, set the environment variable <code>$images</code> to this number.
<p>
In the directory <code>{{wc_dirname}}</code> there is a file called <code>count.txt</code>. Write the number of newlines in this file into the file <code>lines.txt</code> in the same directory.
''',
}
computers = {
'malishell': {
'disks': [
{ 'name': 'malishell',
},
#{ 'name': 'CDROM',
# 'options':{'readonly': True},
# 'parts': [],# no parts, no mounting.
#}
],
'network_interfaces': [{'network': 'net1'}],
'flavor': 'm1.tiny',
'config_drive': False
},
'SimpleArbiterDhcpGW': {
'disks': [
{ 'name': 'simpleArbiterDhcpGW',
# attempt automount
},
#{ 'name': 'CDROM',
# 'options': {'readonly': True},
# 'parts': [{'dev': 'b1', 'path': '/cdrom'}],
#},
],
'network_interfaces': [{'network': 'test-net'}, {'network': 'net1'}],
'flavor': 'm1.tiny',
'config_drive': False
}
}
networks = {'net1': {'public': False}, 'test-net': {'public': True} }
params_meta = {
'IP_malishell': {'descriptions': {'si': 'Naslov malishell'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False},
'file_creator_random_seed': {'descriptions': {'si': 'random file creator seed'}, 'w': False, 'public':False, 'type': None, 'generated': True},
'file_rename_dirname': {'descriptions': {'si': 'imenik z datotekami, ki naj se jih preimenuje'}, 'w': False, 'public':True, 'type': 'dirname', 'generated': True},
'mv_src_dir': {'descriptions': {'si': 'imenik, iz katerega premakni datoteke'}, 'w': False, 'public':True, 'type': 'dirname', 'generated': True},
'mv_dst_dir': {'descriptions': {'si': 'imenik, v katerega premakni datoteke'}, 'w': False, 'public':True, 'type': 'dirname', 'generated': True},
'cowsay_string': {'descriptions': {'si': 'Kaj rece krava?'}, 'w': False, 'public':True, 'type': 'short', 'generated': True},
'syslog_n_zmeda': {'descriptions': {'si': 'st. zapisov zmeda v 5s'}, 'w': False, 'public':True, 'type': 'uint', 'generated': True},
'curl_fname': {'descriptions': {'si': 'ime datoteke, dostopne prek http'}, 'w': False, 'public':True, 'type': 'filename', 'generated': True},
'curl_n_images': {'descriptions': {'si': 'n pojavitev niza images'}, 'w': False, 'public':False, 'type': 'integer', 'generated': True},
'wc_dirname': {'descriptions': {'si': 'imenik, v katerem je datoteka count.txt'}, 'w': False, 'public':True, 'type': 'dirname', 'generated': True},
'wc_n_lines': {'descriptions': {'si': 'n vrstic v count.txt'}, 'w': False, 'public':False, 'type': 'integer', 'generated': True},
}
def task(IP_malishell, file_rename_dirname, mv_src_dir, mv_dst_dir, cowsay_string, curl_fname, wc_dirname):
import collections
import os
# TOD: (polz) this has to be changed! Get a move on!
#
# sv: Z primozem lavricem sva skusala nekaj narediti
# Ker gen params ni narejen, sklepam da je "Mapa" na namizju,
# imena datotek pa so: 1,1-,2,2-,3,3-,4,5,6,7,8,9,.mama1,mama2,mama3,mama4,mama5,mojimenik,novi,oce1
# v mojimenik se nahaja mojimenikfile
# mama2 vsebuje "mama"
#Stirje subt-aski dodani.By Mihec.
results = kpov_util.ssh_test(IP_malishell, 'student', 'vaje', (
('preimenuj', '/bin/ls -a1 {}'.format(file_rename_dirname)),
('pre_mv_src', '/bin/ls -a1 {}'.format(mv_src_dir)),
('pre_mv_dst', '/bin/ls -a1 {}'.format(mv_dst_dir)),
('mv_ls_size', '/bin/ls -l ~/mv_ukaz'),
(None, '. ~/mv_ukaz'),
('post_mv_src', '/bin/ls -a1 {}'.format(mv_src_dir)),
('post_mv_dst', '/bin/ls -a1 {}'.format(mv_dst_dir)),
# mama_ukaz
(None, 'cd /home/student/grep_test_dir'),
(None, '. ~/mama_ukaz'),
('grep_napake', 'cat ~/napake.txt'),
('grep_mama', 'cat ~/mama.txt'),
# sledenje syslog
(None, 'date'),
('syslog_start_time', '/usr/local/bin/syslog_spammer &'),
('syslog_result', '/home/student/syslog_ukaz.sh'),
('syslog_end_time', 'date'),
('cowsay', 'cowsay "{}"'.format(cowsay_string)),
('wc_origfile', 'cat {}/count.txt'.format(wc_dirname)),
('wc_lines', 'cat {}/lines.txt'.format(wc_dirname)),
))
try:
results['curl_env'] = os.environ['images']
except:
results['curl_env'] = ''
#results['curl'] = subprocess.check_output(["cat","/home/student/Desktop/website.txt"])
#results['chkimages'] = subprocess.check_output(["curl www.24ur.com >> dlg.txt && cat dlg.txt | grep -c ","images"])
#results['count'] = subprocess.check_output(["wc","/home/student/Desktop/count.txt"])
#results['lines'] = subprocess.check_output(["cat","/home/student/Desktop/lines.txt"])
#results['cowsay'] = subprocess.check_output(["dpkg --get-selections | grep","cowsay"])
return results
def gen_params(user_id, params_meta):
import random
import subprocess
params = dict()
homedir = '/home/student/'
r = random.Random(user_id)
params['file_creator_random_seed'] = str(r.random())
params['file_rename_dirname'] = homedir + kpov_util.default_generators['dirname'](r)
params['mv_src_dir'] = homedir + "".join([r.choice("abcdefgh") for i in range(6)])
params['mv_dst_dir'] = homedir + "".join([r.choice("ijklmnop") for i in range(6)])
params['syslog_n_zmeda'] = str(r.randint(5, 15))
params['cowsay_string'] = kpov_util.default_generators['short_text'](r)
params['curl_fname'] = kpov_util.default_generators['filename'](r)
params['curl_n_images'] = str(r.randint(30,100))
params['wc_dirname'] = homedir + "".join([r.choice("rstuvxz") for i in range(8)])
params['wc_n_lines'] = str(r.randint(200, 600))
#params['images'] = subprocess.check_output(["echo","$images"])
return params
def task_check(results, params):
import re
import random
score = 0
hints = []
r = random.Random(params['file_creator_random_seed'])
fnames = []
for i in range(20):
fnames.append("".join([ r.choice("_abcdefghijk") for j in range(8)]))
#TO FINISH SCORING WE REQUIRE DICT KEYS AND FUNCTIONS gen_params AND task TO BE FINISHED
# preimenuj 1
task1_ok = True
for fname in fnames:
task1_ok = task1_ok and results['preimenuj'].find(fname) > 0
if task1_ok:
score += 2
else:
hints += ["wrong dash rename"]
# premakni datoteke s cim krajsim ukazom
fnames = []
try:
for i in range(40):
fnames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)]))
task2_re = re.search(
r"-rw[x-][r-][w-][x-][r-][w-][x-] \d* student student (\d*) .*mv_ukaz",
results['mv_ls_size'])
mv_ls_size = int(task2_re.group(1))
task2_ok = mv_ls_size <= 17 and mv_ls_size > 2
except:
task2_ok = False
if task2_ok:
score += 1
else:
hints += ["wrong mv_command size or owner"]
# rename files
pre_src = set([i.strip() for i in results['pre_mv_src'].splitlines()[2:]])
pre_dst = set([i.strip() for i in results['pre_mv_dst'].splitlines()[2:]])
print(set(fnames))
print(pre_src)
task2_ok = task2_ok and pre_src == set(fnames)
task2_ok = task2_ok and len(pre_dst) < 3
post_src = set([i.strip() for i in results['post_mv_src'].splitlines()[2:]])
post_dst = set([i.strip() for i in results['post_mv_dst'].splitlines()[2:]])
task2_ok = task2_ok and post_dst == set(fnames)
task2_ok = task2_ok and len(post_src) < 3
if task2_ok:
score += 1
else:
hints += ["wrong rename files"]
# mama_ukaz
task3_ok = True
mama_fnames = []
for i in range(20):
mama_fnames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)]))
papa_fnames = []
for i in range(20):
papa_fnames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)]))
dirnames = []
for i in range(20):
dirnames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)]))
linknames = []
for i in range(20):
linknames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)]))
for i in dirnames + linknames + papa_fnames:
if (i not in papa_fnames) and results['grep_napake'].find(i) < 0:
task3_ok = False
if results['grep_mama'].find(i) >= 0:
task3_ok = False
for i in papa_fnames:
if results['grep_napake'].find(i) >= 0:
task3_ok = False
if results['grep_mama'].find(i) >= 0:
task3_ok = False
for i in mama_fnames:
if results['grep_mama'].find(i) < 0:
task3_ok = False
if task3_ok:
score += 2
else:
hints += ["mama cries"]
# sledenje syslog
#print( results['syslog_start_time'])
#print( results['syslog_result'])
#print( results['syslog_end_time'])
# syslog count
# TODO: check syslog
task4_ok = True
if task4_ok:
score += 1
else:
hints += ["wrong syslog count"]
task5_ok = True
try:
assert int(results['curl_env'].strip()) == int(params['curl_n_images'])
except:
task5_ok = False
if task5_ok:
score += 2
else:
hints += ["wrong image count"]
task6_ok = True
try:
assert int(results['wc_lines'].strip()) == int(params['wc_n_lines'])
except:
task6_ok = False
if task6_ok:
score += 1
else:
hints += ["wrong line count"]
return score, hints
def prepare_disks(templates, task_params, global_params):
import random
d = templates['malishell']
r = random.Random(task_params['file_creator_random_seed'])
# rename
d.mkdir(task_params['file_rename_dirname'])
d.chown(1000, 1000, task_params['file_rename_dirname'])
for i in range(20):
fname = task_params['file_rename_dirname'] + '/' + "".join(
[r.choice("-abcdefghijk") for j in range(8)])
d.touch(fname)
d.chown(1000, 1000, fname)
# mv ukaz
d.mkdir(task_params['mv_src_dir'])
d.chown(1000, 1000, task_params['mv_src_dir'])
for i in range(40):
fname = task_params['mv_src_dir'] + '/' +"".join(
[r.choice("123456789abcdefghijk") for j in range(8)])
d.touch(fname)
d.chown(1000, 1000, fname)
d.mkdir(task_params['mv_dst_dir'])
d.chown(1000, 1000, task_params['mv_dst_dir'])
# grep mama
# mama fnames
d.mkdir('/home/student/grep_test_dir')
for i in range(20):
d.write("/home/student/grep_test_dir/" + "".join(
[ r.choice("123456789abcdefghijk") for j in range(8)]),
"mama")
# papa fnames
for i in range(20):
d.write("/home/student/grep_test_dir/" + "".join(
[ r.choice("123456789abcdefghijk") for j in range(8)]),
"papa")
# dirnames
for i in range(20):
d.mkdir("/home/student/grep_test_dir/" + "".join(
[ r.choice("123456789abcdefghijk") for j in range(8)]))
# linknames
for i in range(20):
d.ln_sf("mali zeleni", "/home/student/grep_test_dir/" + "".join(
[ r.choice("123456789abcdefghijk") for j in range(8)]))
# sledenje syslog
spammer_source = "#!/bin/sh\n" + int(task_params['syslog_n_zmeda']) * "logger zmeda\n"
d.write("/usr/local/bin/syslog_spammer", spammer_source)
d.chmod(0o775, "/usr/local/bin/syslog_spammer")
# curl
l1 = ['images'] * int(task_params['curl_n_images'])
l2 = ['imeges'] * r.randint(30, 100)
lx = l1 + l2
r.shuffle(lx)
d.write("/var/www/html/{}".format(task_params['curl_fname']), ''.join(lx))
# wc
lx = ['\n']*int(task_params['wc_n_lines'])
lx += ['a', 'b', 'c', 'repa', 'in', 'krompir',
'raus', 'e', 'patacis'] * r.randint(50, 150)
r.shuffle(lx)
d.mkdir(task_params['wc_dirname'])
d.chown(1000, 1000, task_params['wc_dirname'])
d.write("{}".format(os.path.join(task_params['wc_dirname'], 'count.txt')), "".join(lx))
write_default_config(templates['simpleArbiterDhcpGW'], global_params)
|