Mininet 4

Topology 4 (Static Routing)

R1 <-> R2 (Static Routing )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')
  Link(h1, r1)
  Link(h2, r2)
  Link(r1, r2)
  net.build()
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip addr add 192.168.2.1/24 brd + dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254")
  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 10.0.0.1/24 brd + dev r1-eth1")
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r1.cmd("ip route add 192.168.2.0/24 via 10.0.0.2")
  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 10.0.0.2/24 brd + dev r2-eth1")
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r2.cmd("ip route add 192.168.1.0/24 via 10.0.0.1")
  CLI(net)
  net.stop()

Topology 5

SNAT (Source Network Address Translation )

只改 Source Address

Private IP packet 轉出 要做 translate
當我們 去request 外部resource時 對方才找的到我

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')
  Link(h1, r1)
  Link(h2, r2)
  Link(r1, r2)
  net.build()
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip addr add 22.1.1.1/24 brd + dev h2-eth0")
  h2.cmd("ip route add default via 22.1.1.254")
  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.1.1.1/24 brd + dev r1-eth1")
  r1.cmd("ip route add default via 12.1.1.2")
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  r1.cmd("iptables -t nat -A POSTROUTING -o r1-eth1 -s 192.168.1.0/24 -j MASQUERADE")
  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ip addr add 22.1.1.254/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 12.1.1.2/24 brd + dev r2-eth1")
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  CLI(net)
  net.stop()

iptables

http://www.wnvs.cyc.edu.tw/ebook/iptables&firewall.htm

iptables -t nat -A POSTROUTING -o r1-eth1 -s 192.168.1.0/24 -j MASQUERADE
 --table	-t table	table to manipulate (default: `filter')
 --append  -A chain		Append to chain
 --out-interface -o output name[+]
 --source	-s address[/mask]
 --jump	-j target
				target for rule (may load target extension) {規則的目標(可以加載目標擴展)}

Nat table

Postrouting (轉出)

MASQUERADE -> 做網路轉換

Test

r1-eth0

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

r1-eth1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Practice

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

H1 -> R1

from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  r1 = net.addHost('r1')


  Link(h1, r1)
  net.build()
  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")

  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.0.0.1/24 brd + dev r1-eth1")
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
  CLI(net)
  net.stop()

R1 <-> R2

from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')

  Link(h1, r1)
  Link(r1, r2)
  net.build()

  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")

  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.0.0.1/24 brd + dev r1-eth1")
  
  r1.cmd("ip route add 192.168.2.1 via 12.0.0.2")
  
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ip addr add 12.0.0.2/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth1")
  
  r2.cmd("ip route add 192.168.2.1 via 192.168.2.254")
  
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
 
  CLI(net)
  net.stop()

h1 <-> r1 <->r2 <-> H2

from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')

  Link(h1, r1)
  Link(r1, r2)
  Link(r2, h2)
  
  net.build()


  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")
  
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip addr add 192.168.2.1/24 brd + dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254")

  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.0.0.1/24 brd + dev r1-eth1")
  
  #Routing 
  r1.cmd("ip route add 192.168.2.0/24 via 12.0.0.2")
  
  
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ip addr add 12.0.0.2/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth1")
  
  #Routing
  r2.cmd("ip route add 192.168.1.0/24 via 12.0.0.1")
  
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
 
  CLI(net)
  net.stop()

from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1')
  h2 = net.addHost('h2')
  r1 = net.addHost('r1')
  r2 = net.addHost('r2')
  r3 = net.addHost('r3')
  r4 = net.addHost('r4')
  

  Link(h1, r1)
  Link(r1, r2)
  Link(r2, h2)
  Link(r2, r3)
  Link(r3, r4)
  Link(r4, r1)
  net.build()


  h1.cmd("ifconfig h1-eth0 0")
  h1.cmd("ip addr add 192.168.1.1/24 brd + dev h1-eth0")
  h1.cmd("ip route add default via 192.168.1.254")
  
  h2.cmd("ifconfig h2-eth0 0")
  h2.cmd("ip addr add 192.168.2.1/24 brd + dev h2-eth0")
  h2.cmd("ip route add default via 192.168.2.254")

  r1.cmd("ifconfig r1-eth0 0")
  r1.cmd("ifconfig r1-eth1 0")
  r1.cmd("ip addr add 192.168.1.254/24 brd + dev r1-eth0")
  r1.cmd("ip addr add 12.0.0.1/24 brd + dev r1-eth1")
  r1.cmd("ip addr add 14.0.0.1/24 brd + dev r1-eth2")

  
  #Routing 
  
  r1.cmd("ip route add 192.168.2.0/24 via 12.0.0.2")
  r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  

  r2.cmd("ifconfig r2-eth0 0")
  r2.cmd("ifconfig r2-eth1 0")
  r2.cmd("ifconfig r2-eth2 0")
  r2.cmd("ip addr add 12.0.0.2/24 brd + dev r2-eth0")
  r2.cmd("ip addr add 192.168.2.254/24 brd + dev r2-eth1")
  r2.cmd("ip addr add 23.0.0.1/24 brd + dev r2-eth2")
  
  r2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
  
  #Routing
  r2.cmd("ip route add 192.168.1.0/24 via 23.0.0.2")
  
  
  r3.cmd("ifconfig r3-eth0 0")
  r3.cmd("ifconfig r3-eth1 0")
  r3.cmd("ip addr add 23.0.0.2/24 brd + dev r3-eth0")
  r3.cmd("ip addr add 34.0.0.1/24 brd + dev r3-eth1")
  
  r3.cmd("ip route add 192.168.1.0/24 via 34.0.0.2")
  
  r3.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
  r4.cmd("ifconfig r4-eth0 0")
  r4.cmd("ifconfig r4-eth1 0")
  r4.cmd("ip addr add 34.0.0.2/24 brd + dev r4-eth0")
  r4.cmd("ip addr add 14.0.0.2/24 brd + dev r4-eth1")
  
  r4.cmd("ip route add 192.168.1.0/24 via 14.0.0.1")
  

  r4.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
  
  CLI(net)
  net.stop()

還有一些BUG