def do_px(self, line): """Execute a Python statement. Node names may be used, e.g.: px print h1.cmd('ls')""" try: exec(line, globals(), self.getLocals()) except Exception as e: output(str(e) + '\n')
def do_dpctl(self, line): """Run dpctl (or ovs-ofctl) command on all switches. Usage: dpctl command [arg1] [arg2] ...""" args = line.split() if len(args) < 1: error('usage: dpctl command [arg1] [arg2] ...\n') return for sw in self.mn.switches: output('*** ' + sw.name + ' ' + ('-' * 72) + '\n') output(sw.dpctl(*args))
def waitForNode(self, node): "Wait for a node to finish, and print its output." # Pollers nodePoller = poll() nodePoller.register(node.stdout) bothPoller = poll() bothPoller.register(self.stdin, POLLIN) bothPoller.register(node.stdout, POLLIN) if self.isatty(): # Buffer by character, so that interactive # commands sort of work quietRun('stty -isig -icanon min 1') while node.shell: try: bothPoller.poll() # XXX BL: this doesn't quite do what we want. if False and 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) if isReadable(nodePoller): data = node.monitor() output(data) if not node.waiting: quietRun('stty isig') break except KeyboardInterrupt: # There is an at least one race condition here, since # it's possible to interrupt ourselves after we've # read data but before it has been printed. node.sendInt() """ #TODO THERE ARE YOU NEED TO SEND OWN INTERRUPT """ except select.error as e: # pylint: disable=unpacking-non-sequence errno_, errmsg = e.args # pylint: enable=unpacking-non-sequence if errno_ != errno.EINTR: error("select.error: %d, %s" % (errno_, errmsg)) node.sendInt()
def dumpNodeConnections( nodes ): "Dump connections to/from nodes." def dumpConnections( node ): "Helper function: dump connections to node" for intf in node.intfList(): output( ' %s:' % intf ) if intf.link: intfs = [ intf.link.intf1, intf.link.intf2 ] intfs.remove( intf ) output( intfs[ 0 ] ) else: output( ' ' ) for node in nodes: output( node.name ) dumpConnections( node ) output( '\n' )
def run(self): "Run our cmdloop(), catching KeyboardInterrupt" while True: try: # Make sure no nodes are still waiting for node in self.mn.values(): while node.waiting: info('stopping', node, '\n') node.sendInt() node.waitOutput() if self.isatty(): quietRun('stty echo sane intr ^C') self.cmdloop() break except KeyboardInterrupt: # Output a message - unless it's also interrupted # pylint: disable=broad-except try: output('\nInterrupt\n') except Exception: pass
def dumpPorts( switches ): "dump interface to openflow port mappings for each switch" for switch in switches: output( '%s ' % switch.name ) for intf in switch.intfList(): port = switch.ports[ intf ] output( '%s:%d ' % ( intf, port ) ) output( '\n' )
def dumpConnections( node ): "Helper function: dump connections to node" for intf in node.intfList(): output( ' %s:' % intf ) if intf.link: intfs = [ intf.link.intf1, intf.link.intf2 ] intfs.remove( intf ) output( intfs[ 0 ] ) else: output( ' ' )
def do_py(self, line): """Evaluate a Python expression. Node names may be used, e.g.: py h1.cmd('ls')""" try: result = eval(line, globals(), self.getLocals()) if not result: return elif isinstance(result, str): output(result + '\n') else: output(repr(result) + '\n') except Exception as e: output(str(e) + '\n')
def do_links(self, _line): "Report on links" for link in self.mn.links: output(link, link.status(), '\n')
def do_time(self, line): "Measure time taken for any command in Mininet." start = time.time() self.onecmd(line) elapsed = time.time() - start output("*** Elapsed time: %0.6f secs\n" % elapsed)
def do_EOF(self, line): "Exit" output('\n') return self.do_exit(line)
def do_dump(self, _line): "Dump node info." for node in self.mn.values(): output('%s\n' % repr(node))
def do_intfs(self, _line): "List interfaces." for node in self.mn.values(): output('%s: %s\n' % (node.name, ','.join(node.intfNames())))
def do_nodes(self, _line): "List all nodes." nodes = ' '.join(sorted(self.mn)) output('available nodes are: \n%s\n' % nodes)
def do_help(self, line): "Describe available CLI commands." RCmd.do_help(self, line) if line is '': output(self.helpStr)
def errRun( *cmd, **kwargs ): """Run a command and return stdout, stderr and return code cmd: string or list of command and args stderr: STDOUT to merge stderr with stdout shell: run command using shell echo: monitor output to console""" # By default we separate stderr, don't run in a shell, and don't echo stderr = kwargs.get( 'stderr', PIPE ) shell = kwargs.get( 'shell', False ) echo = kwargs.get( 'echo', False ) if echo: # cmd goes to stderr, output goes to stdout info( cmd, '\n' ) if len( cmd ) == 1: cmd = cmd[ 0 ] # Allow passing in a list or a string if isinstance( cmd, BaseString ) and not shell: cmd = cmd.split( ' ' ) cmd = [ str( arg ) for arg in cmd ] elif isinstance( cmd, list ) and shell: cmd = " ".join( arg for arg in cmd ) debug( '*** errRun:', cmd, '\n' ) popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell ) # We use poll() because select() doesn't work with large fd numbers, # and thus communicate() doesn't work either out, err = '', '' poller = poll() poller.register( popen.stdout, POLLIN ) fdtofile = { popen.stdout.fileno(): popen.stdout } outDone, errDone = False, True if popen.stderr: fdtofile[ popen.stderr.fileno() ] = popen.stderr poller.register( popen.stderr, POLLIN ) errDone = False while not outDone or not errDone: readable = poller.poll() for fd, event in readable: f = fdtofile[ fd ] if event & POLLIN: data = f.read( 1024 ) if Python3: data = data.decode( Encoding ) if echo: output( data ) if f == popen.stdout: out += data if data == '': outDone = True elif f == popen.stderr: err += data if data == '': errDone = True else: # POLLHUP or something unexpected if f == popen.stdout: outDone = True elif f == popen.stderr: errDone = True poller.unregister( fd ) returncode = popen.wait() # Python 3 complains if we don't explicitly close these popen.stdout.close() if stderr == PIPE: popen.stderr.close() debug( out, err, returncode ) return out, err, returncode