From 76521ef1d4fbf114ca65cdbeb4fe5b16eda777c9 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Mon, 25 Oct 2010 21:53:29 -0700 Subject: [PATCH] Added example of adding hw interface to mininet. --- examples/hwintf.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 examples/hwintf.py diff --git a/examples/hwintf.py b/examples/hwintf.py new file mode 100755 index 0000000..d824729 --- /dev/null +++ b/examples/hwintf.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +""" +This example shows how to add an interface (for example a real +hardware interface) to a network after the network is created. +""" + +import re + +from mininet.cli import CLI +from mininet.log import setLogLevel, info, error +from mininet.net import Mininet +from mininet.topolib import TreeTopo +from mininet.util import quietRun + +def checkIntf( intf ): + "Make sure intf exists and is not configured." + if ( ' %s:' % intf ) not in quietRun( 'ip link show' ): + error( 'Error:', intf, 'does not exist!\n' ) + exit( 1 ) + ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) ) + if ips: + error( 'Error:', intf, 'has an IP address,' + 'and is probably in use!\n' ) + exit( 1 ) + +if __name__ == '__main__': + setLogLevel( 'info' ) + + newIntf = 'veth1' + info( '*** Checking', newIntf, '\n' ) + checkIntf( newIntf ) + + info( '*** Creating network\n' ) + net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) ) + + switch = net.switches[ 0 ] + info( '*** Adding', newIntf, 'to switch', switch.name, '\n' ) + switch.addIntf( newIntf ) + + net.start() + CLI( net ) + net.stop()