在上面的脚本中增加这一个操作应该怎么添加:ip route add default dev ppp0 table local_network
时间: 2023-12-10 08:42:41 浏览: 120
你可以使用以下命令将该操作添加到脚本中:
```
echo "ip route add default dev ppp0 table local_network" >> /etc/ppp/ip-up
```
这将在 `/etc/ppp/ip-up` 文件的末尾添加一行,该行将在每次成功建立 PPP 连接时执行。注意,这将只在特定网络接口(`ppp0`)上添加默认路由,以便将流量路由到 `local_network` 表中。
相关问题
在下面脚本文件中,为什么不执行ip route add default dev ppp0 table local_network#!/bin/sh pppd call quectel-ppp & ip route add default dev ppp0 table local_network
这脚本文件是启动Quectel Modem的PPPD拨号服务,并且添加了一条默认路由表项。但是它没有执行`ip route add default dev ppp0 table local_network`指令,这可能是因为没有定义`local_network`表。
如果要使用此命令,需要先定义一个名为“local_network”的表。可以通过以下命令来定义该表:
```
sudo echo "200 local_network" >> /etc/iproute2/rt_tables
```
然后再执行`ip route add default dev ppp0 table local_network`命令,这样就可以将默认路由添加到该表中了。
#!/bin/sh # Set the name of the primary network interface primary_interface="eth0" # Set the name of the secondary network interface secondary_interface="wlan0" # Set the IP address range of the local network local_network="192.168.1.0/24" primary_interface_table="eth0_table" # Keep running the script indefinitely while true; do # Check if the primary interface is up and connected to the local network if ip addr show $primary_interface up | grep -q $local_network; then # Add a new routing table for the primary interface echo "200 $primary_interface_table" >> /etc/iproute2/rt_tables # Add default route for primary interface to the new routing table primary_gateway=$(ip route show | grep "default" | grep "$primary_interface" | awk '{print $3}') ip route add default via $primary_gateway dev $primary_interface table $primary_interface_table # Add a rule to route all traffic from primary interface through the new routing table primary_ip=$(ip addr show $primary_interface | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) ip rule add from $primary_ip table $primary_interface_table # Remove any existing default route for the secondary interface ip route del default dev $secondary_interface else # Remove any existing routing table for the primary interface existing_table=$(grep -n " $primary_interface_table" /etc/iproute2/rt_tables | cut -f1 -d:) if [ ! -z "$existing_table" ]; then sed -i "${existing_table}d" /etc/iproute2/rt_tables ip route flush table $primary_interface_table ip rule del table $primary_interface_table fi # Add default route for the secondary interface secondary_gateway=$(ip route show | grep "default" | grep "$secondary_interface" | awk '{print $3}') ip route add default via $secondary_gateway dev $secondary_interface fi # Wait for 1 second before checking the network interfaces again sleep 1 done 运行上述脚本,提示ip: RTNETLINK answers: File exists, 请问如何修正
这个错误通常是因为重复执行了相同的命令,导致 ip 路由表中已经存在相同的路由规则。你可以尝试先手动删除相应的路由规则和路由表,然后再重新执行脚本。具体操作如下:
1. 删除路由规则和路由表:
```
ip rule del from $primary_ip table $primary_interface_table
ip route flush table $primary_interface_table
sed -i "/^200 $primary_interface_table$/d" /etc/iproute2/rt_tables
```
2. 重新执行脚本。
如果还是出现相同的错误,你可以尝试重启网络服务,然后再执行脚本:
```
systemctl restart networking.service
```
阅读全文