优化脚本

lowmem
Matthew 3 weeks ago
parent 60989b5c40
commit 8612c66ed3

@ -1,46 +1,131 @@
#!/system/bin/sh #!/system/bin/sh
while [ $WAITED -lt $MAX_WAIT ]; do # ==============================================
# Configuration parameters - modify as needed
# ==============================================
ETH_IP="192.168.68.91" # Ethernet IP address
ETH_NETMASK="24" # Subnet mask (CIDR format)
ETH_NETWORK="192.168.68.0" # Network address
ETH_BROADCAST="192.168.68.255" # Broadcast address
ETH_GATEWAY="192.168.68.1" # Default gateway
ROUTE_TABLE="20" # Routing table number
MAX_INIT_WAIT=150 # Maximum seconds to wait for ethernet interface
MAX_UP_WAIT=10 # Maximum seconds to wait for interface to come UP
MAX_ROUTE_WAIT=5 # Maximum seconds to wait for routing rules
# For debugging only - comment out in production
# set -x
# Record script start time
SCRIPT_START=$(date +%s)
# Cleanup function - handles unexpected interruptions
cleanup() {
echo "Script interrupted, cleaning up..." >&2
# Add additional cleanup code here if needed
exit 1
}
trap cleanup INT TERM
# Only configure rp_filter for eth0 interface
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter 2>/dev/null || true
# Wait for eth0 interface to appear
WAITED=0
while [ $WAITED -lt $MAX_INIT_WAIT ]; do
if [ -d "/sys/class/net/eth0" ]; then if [ -d "/sys/class/net/eth0" ]; then
echo "eth0 found" echo "eth0 found after $WAITED seconds"
break break
fi fi
echo "Wait eth0... ($WAITED/$MAX_WAIT)" echo "Wait eth0... ($WAITED/$MAX_INIT_WAIT)"
sleep 1 sleep 0.1
WAITED=$((WAITED+1)) WAITED=$((WAITED+1))
done done
# 检查eth0是否存在 # Check if eth0 exists
if ! [ -d "/sys/class/net/eth0" ]; then if ! [ -d "/sys/class/net/eth0" ]; then
echo "Error: eth0 not exists" echo "Error: eth0 not exists" >&2
exit 1 exit 1
fi fi
# Check physical connection status
if [ -f "/sys/class/net/eth0/carrier" ]; then
CARRIER=$(cat /sys/class/net/eth0/carrier)
echo "Physical connection status: $CARRIER (1=connected, 0=disconnected)"
if [ "$CARRIER" != "1" ]; then
echo "Warning: Ethernet physical connection may have issues, please check the cable" >&2
fi
fi
# Clear previous configuration
/system/bin/ip link set eth0 down /system/bin/ip link set eth0 down
/system/bin/ip addr flush dev eth0 /system/bin/ip addr flush dev eth0
/system/bin/ip addr add 192.168.68.91/24 broadcast 192.168.68.255 dev eth0 /system/bin/ip route flush dev eth0
/system/bin/ip route flush table $ROUTE_TABLE
/system/bin/ip rule del to $ETH_NETWORK/$ETH_NETMASK 2>/dev/null || true
# Configure IP address
/system/bin/ip addr add $ETH_IP/$ETH_NETMASK broadcast $ETH_BROADCAST dev eth0
# Enable interface and wait for UP
/system/bin/ip link set eth0 up /system/bin/ip link set eth0 up
/system/bin/sleep 1
/system/bin/ip route delete 192.168.68.0/24 table 20 # Use loop to wait for interface UP instead of fixed sleep
/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20 WAITED=0
while [ $WAITED -lt $MAX_UP_WAIT ]; do
IF_STATUS=$(/system/bin/ip link show eth0 | grep -c "state UP")
if [ "$IF_STATUS" = "1" ]; then
echo "Interface is UP after $WAITED seconds"
break
fi
echo "Waiting for interface UP... ($WAITED/$MAX_UP_WAIT)"
sleep 0.5
WAITED=$((WAITED+1))
done
if [ "$IF_STATUS" != "1" ]; then
echo "Warning: Interface did not come UP within expected time, attempting to enable again" >&2
/system/bin/ip link set eth0 up
sleep 1
fi
# First add to main routing table
/system/bin/ip route add $ETH_NETWORK/$ETH_NETMASK dev eth0 proto static scope link
# Then add to specified routing table
/system/bin/ip route add $ETH_NETWORK/$ETH_NETMASK dev eth0 proto static scope link table $ROUTE_TABLE
ADD_ROUTE_STATUS=$? ADD_ROUTE_STATUS=$?
if [ $ADD_ROUTE_STATUS -eq 0 ]; then if [ $ADD_ROUTE_STATUS -eq 0 ]; then
echo "路由添加命令成功执行" echo "Add route successfully"
else else
echo "路由添加命令失败,状态码: $ADD_ROUTE_STATUS" echo "Failed to add route: $ADD_ROUTE_STATUS" >&2
exit $ADD_ROUTE_STATUS
fi fi
/system/bin/ip route flush cache # Only clear ARP and neighbor cache for eth0
/system/bin/ip rule del to 192.168.68.0/24 /system/bin/ip neigh flush dev eth0
/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000
/system/bin/ip route flush cache
if /system/bin/ip rule | grep -q "192.168.68.0/24"; then # Add routing rules - only flush cache once after rule is added
echo "路由配置成功" /system/bin/ip rule add from all to $ETH_NETWORK/$ETH_NETMASK lookup $ROUTE_TABLE prio 1000
exit 0 /system/bin/ip route flush cache dev eth0
else
echo "路由配置失败" # Only enable forwarding for eth0 interface
exit 1 echo 1 > /proc/sys/net/ipv4/conf/eth0/forwarding 2>/dev/null || true
# Wait for routing rules to take effect - using loop check instead of fixed wait
WAITED=0
while [ $WAITED -lt $MAX_ROUTE_WAIT ]; do
if /system/bin/ip rule | grep -q "$ETH_NETWORK/$ETH_NETMASK"; then
echo "Routing rules are now effective after $WAITED seconds"
break
fi fi
echo "Waiting for routing rules to take effect... ($WAITED/$MAX_ROUTE_WAIT)"
sleep 0.5
WAITED=$((WAITED+1))
done
# Display execution time
SCRIPT_END=$(date +%s)
TOTAL_TIME=$((SCRIPT_END - SCRIPT_START))
echo "Total script execution time: $TOTAL_TIME seconds"
exit 0

Loading…
Cancel
Save