From 6aff964ea07a04e620e5db154bb7368a370323ba Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Tue, 10 Aug 2010 18:47:15 -0700 Subject: [PATCH] Added "source" command and restored echo after noecho command. Interactive commands should "work", sort of. --- mininet/cli.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index 8bfa424..c328da8 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -50,6 +50,7 @@ class CLI( Cmd ): self.stdin = stdin self.inPoller = poll() self.inPoller.register( stdin ) + self.inputFile = None Cmd.__init__( self ) info( '*** Starting CLI:\n' ) while True: @@ -217,7 +218,26 @@ class CLI( Cmd ): if self.isatty(): quietRun( 'stty -echo' ) self.default( line ) - # default() fixes tty + if self.isatty(): + quietRun( 'stty echo' ) + + def do_source( self, line ): + "Read commands from an input file." + args = line.split() + if len(args) != 1: + error( 'usage: source \n' ) + return + try: + self.inputFile = open( args[ 0 ] ) + while True: + line = self.inputFile.readline() + if len( line ) > 0: + self.onecmd( line ) + else: + break + except IOError: + pass + self.inputFile = None def default( self, line ): """Called on an input line when the command prefix is not recognized. @@ -226,7 +246,7 @@ class CLI( Cmd ): corresponding IP addrs.""" first, args, line = self.parseline( line ) - if len(args) > 0 and args[ -1 ] == '\n': + if args and len(args) > 0 and args[ -1 ] == '\n': args = args[ :-1 ] rest = args.split( ' ' ) @@ -243,6 +263,7 @@ class CLI( Cmd ): self.waitForNode( node ) else: error( '*** Unknown command: %s\n' % first ) + # pylint: enable-msg=W0613,R0201 @@ -261,6 +282,12 @@ class CLI( Cmd ): while True: try: bothPoller.poll() + if self.inputFile: + key = self.inputFile.read( 1 ) + if key is not '': + node.write(key) + else: + self.inputFile = None if isReadable( self.inPoller ): key = self.stdin.read( 1 ) node.write( key )