Added py command to evaluate Python expressions, e.g. h1.cmd('ls')
This commit is contained in:
+24
-4
@@ -47,7 +47,7 @@ class CLI( Cmd ):
|
|||||||
|
|
||||||
def __init__( self, mininet ):
|
def __init__( self, mininet ):
|
||||||
self.mn = 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
|
self.nodemap = {} # map names to Node objects
|
||||||
for node in self.nodelist:
|
for node in self.nodelist:
|
||||||
self.nodemap[ node.name ] = node
|
self.nodemap[ node.name ] = node
|
||||||
@@ -69,7 +69,7 @@ class CLI( Cmd ):
|
|||||||
'\n'
|
'\n'
|
||||||
'The interpreter automatically substitutes IP '
|
'The interpreter automatically substitutes IP '
|
||||||
'addresses\n'
|
'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'
|
' like\n'
|
||||||
' mininet> h0 ping -c1 h1\n'
|
' mininet> h0 ping -c1 h1\n'
|
||||||
'should work.\n'
|
'should work.\n'
|
||||||
@@ -90,7 +90,7 @@ class CLI( Cmd ):
|
|||||||
"List network connections."
|
"List network connections."
|
||||||
for switch in self.mn.switches:
|
for switch in self.mn.switches:
|
||||||
info( '%s <->', switch.name )
|
info( '%s <->', switch.name )
|
||||||
for intf in switch.intfs:
|
for intf in switch.intfs.values():
|
||||||
name = switch.connection[ intf ][ 1 ]
|
name = switch.connection[ intf ][ 1 ]
|
||||||
info( ' %s' % name )
|
info( ' %s' % name )
|
||||||
info( '\n' )
|
info( '\n' )
|
||||||
@@ -99,6 +99,25 @@ class CLI( Cmd ):
|
|||||||
"Run an external shell command"
|
"Run an external shell command"
|
||||||
call( args, shell=True )
|
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 ):
|
def do_pingall( self, args ):
|
||||||
"Ping between all hosts."
|
"Ping between all hosts."
|
||||||
self.mn.pingAll()
|
self.mn.pingAll()
|
||||||
@@ -119,7 +138,8 @@ class CLI( Cmd ):
|
|||||||
def do_intfs( self, args ):
|
def do_intfs( self, args ):
|
||||||
"List interfaces."
|
"List interfaces."
|
||||||
for node in self.nodelist:
|
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 ):
|
def do_dump( self, args ):
|
||||||
"Dump node info."
|
"Dump node info."
|
||||||
|
|||||||
Reference in New Issue
Block a user