脚本来源地址
    路由原理

    /script/single-router.sh

    1. #!/bin/sh
    2. # Author: fasion
    3. # Created time: 2020-10-16 08:28:12
    4. # Last Modified by: fasion
    5. # Last Modified time: 2020-12-10 08:45:42
    6. SELF_PATH=`realpath "$0"`
    7. SCRIPT_DIR_PATH=`dirname "$SELF_PATH"`
    8. source "$SCRIPT_DIR_PATH/nsvm-util.sh"
    9. create-host() {
    10. name="$1"
    11. network="$2"
    12. iface="$3"
    13. mac="$4"
    14. ip="$5"
    15. routes="$6"
    16. create-node "$name"
    17. disable-ipv6-for-nsvm "$name" || exit
    18. connect-node-to-network "$name" "$network" "$iface" "$mac" "$ip"
    19. add-routes-for-nsvm "$node" "$routes"
    20. # exec-in-nsvm "$node" ip route add default via "$gw" || exit
    21. }
    22. #禁止IP V6
    23. echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6 || exit
    24. # 创建第一个交换机
    25. create-network net1 || exit 1
    26. # 创建一个节点,分别是名字/放在那个以太网下/网卡/mac/IP/路由
    27. # 这里不懂的是IP和路由
    28. # 实际的创建过程需要细看👀
    29. create-host ant net1 eth0 '1e:1f:84:08:d2:aa' 192.168.1.2/24 192.168.1.1:192.168.2.0/24 || exit
    30. create-host bee net1 eth0 'e6:04:b1:10:f1:bb' 192.168.1.3/24 192.168.1.1:192.168.2.0/24 || exit
    31. create-host cicada net1 eth0 'ce:ba:ec:ff:fd:cc' 192.168.1.4/24 192.168.1.1:192.168.2.0/24 || exit
    32. # 创建第二个交换机
    33. create-network net2 || exit 1
    34. create-host apple net2 eth0 'b6:f1:81:44:21:11' 192.168.2.2/24 192.168.2.1:192.168.1.0/24 || exit
    35. create-host banana net2 eth0 '22:5b:7c:b3:d6:22' 192.168.2.3/24 192.168.2.1:192.168.1.0/24 || exit
    36. # 创建路由器
    37. create-node router || exit
    38. disable-ipv6-for-nsvm router || exit
    39. # 添加路由条目
    40. connect-node-to-network router net1 eth1 'fa:1c:b2:d0:b0:01' 192.168.1.1/24 || exit
    41. connect-node-to-network router net2 eth2 'ee:21:30:4a:5a:02' 192.168.2.1/24 || exit
    42. echo 'press Ctrl-C to exit'
    43. # zsh
    44. cat