carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | This example shows how to add an interface (for example a real |
| 5 | hardware interface) to a network after the network is created. |
| 6 | """ |
| 7 | |
| 8 | import re |
| 9 | |
| 10 | from mininet.cli import CLI |
| 11 | from mininet.log import setLogLevel, info, error |
| 12 | from mininet.net import Mininet |
| 13 | from mininet.link import Intf |
| 14 | from mininet.topolib import TreeTopo |
| 15 | from mininet.util import quietRun |
| 16 | |
| 17 | def checkIntf( intf ): |
| 18 | "Make sure intf exists and is not configured." |
| 19 | if ( ' %s:' % intf ) not in quietRun( 'ip link show' ): |
| 20 | error( 'Error:', intf, 'does not exist!\n' ) |
| 21 | exit( 1 ) |
| 22 | ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) ) |
| 23 | if ips: |
| 24 | error( 'Error:', intf, 'has an IP address,' |
| 25 | 'and is probably in use!\n' ) |
| 26 | exit( 1 ) |
| 27 | |
| 28 | if __name__ == '__main__': |
| 29 | setLogLevel( 'info' ) |
| 30 | |
| 31 | intfName = 'eth1' |
| 32 | info( '*** Checking', intfName, '\n' ) |
| 33 | checkIntf( intfName ) |
| 34 | |
| 35 | info( '*** Creating network\n' ) |
| 36 | net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) ) |
| 37 | |
| 38 | switch = net.switches[ 0 ] |
| 39 | info( '*** Adding hardware interface', intfName, 'to switch', |
| 40 | switch.name, '\n' ) |
| 41 | _intf = Intf( intfName, node=switch ) |
| 42 | |
| 43 | info( '*** Note: you may need to reconfigure the interfaces for ' |
| 44 | 'the Mininet hosts:\n', net.hosts, '\n' ) |
| 45 | |
| 46 | net.start() |
| 47 | CLI( net ) |
| 48 | net.stop() |