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 19:02:26 +0200
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2018-10-07 19:04:21 +0200
commitb5b35faea7f4205f353f57178ddc795b7dce5043 (patch)
treef8531a61d236ea0abc4e9dc80692abdc36c6579a /kpov_judge/tasks/nat_port_forward
parent42076466e00aa066769050bb2e2b4d98e9cc4d20 (diff)
Refactor SSH testing code out of individual tasks
Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes.
Diffstat (limited to 'kpov_judge/tasks/nat_port_forward')
-rw-r--r--kpov_judge/tasks/nat_port_forward/task.py41
1 files changed, 9 insertions, 32 deletions
diff --git a/kpov_judge/tasks/nat_port_forward/task.py b/kpov_judge/tasks/nat_port_forward/task.py
index 7613c4a..e96a8e7 100644
--- a/kpov_judge/tasks/nat_port_forward/task.py
+++ b/kpov_judge/tasks/nat_port_forward/task.py
@@ -19,10 +19,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 +29,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 +38,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 +57,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 +76,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 +86,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,7 +102,6 @@ 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_util.IPv4_subnet_gen(r, "10.36.0.0/14", 24)
@@ -127,12 +110,6 @@ def gen_params(user_id, params_meta):
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('.')]