gpt4 book ai didi

linux - 在单个 Linux 机器上模拟网络跃点

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:00 26 4
gpt4 key购买 nike

我正在尝试模拟许多网络跃点,类似于 Star Wars Traceroute .作者提供了脚本here ,但是它是通过使用 vrf 而不是 Linux 为 Cisco 路由器设计的。

这是我目前的尝试;

echo 1 > /proc/sys/net/ipv4/ip_forward

ifconfig br0 172.16.100.1 netmask 255.255.255.0
ifconfig br0:1 172.16.101.1 netmask 255.255.255.0
ifconfig br0:2 172.16.102.1 netmask 255.255.255.0
ifconfig br0:2 172.16.103.1 netmask 255.255.255.0

ip rule add iif br0 table 100
ip rule add iif br0:1 table 101
ip rule add iif br0:2 table 102

ip route add default table 100 dev 172.16.101.1
ip route add default table 101 dev 172.16.102.1
ip route add default table 102 dev 172.16.103.1

ping -I br0 172.16.103.2

我尝试使用 Source Based Routing 来完成同样的事情,它显然再现了 vrf 的效果,参见 here ,但附加 tcpdump -i br0 显示根本没有流量,而是在 lo0 上拾取数据包。

替代解决方案似乎包括修改源 IP 和使用 iptables 的其他技巧,感觉很脏,所以我不确定我的工作重点在哪里。

任何提示/指针将不胜感激

最佳答案

我将这个想法变成了一个脚本,供那些仍然在尝试使用 Linux 制作自己的星球大战追踪路线时发现这个问题的人使用。

#!/bin/bash
#
# traceroute fun with linux namespaces
# each namespace is basically a router we connect
# to each other using fancy /32 networking
#
# scott nicholas <scott@nicholas.one> 2018-10-27
#

# how many levels deep are we going?
depth=16
prefix=192.168.99.

# can change function easily or just pre-populate ip array
calcip() { printf '%s%d' "$prefix" $((63 + i)); }

# instead of special casing things, if we bind init's netns into a name
# all of the code can use "-n ns"
touch /var/run/netns/default
mount --bind /proc/1/ns/net /var/run/netns/default

ns[0]=default if[0]=root ip[0]=${prefix}1

for ((i = 1; i <= depth; i++)); do
ns[i]=hop$i if[i]=hop$i ip[i]=$(calcip $i)

ip netns add "${ns[i]}"
# interfaces are named by whom is on the other side
# so it's kinda flip-flopped looking.
ip -n "${ns[i-1]}" link add "${if[i]}" type veth peer name "${if[i-1]}" \
netns "${ns[i]}"

ip -n "${ns[i]}" a a "${ip[i]}"/32 dev "${if[i-1]}"

# interfaces must be up before adding routes
ip -n "${ns[i-1]}" link set "${if[i]}" up
ip -n "${ns[i ]}" link set lo up
ip -n "${ns[i ]}" link set "${if[i-1]}" up

ip -n "${ns[i-1]}" route add "${ip[i ]}" dev "${if[i]}"
ip -n "${ns[i ]}" route add "${ip[i-1]}" dev "${if[i-1]}"
ip -n "${ns[i ]}" route add default via "${ip[i-1]}"

# tell everyone above my parent that i'm down here in this mess
for ((j = i - 2; j >= 0; j--)); do
ip -n "${ns[j]}" route add "${ip[i]}" via "${ip[j+1]}"
done
done

关于linux - 在单个 Linux 机器上模拟网络跃点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34320429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com