From 1fdcd6768c3df563b8ee4fff12b6b08976139bc1 Mon Sep 17 00:00:00 2001
From: Bob Lantz <rlantz@cs.stanford.edu>
Date: Wed, 3 Mar 2010 13:46:44 -0800
Subject: [PATCH] Added py command to evaluate Python expressions, e.g.
 h1.cmd('ls')

---
 mininet/cli.py | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/mininet/cli.py b/mininet/cli.py
index cefb75b6..9e334b49 100644
--- a/mininet/cli.py
+++ b/mininet/cli.py
@@ -47,7 +47,7 @@ class CLI( Cmd ):
 
     def __init__( self, mininet ):
         self.mn = mininet
-        self.nodelist = self.mn.hosts + self.mn.switches + self.mn.controllers
+        self.nodelist = self.mn.controllers + self.mn.switches + self.mn.hosts
         self.nodemap = {} # map names to Node objects
         for node in self.nodelist:
             self.nodemap[ node.name ] = node
@@ -69,9 +69,9 @@ def do_help( self, args ):
                    '\n'
                    'The interpreter automatically substitutes IP '
                    'addresses\n'
-                   'for node names when a node is the first arg, so command'
+                   'for node names when a node is the first arg, so commands'
                    ' like\n'
-                   '  mininet> h0 ping -c1 h1\n'
+                   ' mininet> h0 ping -c1 h1\n'
                    'should work.\n'
                    '\n'
                    'Interactive commands are not really supported yet,\n'
@@ -90,7 +90,7 @@ def do_net( self, args ):
         "List network connections."
         for switch in self.mn.switches:
             info( '%s <->', switch.name )
-            for intf in switch.intfs:
+            for intf in switch.intfs.values():
                 name = switch.connection[ intf ][ 1 ]
                 info( ' %s' % name )
             info( '\n' )
@@ -99,6 +99,25 @@ def do_sh( self, args ):
         "Run an external shell command"
         call( args, shell=True )
 
+    # do_py() needs to catch any exception during eval()
+    # pylint: disable-msg=W0703
+
+    def do_py( self, args ):
+        """Evaluate a Python expression.
+           Node names may be used, e.g.: h1.cmd('ls')"""
+        try:
+            result = eval( args, globals(), self.nodemap )
+            if not result:
+                return
+            elif isinstance( result, str ):
+                info( result + '\n' )
+            else:
+                info( repr( result ) + '\n' )
+        except Exception, e:
+            info( str( e ) + '\n' )
+
+    # pylint: enable-msg=W0703
+
     def do_pingall( self, args ):
         "Ping between all hosts."
         self.mn.pingAll()
@@ -119,7 +138,8 @@ def do_iperfudp( self, args ):
     def do_intfs( self, args ):
         "List interfaces."
         for node in self.nodelist:
-            info( '%s: %s\n' % ( node.name, ' '.join( node.intfs ) ) )
+            info( '%s: %s\n' %
+                ( node.name, ' '.join( sorted( node.intfs.values() ) ) ) )
 
     def do_dump( self, args ):
         "Dump node info."
-- 
GitLab