firstTest.py

Mattia Milani, 03/08/2018 10:18 AM

Download (2.84 KB)

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3

    
4
import sys
5
sys.path.append('../')
6
from network_builder import *
7
from os import kill, path, makedirs
8
from matplotlib.pyplot import ion
9
from random import sample, randint
10

    
11
from test_generic import *
12

    
13
class mainFirstTest(MininetTest):
14

    
15
    def __init__(self, mininet, name, duration=10):
16

    
17
        super(mainFirstTest, self).__init__(mininet, path, duration)
18

    
19
    def launch_sniffer(self, host):
20

    
21
        cmd = "tcpdump -i any -n -X -e "
22

    
23
        logfile = self.prefix + host.name + "-dump.log"
24

    
25
        params = {}
26
        params['>'] = logfile
27
        params['2>'] = logfile
28

    
29
        return self.bgCmd(host, True, cmd,
30
                          *reduce(lambda x, y: x + y, params.items()))
31

    
32
    def launchPing(self, host):
33

    
34
        idps = randint(0,100)
35
        logfile = self.prefix + host.name.split('_')[0] + \
36
            "-" + str(idps) + "_ping_$(date +%s).log"
37

    
38
        cmd = "ping " + self.destination
39
        info("Host: "+ host.defaultIntf().ip +" lancio comando: " + cmd + "\n")
40
        params = {}
41
        params['>'] = logfile
42
        params['2>'] = logfile
43

    
44
        return self.bgCmd(host, True, cmd,
45
            *reduce(lambda x, y: x + y, params.items()) )
46

    
47
    def getRoutingTable(self, host):
48

    
49
        idps = randint(0,100)
50
        logfile = self.prefix + host.name.split('_')[0] + \
51
            "-" + str(idps) + "_route_$(date +%s).log"
52

    
53
        cmd = "route -n"
54
        info("Host: "+ host.defaultIntf().ip +" Comando: " + cmd + "\n")
55
        params = {}
56
        params['>'] = logfile
57

    
58
        return self.execute(host, True, cmd,
59
            *reduce(lambda x, y: x + y, params.items()) )
60

    
61
    def runTest(self):
62
        info("*** Launching Ping test\n")
63
        info("Data folder: "+self.prefix+"\n")
64

    
65
        #self.net.setNeighboursRoutes()
66

    
67
        for h in self.getAllHosts():
68
            self.launch_sniffer(h)
69

    
70
        listHost = self.getAllHosts()
71

    
72
        for h in listHost:
73
            self.destination = h.defaultIntf().ip
74
            for j in listHost:
75
                if j.defaultIntf().ip != self.destination:
76
                    self.launchPing(j)
77

    
78
        #info("Ottengo la routing table\n")
79
        #host_zero = self.getAllHosts()[0]
80
        #self.getRoutingTable(host_zero)
81

    
82
        #for ipInf in self.net.getNodeAddrs(host_zero):
83
        #    info("Il nodo zero ha un interfaccia con questo indirizzo: " + ipInf + "\n");
84

    
85
        #link_zero = self.net.getLinks()[0]
86
        #info("Link: " + link_zero.intf1.name + " - " + link_zero.intf2.name + "\n")
87

    
88
        info("Waiting completion...\n")
89
        self.wait(self.duration)
90
        self.killAll()
91

    
92
class firstTest(mainFirstTest):
93

    
94
    def __init__(self, mininet, name, args):
95

    
96
        duration = int(args["duration"])
97
        super(firstTest, self).__init__(mininet, duration)
98
        #self.destination = self.net.values()[0].defaultIntf().ip
99
        self.setPrefix(name)