#!/system/bin/sh # ============================================== # 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 SCRIPT_PATH="$0" # 确保路径是绝对路径 case "$SCRIPT_PATH" in /*) ;; # 已经是绝对路径 *) SCRIPT_PATH="$PWD/$SCRIPT_PATH" ;; esac SCRIPT_DIR=$(dirname "$SCRIPT_PATH") echo "Script directory detected as: $SCRIPT_DIR" # 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 echo "eth0 found after $WAITED seconds" break fi echo "Wait eth0... ($WAITED/$MAX_INIT_WAIT)" sleep 0.1 WAITED=$((WAITED+1)) done # Check if eth0 exists if ! [ -d "/sys/class/net/eth0" ]; then echo "Error: eth0 not exists" >&2 exit 1 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 addr flush 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 physical layer with ethtool (while interface is DOWN) if [ -x "$SCRIPT_DIR/ethtool" ]; then echo "Using ethtool from script directory: $SCRIPT_DIR/ethtool" "$SCRIPT_DIR/ethtool" -s eth0 speed 10 duplex full autoneg off # 其次尝试使用原路径 elif [ -x "/data/data/com.xypower.mpapp/files/ethtool" ]; then echo "Configuring eth0 to 10Mbps full duplex..." /data/data/com.xypower.mpapp/files/ethtool -s eth0 speed 10 duplex full autoneg off else echo "Warning: ethtool not found, falling back to sysfs configuration" >&2 # Try sysfs configuration as fallback if [ -f "/sys/class/net/eth0/speed" ]; then echo "off" > /sys/class/net/eth0/autoneg 2>/dev/null || true echo "10" > /sys/class/net/eth0/speed 2>/dev/null || true echo "full" > /sys/class/net/eth0/duplex 2>/dev/null || true fi fi # 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 # Use loop to wait for interface UP instead of fixed sleep WAITED=0 while [ $WAITED -lt $MAX_UP_WAIT ]; do IF_STATUS=$(/system/bin/ip link show eth0 | grep -c ",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=$? if [ $ADD_ROUTE_STATUS -eq 0 ]; then echo "Add route successfully" else echo "Failed to add route: $ADD_ROUTE_STATUS" >&2 fi # Only clear ARP and neighbor cache for eth0 /system/bin/ip neigh flush dev eth0 # Add routing rules - only flush cache once after rule is added /system/bin/ip rule add from all to $ETH_NETWORK/$ETH_NETMASK lookup $ROUTE_TABLE prio 1000 /system/bin/ip route flush cache dev eth0 # Only enable forwarding for eth0 interface 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 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