From 7aabade2b64ecf58ea7c10cf7be060c9c74fafb4 Mon Sep 17 00:00:00 2001 From: Matthias Fetzer Date: Fri, 25 Mar 2016 15:00:35 +0100 Subject: [PATCH 1/4] Added multi host support. --- mininet/cli.py | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index a7fab397..b6bf061d 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -401,25 +401,48 @@ def default( self, line ): Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" + # Hosts are now a list. + host_list = [] + first, args, line = self.parseline( line ) - if first in self.mn: - if not args: - print "*** Enter a command for node: %s " % 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: - node.sendCmd( rest ) - self.waitForNode( node ) - else: - error( '*** Unknown command: %s\n' % line ) + # If the command begins with a host and a range. + # E.g.: h[1-2], h[3,4,7-10],... then parse the + # list and create the host list. Otherwise, add + # the first argument to the list ("normal" processing). + m = re.match("(.*?)\[(.*?)\]", line) + if (m and m.group(2)): + host_string = m.group(2) + host_ranges = (host_range.split("-") for host_range in host_string.split(",")) + host_list = [first + str(i) for hr in host_ranges for i in range(int(hr[0]), int(hr[-1]) + 1)] + args = " ".joint(args.split()[1:]) + else: + host_list.append(first) + + # Iterate over all hosts and execute the command. + for host in host_list: + if first in self.mn: + if not args: + print "*** Enter a command for node: %s " % 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 ) + + # If the command is executed on more than one host, + # print on which host the command is executed. + if (len(host_list) > 1): + print "*** Executing command on node: %s" % host + # Run cmd on node: + node.sendCmd( rest ) + self.waitForNode( node ) + else: + error( '*** Unknown command: %s\n' % line ) def waitForNode( self, node ): "Wait for a node to finish, and print its output." From 9ca53c083040dc12455631b7d0da660ca703fe02 Mon Sep 17 00:00:00 2001 From: Matthias Fetzer Date: Wed, 13 Apr 2016 16:46:46 +0200 Subject: [PATCH 2/4] Squashed commit of the following: commit b97cec163953fd6852831594462f3cd4d55ee52c Author: Matthias Fetzer Date: Wed Apr 13 16:41:02 2016 +0200 Fixes. --- mininet/cli.py | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index b6bf061d..1efe08c7 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -33,6 +33,7 @@ import time import os import atexit +import re from mininet.log import info, output, error from mininet.term import makeTerms, runX11 @@ -401,31 +402,29 @@ def default( self, line ): Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" - # Hosts are now a list. - host_list = [] + + host_list = [ ] first, args, line = self.parseline( line ) - # If the command begins with a host and a range. - # E.g.: h[1-2], h[3,4,7-10],... then parse the - # list and create the host list. Otherwise, add - # the first argument to the list ("normal" processing). m = re.match("(.*?)\[(.*?)\]", line) if (m and m.group(2)): - host_string = m.group(2) - host_ranges = (host_range.split("-") for host_range in host_string.split(",")) - host_list = [first + str(i) for hr in host_ranges for i in range(int(hr[0]), int(hr[-1]) + 1)] - args = " ".joint(args.split()[1:]) - else: + #print args + s = m.group(2) + ranges = (x.split("-") for x in s.split(",")) + #print ranges + host_list = [first + str(i) for r in ranges for i in range(int(r[0]), int(r[-1]) + 1)] + args = " ".join(args.split()[1:]) + #print args + else: host_list.append(first) - # Iterate over all hosts and execute the command. for host in host_list: - if first in self.mn: + if host in self.mn: if not args: - print "*** Enter a command for node: %s " % first + print "*** Enter a command for node: %s " % host return - node = self.mn[ first ] + node = self.mn[ host ] rest = args.split( ' ' ) # Substitute IP addresses for node names in command # If updateIP() returns None, then use node name @@ -433,18 +432,15 @@ def default( self, line ): if arg in self.mn else arg for arg in rest ] rest = ' '.join( rest ) - - # If the command is executed on more than one host, - # print on which host the command is executed. - if (len(host_list) > 1): - print "*** Executing command on node: %s" % host # Run cmd on node: + if(len(host_list) > 1): + print "*** Executing command on node: %s" % host node.sendCmd( rest ) - self.waitForNode( node ) + self.waitForNode( node, (len(host_list) > 1) ) else: error( '*** Unknown command: %s\n' % line ) - def waitForNode( self, node ): + def waitForNode( self, node, is_list ): "Wait for a node to finish, and print its output." # Pollers nodePoller = poll() From b2208b320c3ea972f12574e42e25fc761a258de5 Mon Sep 17 00:00:00 2001 From: Matthias Fetzer Date: Wed, 8 Feb 2017 10:18:25 -0800 Subject: [PATCH 3/4] Fixed bug, that breaks commands which include brackets. Minor refactoring. --- mininet/cli.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index cf005af5..e55423ef 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -404,24 +404,9 @@ def default( self, line ): Past the first CLI argument, node names are automatically replaced with corresponding IP addrs.""" + hostList, args = self.parseHosts(line) - host_list = [ ] - - first, args, line = self.parseline( line ) - - m = re.match("(.*?)\[(.*?)\]", line) - if (m and m.group(2)): - #print args - s = m.group(2) - ranges = (x.split("-") for x in s.split(",")) - #print ranges - host_list = [first + str(i) for r in ranges for i in range(int(r[0]), int(r[-1]) + 1)] - args = " ".join(args.split()[1:]) - #print args - else: - host_list.append(first) - - for host in host_list: + for host in hostList: if host in self.mn: if not args: error( "*** Enter a command for node: %s " % host ) @@ -435,10 +420,10 @@ def default( self, line ): for arg in rest ] rest = ' '.join( rest ) # Run cmd on node: - if(len(host_list) > 1): + if(len(hostList) > 1): print "*** Executing command on node: %s" % host node.sendCmd( rest ) - self.waitForNode( node, (len(host_list) > 1) ) + self.waitForNode( node, (len(hostList) > 1) ) else: error( '*** Unknown command: %s\n' % line ) @@ -485,6 +470,24 @@ def precmd( self, line ): return line + def parseHosts( self, line ): + "expand the hosts to a host list. This is called from the default function." + + hostList = [] + first, args, line = self.parseline( line ) + + match = re.match("(\w)\[(.*?)\]", line) + + if (match and match.group(2)): + s = match.group(2) + ranges = (x.split("-") for x in s.split(",")) + hostList = [first + str(i) for r in ranges for i in range(int(r[0]), int(r[-1]) + 1)] + args = " ".join(args.split()[1:]) + else: + hostList.append(first) + + return hostList, args + # Helper functions def isReadable( poller ): From f790799aa5311f8e0ae2345838b1368fe4056294 Mon Sep 17 00:00:00 2001 From: Matthias Fetzer Date: Wed, 8 Feb 2017 10:21:06 -0800 Subject: [PATCH 4/4] Fixed comment. --- mininet/cli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index e55423ef..67b54e1a 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -471,8 +471,7 @@ def precmd( self, line ): def parseHosts( self, line ): - "expand the hosts to a host list. This is called from the default function." - + "Expand the hosts to a host list. This is called from the default function." hostList = [] first, args, line = self.parseline( line )