summaryrefslogtreecommitdiff
path: root/kpov_judge/tasks/nat_port_forward
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2018-10-07 20:21:31 +0200
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2018-10-07 20:21:31 +0200
commit80f050080ce11c6a0cce67c9b0281c09702b00d6 (patch)
treeb486e1f6342bc757dda87df4856084f652895791 /kpov_judge/tasks/nat_port_forward
parent43d7ef057b4ae752a60b947e523b3a56782bf5cd (diff)
parentdbf585c2a59dbcb7f6d803d5756826b562cd7f43 (diff)
Merge branch 'fix-tasks'
Fixes pexpect usage in most tasks. Several other cleanups and minor refactoring.
Diffstat (limited to 'kpov_judge/tasks/nat_port_forward')
-rw-r--r--kpov_judge/tasks/nat_port_forward/task.py48
1 files changed, 11 insertions, 37 deletions
diff --git a/kpov_judge/tasks/nat_port_forward/task.py b/kpov_judge/tasks/nat_port_forward/task.py
index 2688268..8fe28d7 100644
--- a/kpov_judge/tasks/nat_port_forward/task.py
+++ b/kpov_judge/tasks/nat_port_forward/task.py
@@ -1,7 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-# kpov_random_helpers should be imported by add_assignment.py
+# kpov_util should be imported by add_assignment.py
instructions = {
'si':"""
@@ -19,10 +16,6 @@ computers = {
'disks': [
{ 'name': 'maliNetworkManager',
},
- #{ 'name': 'CDROM',
- # 'options':{'readonly': True},
- # 'parts': [],# no parts, no mounting.
- #}
],
'network_interfaces': [{'network': 'test-net'}],
'flavor': 'm1.tiny',
@@ -33,10 +26,6 @@ computers = {
'disks': [
{ 'name': 'student-NATServer',
},
- #{ 'name': 'CDROM',
- # 'options':{'readonly': True},
- # 'parts': [],# no parts, no mounting.
- #}
],
'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}],
'flavor': 'm1.tiny',
@@ -46,12 +35,7 @@ computers = {
'SimpleArbiter': {
'disks': [
{ 'name': 'simpleArbiter',
- # attempt automount
},
- #{ 'name': 'CDROM',
- # 'options': {'readonly': True},
- # 'parts': [{'dev': 'b1', 'path': '/cdrom'}],
- #},
],
'network_interfaces': [{'network': 'net1'}],
'flavor': 'm1.tiny',
@@ -70,18 +54,15 @@ params_meta = {
}
def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET):
- import pexpect
- from pexpect import pxssh
- import re
import random
import time
- # return None
- peer_user = 'student'
- peer_password = 'vaje'
+ import pexpect
+ from pexpect import pxssh
+
results = dict()
- tcpdump = pexpect.spawn('sudo /usr/sbin/tcpdump src host {} and dst port {}'.format(IP_TEST_CLIENT, PORT_INNER))
- sshconn = pxssh.pxssh()
- sshconn.login(IP_TEST_CLIENT, peer_user, peer_password)
+ tcpdump = pexpect.spawn('sudo /usr/sbin/tcpdump src host {} and dst port {}'.format(IP_TEST_CLIENT, PORT_INNER), encoding='utf-8')
+ sshconn = pxssh.pxssh(encoding='utf-8')
+ sshconn.login(IP_TEST_CLIENT, 'student', 'vaje')
r = random.Random()
k = r.randint(10, 15)
results['pre_nc'] = str(k)
@@ -92,7 +73,7 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET):
sshconn.sendintr()
sshconn.prompt()
results['nc_pre'] += sshconn.before
- nc = pexpect.spawn('nc -l -p {}'.format(PORT_INNER))
+ nc = pexpect.spawn('nc -l -p {}'.format(PORT_INNER), encoding='utf-8')
sshconn.sendline()
sshconn.prompt()
sshconn.sendline("nc {} {}".format(IP_NAT, PORT_OUTER))
@@ -102,8 +83,8 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET):
sshconn.sendintr()
nc.expect(pexpect.EOF)
results['nc_ret'] = nc.before
- results['route'] = pexpect.run('/sbin/ip route list 0/0')
- results['traceroute'] = pexpect.run('traceroute {}'.format(IP_TEST_CLIENT))
+ results['route'] = pexpect.run('ip route list 0/0', encoding='utf-8')
+ results['traceroute'] = pexpect.run('traceroute {}'.format(IP_TEST_CLIENT), encoding='utf-8')
# wait for traceroute
time.sleep(10)
tcpdump.sendintr()
@@ -118,21 +99,14 @@ def task(IP_TEST_CLIENT, IP_NAT, PORT_OUTER, PORT_INNER, NET):
def gen_params(user_id, params_meta):
params = dict()
r = random.Random(user_id)
- # IP_NM, DNS_NM, IP_static, DNS_static)
params['PORT_INNER'] = str(r.randint(6000, 10000))
params['PORT_OUTER'] = str(r.randint(10001, 15000))
- params['NET'] = kpov_random_helpers.IPv4_subnet_gen(r, "10.36.0.0/14", 24)
+ params['NET'] = kpov_util.IPv4_subnet_gen(r, "10.36.0.0/14", 24)
return params
def task_check(results, params):
import re
import pickle
- #if results is None:
- # with open('bla.pickle') as f:
- # results = cPickle.load(f)
- #else:
- # with open('bla.pickle', 'w') as f:
- # cPickle.dump(results, f)
score = 0
hints = []
local_net = params['NET'][:params['NET'].rfind('.')]