How to add a node and a link during runtime?

[Topology]

H1---S1---H2

 

During runtime, I will add one node (h3) to the topology.

 

h1 can ping h2.

 

Add one host h3.

 

Add a link between s1 and h3

 

Before assign s1-eth3 to s1, we can see that s1 has s1-eth1 and s2-eth2. (s1-eth3 can not be seen.)

assign s1-eth3 to s1

Set the ip address for h3

h1 can ping h3.

 

[topology]

H1---R1----H2

 

Mininet script:1.py

#!/usr/bin/python

 

from mininet.net import Mininet

from mininet.node import Host, Node

from mininet.cli import CLI

from mininet.link import TCLink, Intf

from mininet.log import setLogLevel, info

from subprocess import call

 

def myNetwork():

 

    net = Mininet()

    r1 = net.addHost('r1')

    r1.cmd('sysctl -w net.ipv4.ip_forward=1')

 

    h2 = net.addHost('h2', cls=Host, ip='192.168.2.1/24')

    h1 = net.addHost('h1', cls=Host, ip='192.168.1.1/24')

 

    info( '*** Add links\n')

    net.addLink(h1, r1)

    net.addLink(h2, r1)

 

 

    info( '*** Starting network\n')

    net.build()

    h1.cmd("ip route add default via 192.168.1.254")

    h2.cmd("ip route add default via 192.168.2.254")

    r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")

    r1.cmd("ip addr add 192.168.2.254/24 brd + dev r1-eth1")

  

    CLI(net)

    net.stop()

 

if __name__ == '__main__':

    setLogLevel( 'info' )

    myNetwork()

 

 

Add one node (h3) and one link between r1 and h3

 

Set the ip addresses for h3 and r1.

 

 

h1 can ping h3

 

References.

https://www.cnblogs.com/qq952693358/p/5882931.html

 

Dr. Chih-Heng Ke (smallko@gmail.com)

Department of Computer Science and Information Engineering,

National Quemoy University, Kinmen, Taiwan.