如何使 ip 命令持续存在

如何使 ip 命令持续存在

我有一个脚本(问题底部),它添加一个虚拟链接,向该链接添加一些地址,并向该链接添加一些路由。

如何使ip linkip addr、 和ip route命令持续存在?

我使用的是 Debian Stretch,已安装 NetworkManager。

想法1:

如果我把这个文件放进去,/etc/network/if-up.d/它可能会在机器启动时被调用。 /etc/NetworkManager/dispatcher.d/01ifupdown似乎指向这个目录。

更新:我尝试过,这不是一个好主意

想法2:

我可能需要将其翻译成不同的配置语言并将其放入 中/etc/network/interfaces.d/,但到目前为止我看到的配置此文件的示例假设设备存在。我还没有找到相当于ip link add mocknet type dummy.

该文件依赖于auto行开头的关键字来标识物理接口 ( auto mocknet),但mocknet尚不存在。

工作脚本

ip link add mocknet type dummy

ip addr add 10.10.1.1/24 dev mocknet
ip addr add 10.10.2.1/24 dev mocknet
ip addr add 10.10.3.1/24 dev mocknet
ip addr add 10.10.4.1/24 dev mocknet
ip addr add 10.10.5.1/24 dev mocknet
ip addr add 10.10.6.1/24 dev mocknet
ip addr add 10.10.7.1/24 dev mocknet
ip addr add 10.10.8.1/24 dev mocknet
ip addr add 10.10.9.1/24 dev mocknet

ip link set mocknet up

ip route del 10.10.1.0/24 
ip route del 10.10.2.0/24 
ip route del 10.10.3.0/24 
ip route del 10.10.4.0/24 
ip route del 10.10.5.0/24 
ip route del 10.10.6.0/24 
ip route del 10.10.7.0/24 
ip route del 10.10.8.0/24 
ip route del 10.10.9.0/24 

ip route add 10.10.1.0/24 dev mocknet proto kernel scope link src 10.10.1.1 metric 6000
ip route add 10.10.2.0/24 dev mocknet proto kernel scope link src 10.10.2.1 metric 6000
ip route add 10.10.3.0/24 dev mocknet proto kernel scope link src 10.10.3.1 metric 6000
ip route add 10.10.4.0/24 dev mocknet proto kernel scope link src 10.10.4.1 metric 6000
ip route add 10.10.5.0/24 dev mocknet proto kernel scope link src 10.10.5.1 metric 6000
ip route add 10.10.6.0/24 dev mocknet proto kernel scope link src 10.10.6.1 metric 6000
ip route add 10.10.7.0/24 dev mocknet proto kernel scope link src 10.10.7.1 metric 6000
ip route add 10.10.8.0/24 dev mocknet proto kernel scope link src 10.10.8.1 metric 6000
ip route add 10.10.9.0/24 dev mocknet proto kernel scope link src 10.10.9.1 metric 6000

答案1

想法2是正确的。此外,将这个简单的脚本转换为正确的文件并不困难。这里的一个惊喜是它ip link set mocknet up是自动的。 man interfaces提供了一些很好的信息。这是工作文件:

/etc/network/interfaces.d/mocknet:

# Mocknet: 
#    A mock network meant to replace parts of the real one if they don't exist.
# 
# We are creating a network sink called mocknet. It's a dummy, so data sent to 
# this is ignored. This is useful for when we want to run the simulation without
# all other machines connected. If a machine doesn't exist, then this prevents 
# that packet from being sent to the default route and spamming the LAN.

auto mocknet
iface mocknet inet manual
#
#   This creates the dummy interface.  Dummy means that data sent here will be 
#   ignored.
    pre-up /bin/ip link add mocknet type dummy
#
#   Here we add all of the IP adresses that we might have as host on the FFS.  
#   This guarantees that we can bind to these addresses for Rx and helps us make 
#   a route for Tx
    up /bin/ip addr add 10.10.1.1/24 dev mocknet
    up /bin/ip addr add 10.10.2.1/24 dev mocknet
    up /bin/ip addr add 10.10.3.1/24 dev mocknet
    up /bin/ip addr add 10.10.4.1/24 dev mocknet
    up /bin/ip addr add 10.10.5.1/24 dev mocknet
    up /bin/ip addr add 10.10.6.1/24 dev mocknet
    up /bin/ip addr add 10.10.7.1/24 dev mocknet
    up /bin/ip addr add 10.10.8.1/24 dev mocknet
    up /bin/ip addr add 10.10.9.1/24 dev mocknet
#
#   The previous line created some default routes. Those routes are super high 
#   priority (metric=0) so they override real connections if they exist. We 
#   don't want that so here we need to delete the default routes and then add 
#   good versions. Now if a packet is sent to something in this ip range it goes 
#   to mocknet.  `metric 6000` sets the route priority super low (a bigger 
#   number is lower priority so if it conflicts with a real device, that real 
#   device will take priority and data will be sent and not ignored.
    post-up /bin/ip route del 10.10.1.0/24 && /bin/ip route add 10.10.1.0/24 dev mocknet proto kernel scope link src 10.10.1.1 metric 6000
    post-up /bin/ip route del 10.10.2.0/24 && /bin/ip route add 10.10.2.0/24 dev mocknet proto kernel scope link src 10.10.2.1 metric 6000
    post-up /bin/ip route del 10.10.3.0/24 && /bin/ip route add 10.10.3.0/24 dev mocknet proto kernel scope link src 10.10.3.1 metric 6000
    post-up /bin/ip route del 10.10.4.0/24 && /bin/ip route add 10.10.4.0/24 dev mocknet proto kernel scope link src 10.10.4.1 metric 6000
    post-up /bin/ip route del 10.10.5.0/24 && /bin/ip route add 10.10.5.0/24 dev mocknet proto kernel scope link src 10.10.5.1 metric 6000
    post-up /bin/ip route del 10.10.6.0/24 && /bin/ip route add 10.10.6.0/24 dev mocknet proto kernel scope link src 10.10.6.1 metric 6000
    post-up /bin/ip route del 10.10.7.0/24 && /bin/ip route add 10.10.7.0/24 dev mocknet proto kernel scope link src 10.10.7.1 metric 6000
    post-up /bin/ip route del 10.10.8.0/24 && /bin/ip route add 10.10.8.0/24 dev mocknet proto kernel scope link src 10.10.8.1 metric 6000
    post-up /bin/ip route del 10.10.9.0/24 && /bin/ip route add 10.10.9.0/24 dev mocknet proto kernel scope link src 10.10.9.1 metric 6000
#
#   This cleans up the interface so it doesn't fail if we try to 
#   systemctl restart networking.
    post-down /bin/ip link del mocknet

我不确定这样做/bin/ip是否是“正确”的方法,但一切似乎都有效。/sbin/ip

相关内容