如何在OSX上更改路由表以分割流量


1

如何在OSX上分割流量,以便只将某个特定子网路由到以太网接口,其余子网路由到Wifi?

我已经使用这个脚本超过1年半,但今天它停止了工作。它所做的就是删除默认网关并将所需的网关设置为默认网关(即en2 = wifi),并为每个子网添加记录,告诉它将其路由到所需的“隧道”接口(即en0 = ethernet)。

#! /usr/bin/env zsh

if (( EUID != 0 )); then
    echo "Please, run this command with sudo" >&2
    exit 1
fi

if [ "$#" -ne "2" ]; then
    echo "Usage: $0 tunnel_interface default_interface" >&2
    exit 1
fi    

TUNNEL_INTERFACE=$1
DEFAULT_INTERFACE=$2

GATEWAY=$(netstat -nrf inet | grep default | grep $DEFAULT_INTERFACE | awk '{print $2}')

if [ "$GATEWAY" = "" ]; then
    read "GATEWAY?Enter gateway IP address: "
fi

echo "Resetting routes with gateway => $GATEWAY"
echo

route -n delete default -ifscope $DEFAULT_INTERFACE 1>/dev/null
route -n delete -net default -interface $TUNNEL_INTERFACE 1>/dev/null
route -n add -net default $GATEWAY 1>/dev/null

for subnet in 10.89 10.153 10.162 10.168 172.24.0.0/13
do
    route -n add -net $subnet -interface $TUNNEL_INTERFACE 1>/dev/null 2>&1
done

echo "google.cz -> $(route get google.cz | grep interface | sed 's/^ *//')"
echo "r5d00 -> $(route get r5d00 | grep interface | sed 's/^ *//')"

我像这样运行这个脚本

$ sudo tunnel-ips.sh no en0 en2
Password:
Resetting routes with gateway => 172.20.10.1

google.cz -> interface: en2
r5d00 -> interface: en0

我通过Wifi连接到我的iPhone个人热点,它为我分配了IP 172.20.10.4和掩码255.255.255.240。r5d00服务器的IP设置为172.24.146.155,与172.24.0.0/13匹配。

我的路由表如下所示:$ netstat -rn路由表

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            172.20.10.1        UGSc            3        0     en2
10.89/16           link#4             UCSc            1        0     en0
10.153/16          link#4             UCSc            1        0     en0
10.162/21          link#4             UCS             6        0     en0
10.162/16          link#4             UCSc            1        0     en0
10.162.0.1/32      link#4             UCS             2        0     en0
10.162.0.1         0:0:c:9f:f0:1      UHLWIi          2        4     en0   1167
10.162.0.241       0:20:4a:e6:5c:2c   UHLWIi          1        0     en0   1149
10.162.0.242       0:20:4a:54:87:a0   UHLWIi          1        0     en0   1079
10.162.0.243       0:20:4a:d7:d8:3e   UHLWIi          1        0     en0   1132
10.162.1.22        0:1e:b:ed:21:b8    UHLWIi          1       18     en0   1136
10.162.7.241/32    link#4             UCS             1        0     en0
10.167/16          link#4             UCSc            1        0     en0
10.168/16          link#4             UCSc            1        0     en0
10.255/16          link#4             UCSc            1        0     en0
127                127.0.0.1          UCS             1        0     lo0
127.0.0.1          127.0.0.1          UH              5     2329     lo0
169.254            link#4             UCS             1        0     en0
169.254            link#5             UCSI            1        0     en2
172.20.10/28       link#5             UCS             1        0     en2
172.20.10.1/32     link#5             UCS             2        0     en2
172.20.10.1        36:e2:fd:74:7b:64  UHLWIir         5       11     en2   1067
172.20.10.4/32     link#5             UCS             1        0     en2
172.24/13          link#4             UCSc            7        0     en0
255.255.255.255/32 link#4             UCS             1        0     en0
255.255.255.255/32 link#5             UCSI            1        0     en2

Internet6:
Destination                             Gateway                         Flags         Netif Expire
::1                                     ::1                             UHL             lo0
fe80::%lo0/64                           fe80::1%lo0                     UcI             lo0
fe80::1%lo0                             link#1                          UHLI            lo0
fe80::%awdl0/64                         link#7                          UCI           awdl0
fe80::a4a8:81ff:fe02:a44d%awdl0         a6:a8:81:2:a4:4d                UHLI            lo0
fe80::%utun0/64                         fe80::db39:e7b8:6234:7841%utun0 UcI           utun0
fe80::db39:e7b8:6234:7841%utun0         link#9                          UHLI            lo0
ff01::%lo0/32                           ::1                             UmCI            lo0
ff01::%awdl0/32                         link#7                          UmCI          awdl0
ff01::%utun0/32                         fe80::db39:e7b8:6234:7841%utun0 UmCI          utun0
ff02::%lo0/32                           ::1                             UmCI            lo0
ff02::%awdl0/32                         link#7                          UmCI          awdl0
ff02::%utun0/32                         fe80::db39:e7b8:6234:7841%utun0 UmCI          utun0

当我ping r5d00时,它说“没有主机路由”

$ ping r5d00     
PING r5d00.cezdata.corp (172.24.146.155): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
ping: sendto: No route to host
Request timeout for icmp_seq 4
ping: sendto: Host is down
Request timeout for icmp_seq 5
ping: sendto: Host is down
Request timeout for icmp_seq 6
ping: sendto: Host is down
Request timeout for icmp_seq 7
ping: sendto: Host is down
Request timeout for icmp_seq 8
ping: sendto: Host is down
Request timeout for icmp_seq 9
ping: sendto: Host is down
Request timeout for icmp_seq 10

我下载了Wireshark并记录了流量,但ICMP数据包无处可寻(当我禁用Wifi并重置路由表时,它会被记录并且ping成功)。看起来IP与路由表记录不匹配,但是当我通过路由get检查它时,它表示它通过en0路由(这是正确的)。

$ route get r5d00
   route to: r5d00
destination: r5d00
  interface: en0
      flags: <UP,HOST,REJECT,DONE,LLINFO,WASCLONED,IFSCOPE,IFREF>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500      -138 

就像我说的那样,它昨天正在运作,但今天却没有。我没有下载任何更新或任何东西。AFAIK是我的Mac上的问题,而不是内部网络,因为没有其他人有问题。

Answers:


0

单个ping: sendto: No route to host消息首先表明测试期间发生了一些变化。根据您的路由表172.24.0.0/13,直接连接到Link#4,这意味着到达该主机不应该需要路由,甚至不生成该消息。

随后,除第一个消息外,其他消息反映了路由表。在ping: sendto: Host is down表明主机是被直接连接在网络上,并且该主机或它的代理并没有回答ARP请求。

尝试route monitor在重复测试时在另一个窗口中运行,以及在控制台中查看任何接口转换或其他网络更改。


你好,抱歉我没有回答,我一定错过了通知。我能够改变脚本以使其工作。谢谢您的帮助。
Hologos
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.