-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5.py
executable file
·156 lines (130 loc) · 4.99 KB
/
lab5.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python
"""
Start up a Simple topology for CS144
"""
from mininet.net import Mininet
from mininet.node import Controller, RemoteController
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from mininet.topo import Topo
from mininet.util import quietRun
from mininet.moduledeps import pathCheck
from sys import exit
import os.path
from subprocess import Popen, STDOUT, PIPE
IPBASE = '10.3.0.0/16'
ROOTIP = '10.3.0.100/16'
IPCONFIG_FILE = './IP_CONFIG'
IP_SETTING={}
class CS144Topo( Topo ):
"NAT Topology"
def __init__( self, *args, **kwargs ):
Topo.__init__( self, *args, **kwargs )
server1 = self.addHost( 'server1' )
server2 = self.addHost( 'server2' )
nat = self.addSwitch( 'sw0' )
bridge = self.addSwitch( 'sw1' )
client = self.addHost('client')
self.addLink(client, nat)
for h in server1, server2, nat: #client, root:
self.addLink( h, bridge)
class CS144Controller( Controller ):
"Controller for CS144 Multiple IP Bridge"
def __init__( self, name, inNamespace=False, command='controller',
cargs='-v ptcp:%d', cdir=None, ip="127.0.0.1",
port=6633, **params ):
"""command: controller command name
cargs: controller command arguments
cdir: director to cd to before running controller
ip: IP address for controller
port: port for controller to listen at
params: other params passed to Node.__init__()"""
Controller.__init__( self, name, ip=ip, port=port, **params)
def start( self ):
"""Start <controller> <args> on controller.
Log to /tmp/cN.log"""
pathCheck( self.command )
cout = '/tmp/' + self.name + '.log'
if self.cdir is not None:
self.cmd( 'cd ' + self.cdir )
self.cmd( self.command, self.cargs % self.port, '>&', cout, '&' )
def stop( self ):
"Stop controller."
self.cmd( 'kill %' + self.command )
self.terminate()
def startsshd( host ):
"Start sshd on host"
stopsshd()
info( '*** Starting sshd\n' )
name, intf, ip = host.name, host.defaultIntf(), host.IP()
banner = '/tmp/%s.banner' % name
host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) )
host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' )
info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' )
def stopsshd():
"Stop *all* sshd processes with a custom banner"
info( '*** Shutting down stale sshd/Banner processes ',
quietRun( "pkill -9 -f Banner" ), '\n' )
def starthttp( host ):
"Start simple Python web server on hosts"
info( '*** Starting SimpleHTTPServer on host', host, '\n' )
host.cmd( 'cd ./http_%s/; nohup python2.7 ./webserver.py &' % (host.name) )
def stophttp():
"Stop simple Python web servers"
info( '*** Shutting down stale SimpleHTTPServers',
quietRun( "pkill -9 -f SimpleHTTPServer" ), '\n' )
info( '*** Shutting down stale webservers',
quietRun( "pkill -9 -f webserver.py" ), '\n' )
def set_default_route(host):
info('*** setting default gateway of host %s\n' % host.name)
if(host.name == 'server1'):
routerip = IP_SETTING['sw0-eth2']
elif(host.name == 'server2'):
routerip = IP_SETTING['sw0-eth2']
elif(host.name == 'client'):
routerip = IP_SETTING['sw0-eth1']
print host.name, routerip
host.cmd('route add %s/32 dev %s-eth0' % (routerip, host.name))
host.cmd('route add default gw %s dev %s-eth0' % (routerip, host.name))
ips = IP_SETTING[host.name].split(".")
host.cmd('route del -net %s.0.0.0/8 dev %s-eth0' % (ips[0], host.name))
def get_ip_setting():
if (not os.path.isfile(IPCONFIG_FILE)):
return -1
f = open(IPCONFIG_FILE, 'r')
for line in f:
if( len(line.split()) == 0):
break
name, ip = line.split()
print name, ip
IP_SETTING[name] = ip
return 0
def cs144net():
stophttp()
"Create a simple network for cs144"
r = get_ip_setting()
if r == -1:
exit("Couldn't load config file for ip addresses, check whether %s exists" % IPCONFIG_FILE)
else:
info( '*** Successfully loaded ip settings for hosts\n %s\n' % IP_SETTING)
topo = CS144Topo()
info( '*** Creating network\n' )
net = Mininet( topo=topo, controller=RemoteController, ipBase=IPBASE )
net.start()
server1, server2, client, nat = net.get( 'server1', 'server2', 'client', 'sw0')
s1intf = server1.defaultIntf()
s1intf.setIP('%s/8' % IP_SETTING['server1'])
s2intf = server2.defaultIntf()
s2intf.setIP('%s/8' % IP_SETTING['server2'])
clintf = client.defaultIntf()
clintf.setIP('%s/8' % IP_SETTING['client'])
for host in server1, server2, client:
set_default_route(host)
starthttp( server1 )
starthttp( server2 )
CLI( net )
stophttp()
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
cs144net()