summaryrefslogtreecommitdiff
path: root/kpov_judge/tasks/dhcp_dns_predefined_ip/task.py
blob: 40507d5f18826f908ff9ef677b93b32a532540d9 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# kpov_random_helpers should be imported by add_assignment.py

instructions = {
    'si':"""
Ustvari tri navidezne računalnike. Za enega (SimpleArbiter) uporabi sliko diska simpleArbiter. Na drugega (DHCP_server) postavi strežnika DHCP in DNS in poskrbi, da ta računalnik dobi IP naslov {IP_server}. Poskrbi, da bo preostali računalnik (DHCP_client) dobil naslov {IP_client}, ki mu ga določi DHCP strežnik. Poskrbi še,da DNS strežnik vrne za hostname {HOSTNAME_X} IP naslov {IP_X}.
"""
}

computers = {
    'maliNetworkManager': {
        'disks': [
            {   'name': 'maliNetworkManager',
            },
            #{   'name': 'CDROM',
            #    'options':{'readonly': True},
            #    'parts': [],# no parts, no mounting.
            #}
        ],
        'network_interfaces': [{'network': 'net1'}],
        'flavor': 'm1.tiny',
        'config_drive': False

    },
    'maliBrezNetworkManager': {
        'disks': [
            {   'name': 'maliBrezNetworkManager',
            },
            #{   'name': 'CDROM',
            #    'options':{'readonly': True},
            #    'parts': [],# no parts, no mounting.
            #}
        ],
        'network_interfaces': [{'network': 'net1'}],
        'flavor': 'm1.tiny',
        'config_drive': False

    },
    'SimpleArbiter': {
        'disks': [
            {   'name': 'simpleArbiterDhcp',
                # 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 = {
    'IP_server': {'descriptions': {'si': 'IP naslov DHCP streznika'}, 'w': False, 'public':True, 'type': 'IP', 'generated': True},
    'IP_client': {'descriptions': {'si': 'IP naslov DHCP klienta'}, 'w': False, 'public': True, 'type': 'IP', 'generated': True},
    'MAC_client': {'descriptions': {'si': 'MAC naslov DHCP klienta'}, 'w': True, 'public': True, 'type': 'MAC', 'generated': False},
    'HOSTNAME_X': {'descriptions': {'si': 'Hostname za DNS'}, 'w': False, 'public': True, 'type': 'short_text', 'generated': True},
    'IP_X': {'descriptions': {'si': 'Naslov, ki ga vrne DNS'}, 'w': False, 'public': False, 'type': 'IP', 'generated': True},
}

def task(IP_server, IP_client, MAC_client, HOSTNAME_X):
    from pexpect import pxssh
    #import pexpect
    results = dict()
    peer_user = 'student'
    peer_passwd = 'vaje'
    sA = pxssh.pxssh()
    sB = pxssh.pxssh()
    #prijavimo se na streznik (IP_NM je IP nalov streznika)
    sA.login(IP_server, peer_user, peer_passwd)
    #prijavimo se na  klienta (IP_static je IP naslov klienta)
    sB.login(IP_client, peer_user, peer_passwd)
    sA.sendline('sudo ps -A')
    sA.sendline(peer_passwd)
    sA.prompt()
    results['dhcp_proces'] = sA.before
    # tukaj dobimo podatke, ce je na IP_NM naslovu res postavljen DHCP streznik
    sB.sendline('sudo dhcping -s {} -h {} -c {}'.format(IP_server, MAC_client, IP_client))
    sB.sendline(peer_passwd)
    sB.prompt()
    results['dhcp'] = sB.before
    # tukaj pa dobimo podatek, ce je IP, ki ga vraca DNS streznik za HOSTNAME_X pravilen
    sB.sendline('nslookup {}'.format(HOSTNAME_X))
    sB.prompt()
    results['dns_hostname'] = sB.before
    sB.sendline('/sbin/ifconfig')
    sB.prompt()
    results['client_IP'] = sB.before
    sA.logout()
    sB.logout()
    return results


def gen_params(user_id, params_meta):
    params = dict()
    r = random.Random(user_id+"bla")
    net = kpov_random_helpers.IPv4_subnet_gen(r, '172.23.128.0/18', 24)
    params['IP_server'], params['IP_client'], params['IP_X'] = kpov_random_helpers.IPv4_addr_gen(r, net, 3)
    params['HOSTNAME_X'] = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ') for i in range(5)])
    return params


def task_check(results, params):
    import re
    score = 0
    hints = []
    if results['dhcp'].find("Got answer from: {}".format(params['IP_server'])) > -1:
        score += 2
    else:
        hints += ["dhcp server IP wrong"]
    if results['dhcp_proces'].find("dhcpd") > -1:
        score += 2
    else:
        hints += ["dhcp wrong"]
    if results['dns_hostname'].find("Address: {}".format(params['IP_X'])) >= 0:
        score += 2
    else:
        hints += ['dns  hostname IP wrong']
    if results['dns_hostname'].find("Server:\t\t{}".format(params['IP_server'])) > -1:
        score += 2
    else:
        hints += ['dns server IP wrong']
    if results['client_IP'].find('inet {}'.format(params['IP_client'])) > -1:
        score += 2
    else:
        hints += ['client IP wrong']
    return score, hints


def prepare_disks(templates, task_params, global_params):
    write_default_config(templates['simpleArbiterDhcp'], global_params)