Skip to content
Snippets Groups Projects
xterms.py 1.46 KiB
Newer Older
Bob Lantz's avatar
Bob Lantz committed
#!/usr/bin/python

Bob Lantz's avatar
Bob Lantz committed
Create a network and run an xterm (connected via screen(1) ) on each
host. Requires xterm(1) and GNU screen(1).
Bob Lantz's avatar
Bob Lantz committed

Bob Lantz's avatar
Bob Lantz committed
import os, re
Bob Lantz's avatar
Bob Lantz committed
from subprocess import Popen
from mininet import init, TreeNet, Cli, quietRun

def makeXterm( node, title ):
   "Run screen on a node, and hook up an xterm."
   node.cmdPrint( 'screen -dmS ' + node.name )
Bob Lantz's avatar
Bob Lantz committed
   if not node.inNamespace: title += ' (root)'
   cmd = [ 'xterm', '-title', title ]
   cmd += [ '-e', 'screen', '-D', '-RR', '-S', node.name ]
   return Popen( cmd )
Bob Lantz's avatar
Bob Lantz committed

def cleanUpScreens():
   "Remove moldy old screen sessions."      
Bob Lantz's avatar
Bob Lantz committed
   r = r'(\d+.[hsc]\d+)'
   output = quietRun( 'screen -ls' ).split( '\n' )
   for line in output:
      m = re.search( r, line )
      if m is not None:
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
Bob Lantz's avatar
Bob Lantz committed
def makeXterms( nodes, title ):
   terms = []
   for node in nodes:
      if not node.execed:
         terms += [ makeXterm( node, title ) ]
   return terms

def xterms( controllers, switches, hosts ):
Bob Lantz's avatar
Bob Lantz committed
   cleanUpScreens()
Bob Lantz's avatar
Bob Lantz committed
   terms = []
   terms += makeXterms( controllers, 'controller' )
   terms += makeXterms( switches, 'switch' )
   terms += makeXterms( hosts, 'host' )
   # Wait for xterms to exit
Bob Lantz's avatar
Bob Lantz committed
   for term in terms: 
Bob Lantz's avatar
Bob Lantz committed
      os.waitpid( term.pid, 0 )
Bob Lantz's avatar
Bob Lantz committed
   cleanUpScreens()
Bob Lantz's avatar
Bob Lantz committed
   
if __name__ == '__main__':
   init()
Bob Lantz's avatar
Bob Lantz committed
   print "Running xterms on", os.environ[ 'DISPLAY' ]
   network = TreeNet( depth=2, fanout=2, kernel=True )
Bob Lantz's avatar
Bob Lantz committed
   network.run( xterms )