def sendCmd( self, *args, **kwargs ): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" assert not self.waiting printPid = kwargs.get( 'printPid', True ) # Allow sendCmd( [ list ] ) if len( args ) == 1 and type( args[ 0 ] ) is list: cmd = args[ 0 ] # Allow sendCmd( cmd, arg1, arg2... ) elif len( args ) > 0: cmd = args # Convert to string if not isinstance( cmd, str ): cmd = ' '.join( [ str( c ) for c in cmd ] ) if not re.search( r'\w', cmd ): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd printPid = printPid and not isShellBuiltin( cmd ) if len( cmd ) > 0 and cmd[ -1 ] == '&': # print ^A{pid}\n{sentinel} cmd += ' printf "\\001%d\n\\177" $! \n' else: # print sentinel cmd += '; printf "\\177"' if printPid and not isShellBuiltin( cmd ): cmd = 'mnexec -p ' + cmd self.write( cmd + '\n' ) self.lastPid = None self.waiting = True
def sendCmd( self, *args, **kwargs ): assert self.shell and not self.waiting printPid = kwargs.get( 'printPid', True ) # Allow sendCmd( [ list ] ) if len( args ) == 1 and isinstance( args[ 0 ], list ): cmd = args[ 0 ] # Allow sendCmd( cmd, arg1, arg2... ) elif len( args ) > 0: cmd = args # Convert to string if not isinstance( cmd, str ): cmd = ' '.join( [ str( c ) for c in cmd ] ) if not re.search( r'\w', cmd ): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd printPid = printPid and not isShellBuiltin( cmd ) if len( cmd ) > 0 and cmd[ -1 ] == '&': # print ^A{pid}\n{sentinel} cmd += ' printf "\\001%d\\012" $! ' else: pass self.write( cmd + '\n' ) self.lastPid = None self.waiting = True
def sendCmd(self, *args, **kwargs): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID? (False)""" assert self.shell and not self.waiting printPid = kwargs.get('printPid', False) # Allow sendCmd( [ list ] ) if len(args) == 1 and isinstance(args[ 0 ], list): cmd = args[ 0 ] # Allow sendCmd( cmd, arg1, arg2... ) elif len(args) > 0: cmd = args # Convert to string if not isinstance(cmd, str): cmd = ' '.join([ str(c) for c in cmd ]) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd # if a builtin command is backgrounded, it still yields a PID if len(cmd) > 0 and cmd[ -1 ] == '&': # print ^A{pid}\n so monitor() can set lastPid cmd += ' printf "\\001%d\\012" $! ' elif printPid and not isShellBuiltin(cmd): cmd = 'mnexec -p ' + cmd self.write(cmd + '\n') self.lastPid = None self.waiting = True
def sendCmd(self, *args, **kwargs): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" assert not self.waiting printPid = kwargs.get('printPid', True) if len(args) > 0: cmd = args if not isinstance(cmd, str): cmd = ' '.join(cmd) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' if len(cmd) > 0 and cmd[-1] == '&': separator = '&' cmd = cmd[:-1] else: separator = ';' if printPid and not isShellBuiltin(cmd): cmd = 'mnexec -p ' + cmd self.write(cmd + separator + ' printf "\\177" \n') self.lastCmd = cmd self.lastPid = None self.waiting = True
def sendCmd( self, *args, **kwargs ): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" assert not self.waiting printPid = kwargs.get( 'printPid', True ) if len( args ) > 0: cmd = args if not isinstance( cmd, str ): cmd = ' '.join( cmd ) if not re.search( r'\w', cmd ): # Replace empty commands with something harmless cmd = 'echo -n' if len( cmd ) > 0 and cmd[ -1 ] == '&': separator = '&' cmd = cmd[ :-1 ] else: separator = ';' if printPid and not isShellBuiltin( cmd ): cmd = 'mnexec -p ' + cmd self.write( cmd + separator + ' printf "\\177" \n' ) self.lastCmd = cmd self.lastPid = None self.waiting = True
def default( self, line ): """Called on an input line when the command prefix is not recognized. Overridden to run shell commands when a node is the first CLI argument. Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" first, args, line = self.parseline( line ) if first in self.mn: if not args: print "*** Enter a command for node: %s <cmd>" % first return node = self.mn[ first ] rest = args.split( ' ' ) # Substitute IP addresses for node names in command # If updateIP() returns None, then use node name rest = [ self.mn[ arg ].defaultIntf().updateIP() or arg if arg in self.mn else arg for arg in rest ] rest = ' '.join( rest ) # Run cmd on node: builtin = isShellBuiltin( first ) node.sendCmd( rest, printPid=( not builtin ) ) self.waitForNode( node ) else: error( '*** Unknown command: %s\n' % line )
def default(self, line): """Called on an input line when the command prefix is not recognized. Overridden to run shell commands when a node is the first CLI argument. Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" first, args, line = self.parseline(line) if args and len(args) > 0 and args[-1] == '\n': args = args[:-1] rest = args.split(' ') if first in self.nodemap: node = self.nodemap[first] # Substitute IP addresses for node names in command for index in range(len(rest)): arg = rest[index] if arg in self.nodemap: ip = self.nodemap[arg].IP() if not ip: error('%s is an unreachable, detached host\n' % arg) return rest[index] = ip #rest = [ self.nodemap[ arg ].IP() # if arg in self.nodemap else arg # for arg in rest ] rest = ' '.join(rest) # Run cmd on node: builtin = isShellBuiltin(first) node.sendCmd(rest, printPid=(not builtin)) self.waitForNode(node) else: error('*** Unknown command: %s\n' % first)
def default( self, line ): """Called on an input line when the command prefix is not recognized. Overridden to run shell commands when a node is the first CLI argument. Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" first, args, line = self.parseline( line ) if args and len(args) > 0 and args[ -1 ] == '\n': args = args[ :-1 ] rest = args.split( ' ' ) if first in self.nodemap: node = self.nodemap[ first ] # Substitute IP addresses for node names in command for index in range(len(rest)): arg = rest[index] if arg in self.nodemap: ip = self.nodemap[arg].IP() if not ip: error('%s is an unreachable, detached host\n' % arg) return rest[index] = ip #rest = [ self.nodemap[ arg ].IP() # if arg in self.nodemap else arg # for arg in rest ] rest = ' '.join( rest ) # Run cmd on node: builtin = isShellBuiltin( first ) node.sendCmd( rest, printPid=( not builtin ) ) self.waitForNode( node ) else: error( '*** Unknown command: %s\n' % first )
def sendCmd(self, *args, **kwargs): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID? (False)""" assert self.shell and not self.waiting printPid = kwargs.get('printPid', False) # Allow sendCmd( [ list ] ) if len(args) == 1 and isinstance(args[0], list): cmd = args[0] # Allow sendCmd( cmd, arg1, arg2... ) elif len(args) > 0: cmd = args # Convert to string if not isinstance(cmd, str): cmd = ' '.join([str(c) for c in cmd]) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd # if a builtin command is backgrounded, it still yields a PID if len(cmd) > 0 and cmd[-1] == '&': # print ^A{pid}\n so monitor() can set lastPid cmd += ' printf "\\001%d\\012" $! ' elif printPid and not isShellBuiltin(cmd): cmd = 'mnexec -p ' + cmd self.write(cmd + '\n') self.lastPid = None self.waiting = True
def sendCmd(self, *args, **kwargs): assert not self.waiting printPid = kwargs.get('printPid', True) # Allow sendCmd( [ list ] ) if len(args) == 1 and type(args[0]) is list: cmd = args[0] # Allow sendCmd( cmd, arg1, arg2... ) elif len(args) > 0: cmd = args # Convert to string if not isinstance(cmd, str): cmd = ' '.join([str(c) for c in cmd]) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd printPid = printPid and not isShellBuiltin(cmd) if len(cmd) > 0 and cmd[-1] == '&': # print ^A{pid}\n{sentinel} cmd += ' printf "\\001%d\\012" $! ' else: pass self.write(cmd + '\n') self.lastPid = None self.waiting = True
def default( self, line ): """Called on an input line when the command prefix is not recognized. Overridden to run shell commands when a component is the first CLI argument. Past the first CLI argument, component names are then automatically replaced with corresponding node IP addrs.""" first, args, line = self.parseline( line ) if not args: return if args and len(args) > 0 and args[ -1 ] == '\n': args = args[ :-1 ] rest = args.split( ' ' ) if first in self.cn: comp = self.cn[ first ] # Substitute IP addresses for node names in command rest = [ self.cn[ arg ].node.defaultIntf().updateIP() if arg in self.cn else arg for arg in rest ] rest = ' '.join( rest ) # Run cmd on node: builtin = isShellBuiltin( first ) comp.node.sendCmd( rest, printPid=( not builtin ) ) self.waitForNode( comp.node ) else: error( '*** Unknown command: %s\n' % first )
def sendCmd( self, *args, **kwargs ): assert not self.waiting printPid = kwargs.get( 'printPid', True ) if len( args ) == 1 and type( args[ 0 ] ) is list: cmd = args[ 0 ] elif len( args ) > 0: cmd = args if not isinstance( cmd, str ): cmd = ' '.join( [ str( c ) for c in cmd ] ) if not re.search( r'\w', cmd ): cmd = 'echo -n' self.lastCmd = cmd printPid = printPid and not isShellBuiltin( cmd ) if len( cmd ) > 0 and cmd[ -1 ] == '&': cmd += ' printf "\\001%d\n\\177" $! \n' else: cmd += '; printf "\\177"' self.write( cmd + '\n' ) self.lastPid = None self.waiting = True
def sendCmd(self, *args, **kwargs): """Send a command, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" assert not self.waiting self.serial += 1 self.write('echo __ %s __\n' % self.serial) match = '__ %s __' % self.serial buf = '' while True: i = buf.find(match) if i >= 0: buf = buf[i + len(match):] break buf += self.read(1024) while True: if chr(127) in buf: break buf += self.read(1024) printPid = kwargs.get('printPid', True) if len(args) > 0: cmd = args if not isinstance(cmd, str): cmd = ' '.join(cmd) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' if printPid and not isShellBuiltin(cmd): use_mnexec = kwargs.get('mn_use_mnexec', True) if use_mnexec: cmd = 'mnexec -p ' + cmd disable_io_buf = kwargs.get('mn_disable_io_buf', False) if disable_io_buf: cmd = 'stdbuf -i0 -o0 -e0 ' + cmd self.write(cmd + '\n') wait_flag = kwargs.get('mn_wait', True) if wait_flag: self.lastCmd = cmd self.lastPid = None self.waiting = True
def sendCmd( self, *args, **kwargs ): """Send a command, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" assert not self.waiting self.serial += 1 self.write( 'echo __ %s __\n' % self.serial ) match = '__ %s __' % self.serial buf = '' while True: i = buf.find( match ) if i >= 0: buf = buf[ i + len( match ): ] break buf += self.read( 1024 ) while True: if chr( 127 ) in buf: break buf += self.read( 1024 ) printPid = kwargs.get( 'printPid', True ) if len( args ) > 0: cmd = args if not isinstance( cmd, str ): cmd = ' '.join( cmd ) if not re.search( r'\w', cmd ): # Replace empty commands with something harmless cmd = 'echo -n' if printPid and not isShellBuiltin( cmd ): use_mnexec = kwargs.get( 'mn_use_mnexec', True) if use_mnexec: cmd = 'mnexec -p ' + cmd disable_io_buf = kwargs.get( 'mn_disable_io_buf', False) if disable_io_buf: cmd = 'stdbuf -i0 -o0 -e0 ' + cmd self.write( cmd + '\n' ) wait_flag = kwargs.get( 'mn_wait', True) if wait_flag: self.lastCmd = cmd self.lastPid = None self.waiting = True
def default(self, line): """Called on an input line when the command prefix is not recognized. Overridden to run shell commands when a node is the first CLI argument. Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" first, args, line = self.parseline(line) if args and len(args) > 0 and args[-1] == "\n": args = args[:-1] rest = args.split(" ") if first in self.nodemap: node = self.nodemap[first] # Substitute IP addresses for node names in command rest = [self.nodemap[arg].IP() if arg in self.nodemap else arg for arg in rest] rest = " ".join(rest) # Run cmd on node: builtin = isShellBuiltin(first) node.sendCmd(rest, printPid=(not builtin)) self.waitForNode(node) else: error("*** Unknown command: %s\n" % first)
def sendCmd(self, *args, **kwargs): """Send a command, followed by a command to echo a sentinel, and return without waiting for the command to complete. args: command and arguments, or string printPid: print command's PID?""" print 'got commmand = ' print args assert not self.waiting printPid = kwargs.get('printPid', True) # Allow sendCmd( [ list ] ) if len(args) == 1 and type(args[0]) is list: cmd = args[0] # Allow sendCmd( cmd, arg1, arg2... ) elif len(args) > 0: cmd = args cmdorig = cmd # Convert to string if not isinstance(cmd, str): cmd = ' '.join([str(c) for c in cmd]) if not re.search(r'\w', cmd): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd printPid = printPid and not isShellBuiltin(cmd) #new_cmd = ['docker', 'exec', "mininet-"+self.name] #new_cmd = new_cmd + list(cmdorig) new_cmd = 'docker exec ' + "mininet-" + self.name + ' ' + cmd call(new_cmd, shell=True) '''pidp = Popen( new_cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=False ) ps_out = pidp.stdout.readlines() if not ps_out: print 'no output' else: print ps_out[0] call("sleep 2", shell=True) ''' '''if len( cmd ) > 0 and cmd[ -1 ] == '&':