O pensamento 2 está correto. Além disso, não é tão difícil traduzir esse script simples em um arquivo correto. Uma boa surpresa aqui é que ip link set mocknet up
é automático. man interfaces
fornece boas informações. Aqui está o arquivo de trabalho:
/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
Não tenho certeza se /bin/ip
ou /sbin/ip
é a maneira "certa" de fazer isso, mas tudo parece funcionar.