Statistics
| Branch: | Tag: | Revision:

mininet / examples / hwintf.py @ 220376b6

History | View | Annotate | Download (1.5 KB)

1 76521ef1 Bob Lantz
#!/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 220376b6 Brian O'Connor
import re, sys
9 76521ef1 Bob Lantz
10
from mininet.cli import CLI
11
from mininet.log import setLogLevel, info, error
12
from mininet.net import Mininet
13 b96e1596 Bob Lantz
from mininet.link import Intf
14 76521ef1 Bob Lantz
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 edf60032 Brandon Heller
               'and is probably in use!\n' )
26 76521ef1 Bob Lantz
        exit( 1 )
27
28
if __name__ == '__main__':
29
    setLogLevel( 'info' )
30
31 220376b6 Brian O'Connor
    # try to get hw intf from the command line; by default, use eth1
32
    intfName = sys.argv[ 1 ] if len( sys.argv ) > 1 else 'eth1'
33
    info( '*** Connecting to hw intf: %s' % intfName )
34
35 b96e1596 Bob Lantz
    info( '*** Checking', intfName, '\n' )
36
    checkIntf( intfName )
37 76521ef1 Bob Lantz
38
    info( '*** Creating network\n' )
39
    net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) )
40
41
    switch = net.switches[ 0 ]
42 b96e1596 Bob Lantz
    info( '*** Adding hardware interface', intfName, 'to switch',
43
          switch.name, '\n' )
44 c8b85746 Bob Lantz
    _intf = Intf( intfName, node=switch )
45 b96e1596 Bob Lantz
46
    info( '*** Note: you may need to reconfigure the interfaces for '
47
          'the Mininet hosts:\n', net.hosts, '\n' )
48 76521ef1 Bob Lantz
49
    net.start()
50
    CLI( net )
51
    net.stop()