From e33085889f26c76587fdab2ad600ed336dc028b5 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:35:00 +0200 Subject: Improve ssh error reporting in some tasks --- kpov_judge/tasks/rename_grep_network/task.py | 74 ++++++++++------------------ 1 file changed, 26 insertions(+), 48 deletions(-) (limited to 'kpov_judge/tasks/rename_grep_network') diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index e8ce0b2..948edcf 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -139,26 +139,6 @@ params_meta = { 'generated': True, }, - # 'progname': { - # 'descriptions': { - # 'si': 'Ime programa', - # 'en': 'Program filename', - # }, - # 'w': False, - # 'public': True, - # 'type': 'filename', - # 'generated': True, - # }, - 'pubseed': { - 'descriptions': { - 'si': 'Nekaj nepredvidenega', - 'en': 'A random seed', - }, - 'w': False, - 'public': True, - 'type': None, - 'generated': True, - }, 'rndseed': { 'descriptions': { 'si': 'random seed za skrito datoteko', @@ -171,30 +151,30 @@ params_meta = { }, } -def task(testip, testuser, passwd, mntdir, magicstr, pubseed): +def task(testip, testuser, passwd, mntdir, magicstr): + import collections from pexpect import pxssh - import random - target = pxssh.pxssh() - target.login(testip, testuser, passwd) - target.sendline('ls ~/') - target.prompt() - home_ls = target.before - target.sendline('cat ~/*I*.txt') - target.prompt() - dst_file_contents = target.before - target.sendline('ls -l ~/*I*.txt') - target.prompt() - dst_ls = target.before - target.sendline('mount') - target.prompt() - mnt = target.before - results = { - 'home_ls': home_ls, - 'dst_ls': dst_ls, - 'mnt': mnt, - 'dst_file_contents': dst_file_contents, - } - target.logout() + + commands = [ + ('home_ls', 'ls ~/'), + ('dst_file_contents', 'cat ~/*I*.txt'), + ('dst_ls', 'ls -l ~/*I*.txt'), + ('mnt', 'mount'), + ] + + results = collections.defaultdict(str) + try: + s = pxssh.pxssh(encoding='utf-8') + s.login(testip, testuser, passwd) + results['ssh'] = True + for test, command in commands: + s.sendline(command) + s.prompt() + if test: + results[test] = s.before + s.logout() + except Exception as ex: + results['ssh'] = str(ex) return results def gen_params(user_id, params_meta): @@ -207,10 +187,6 @@ def gen_params(user_id, params_meta): params['passwd'] = kpov_random_helpers.alnum_gen(r, 8) params['magicstr'] = "".join([r.choice("qwerztlpoQWERTPOL") for i in range(10)]) params['mntdir'] = "/" + kpov_random_helpers.default_generators['filename'](r) - # params['progname'] = kpov_random_helpers.default_generators['filename'](r) - # while params['dstfile'] == params['progname']: - # params['progname'] = kpov_random_helpers.default_generators['filename'](r) - params['pubseed'] = kpov_random_helpers.alnum_gen(r, 8) params['rndseed'] = kpov_random_helpers.alnum_gen(r, 8) return params @@ -220,6 +196,8 @@ def task_check(results, params): import re hints = [] score = 0 + if results['ssh'] is not True: + hints += ['ssh failed: ' + results['ssh']] hidden_contents = params['magicstr'] r = random.Random(params['rndseed']) dstfile = "".join([r.choice("qQoOp") for i in range(64)]) + "I.txt" @@ -242,7 +220,7 @@ def task_check(results, params): if results["dst_file_contents"] == expected_contents: score += 3 else: - diff_pos = "" + diff_pos = (0, "") for i, c in enumerate(results["dst_file_contents"]): if len(expected_contents) < i or c != expected_contents[i]: start = max(0, i-10) -- cgit v1.2.1 From 9c51bbe5016495c714789f5df01806a6d366e11a Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:56:03 +0200 Subject: Rename kpov_random_helpers to kpov_util --- kpov_judge/tasks/rename_grep_network/task.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'kpov_judge/tasks/rename_grep_network') diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index 948edcf..cba9ac9 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -181,13 +181,13 @@ def gen_params(user_id, params_meta): import random params = dict() r = random.Random(user_id) - params['testip'] = kpov_random_helpers.IPv4_addr_gen(r, + params['testip'] = kpov_util.IPv4_addr_gen(r, network = '10.94.80.0/19', n_generated=1)[0] - params['testuser'] = kpov_random_helpers.default_generators['username'](r) - params['passwd'] = kpov_random_helpers.alnum_gen(r, 8) + params['testuser'] = kpov_util.default_generators['username'](r) + params['passwd'] = kpov_util.alnum_gen(r, 8) params['magicstr'] = "".join([r.choice("qwerztlpoQWERTPOL") for i in range(10)]) - params['mntdir'] = "/" + kpov_random_helpers.default_generators['filename'](r) - params['rndseed'] = kpov_random_helpers.alnum_gen(r, 8) + params['mntdir'] = "/" + kpov_util.default_generators['filename'](r) + params['rndseed'] = kpov_util.alnum_gen(r, 8) return params -- cgit v1.2.1 From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- kpov_judge/tasks/rename_grep_network/task.py | 37 ++++++---------------------- 1 file changed, 7 insertions(+), 30 deletions(-) (limited to 'kpov_judge/tasks/rename_grep_network') diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index cba9ac9..c1be010 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -152,30 +152,12 @@ params_meta = { } def task(testip, testuser, passwd, mntdir, magicstr): - import collections - from pexpect import pxssh - - commands = [ + return kpov_util.ssh_test(testip, testuser, passwd, ( ('home_ls', 'ls ~/'), ('dst_file_contents', 'cat ~/*I*.txt'), ('dst_ls', 'ls -l ~/*I*.txt'), ('mnt', 'mount'), - ] - - results = collections.defaultdict(str) - try: - s = pxssh.pxssh(encoding='utf-8') - s.login(testip, testuser, passwd) - results['ssh'] = True - for test, command in commands: - s.sendline(command) - s.prompt() - if test: - results[test] = s.before - s.logout() - except Exception as ex: - results['ssh'] = str(ex) - return results + )) def gen_params(user_id, params_meta): import random @@ -216,7 +198,6 @@ def task_check(results, params): # r"pokakalz\r", # hidden_contents, # re.MULTILINE) - expected_contents = "cat ~/*I*.txt\r\n".format(dstfile) + expected_contents if results["dst_file_contents"] == expected_contents: score += 3 else: @@ -228,9 +209,8 @@ def task_check(results, params): diff_pos = (i, results["dst_file_contents"][start:end]) break hints += ["wrong file contents\n" + str(diff_pos[1])] - #hints += ["wrong file contents"] params['dstfile'] = dstfile - expected_dst_ls = "ls -l ~/\\*I\\*.txt\r\n-rw--w---- 1 {testuser} bilbo .*{dstfile}\r\n".format(**params) + expected_dst_ls = "-rw--w---- 1 {testuser} bilbo .*{dstfile}".format(**params) if re.match(expected_dst_ls, results["dst_ls"]): score += 2 else: @@ -253,10 +233,7 @@ def task_check(results, params): def prepare_disks(templates, task_params, global_params): import random import os - #print "Haha!" - #print params - #print templates - d = templates['smallstudent-personal'] + # first create the file contents to make it easyer to check. hidden_contents = task_params['magicstr'] r = random.Random(task_params['rndseed']) @@ -266,6 +243,7 @@ def prepare_disks(templates, task_params, global_params): x += "".join([r.choice("uiasdfghjkyxcvbnm1234567890ASDFGHJKYZXCVBNM") for i in range(60)]) x += r.choice(["lz", "1z", "Iz", "iz", "l2", "I2", "12"]) hidden_contents += x + "\n" + # create hidden file dir_list = ['Qlipper', 'Thunar', 'blender', 'autostart', 'kazam', 'mc', 'netsurf', 'pulse', 'qupzilla', 'radare2', 'teamviewer', 'texstudio', 'vlc'] ending_list = ['rc', '.conf', '', '.txt'] @@ -274,6 +252,8 @@ def prepare_disks(templates, task_params, global_params): start_list.append("".join([r.choice("qQoOp") for i in range(64)]) + "O") r.shuffle(dir_list) file_letters = ["mod", "co"] + + d = templates['smallstudent-personal'] d.mkdir('/mnt/.hideme') d.mkdir('/media/.hideme') for potential_dir in dir_list: @@ -304,6 +284,3 @@ def prepare_disks(templates, task_params, global_params): # TODO create some additional files # write_default_config(templates['simpleArbiterDhcpGW'], global_params) - # finish here - # rename - -- cgit v1.2.1 From 3091fc2877600f7926ed6a596c316183499125f8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:11:49 +0200 Subject: Remove coding: lines Python 3 source is utf-8 by default. --- kpov_judge/tasks/rename_grep_network/task.py | 1 - 1 file changed, 1 deletion(-) (limited to 'kpov_judge/tasks/rename_grep_network') diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index c1be010..aa61e8a 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # TODO: # - check if everything is filled in (computers, params, preparation) -- cgit v1.2.1 From dbf585c2a59dbcb7f6d803d5756826b562cd7f43 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:38:40 +0200 Subject: Fix or remove hashbangs --- kpov_judge/tasks/rename_grep_network/task.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'kpov_judge/tasks/rename_grep_network') diff --git a/kpov_judge/tasks/rename_grep_network/task.py b/kpov_judge/tasks/rename_grep_network/task.py index aa61e8a..4f8f1ad 100644 --- a/kpov_judge/tasks/rename_grep_network/task.py +++ b/kpov_judge/tasks/rename_grep_network/task.py @@ -1,12 +1,9 @@ -#!/usr/bin/env python -# # TODO: # - check if everything is filled in (computers, params, preparation) # - improve scoring # - test # - switch to a real SSH/SFTP client to properly handle filenames - instructions = { 'si': """ Ustvari dva navidezna računalnika s slikama diskov -- cgit v1.2.1