Statistics
| Branch: | Tag: | Revision:

mininet / examples / xterms.py @ 98332cb6

History | View | Annotate | Download (1.48 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a network and run an xterm (connected via screen(1) ) on each
5
host. Requires xterm(1) and GNU screen(1).
6
"""
7

    
8
import os, re
9
from subprocess import Popen
10
from mininet import init, TreeNet, quietRun
11

    
12
def makeXterm( node, title ):
13
   "Run screen on a node, and hook up an xterm."
14
   title += ': ' + node.name
15
   if not node.inNamespace: title += ' (root)'
16
   cmd = [ 'xterm', '-title', title, '-e' ]
17
   if not node.execed:
18
      node.cmdPrint( 'screen -dmS ' + node.name )
19
      cmd += [ 'screen', '-D', '-RR', '-S', node.name ]
20
   else:
21
      cmd += [ 'sh', '-c', 'exec tail -f /tmp/' + node.name + '*.log' ]
22
   return Popen( cmd )
23

    
24
def cleanUpScreens():
25
   "Remove moldy old screen sessions."      
26
   r = r'(\d+.[hsc]\d+)'
27
   output = quietRun( 'screen -ls' ).split( '\n' )
28
   for line in output:
29
      m = re.search( r, line )
30
      if m is not None:
31
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
32
   
33
def makeXterms( nodes, title ):
34
   return [ makeXterm( node, title) for node in nodes]
35

    
36
def xterms( controllers, switches, hosts ):
37
   cleanUpScreens()
38
   terms = makeXterms( controllers, 'controller' )
39
   terms += makeXterms( switches, 'switch' )
40
   terms += makeXterms( hosts, 'host' )
41
   # Wait for xterms to exit
42
   for term in terms: 
43
      os.waitpid( term.pid, 0 )
44
   cleanUpScreens()
45
   
46
if __name__ == '__main__':
47
   init()
48
   print "Running xterms on", os.environ[ 'DISPLAY' ]
49
   network = TreeNet( depth=2, fanout=2, kernel=True )
50
   network.run( xterms )