故障转移时使用 Java 程序运行“run-parts”进行端口映射的脚本不起作用

故障转移时使用 Java 程序运行“run-parts”进行端口映射的脚本不起作用
#!/bin/sh
# Setup portforwarding on router depending on interface running scripts from a directory 10.0.0.30 being eth0 represented by folder 30 and 10.0.0.31 being wlan0 represented by folder 31
eth0_status=$(cat /sys/class/net/eth0/operstate)
wlan0_status=$(cat /sys/class/net/wlan0/operstate)
if [ "$eth0_status" = "up" ] && [ "$wlan0_status" = "down" ] ;
    then
      CURRDEV=eth0
fi  
if [ "$eth0_status" = "down" ] && [ "$wlan0_status" = "up" ] ;
    then
       CURRDEV=wlan0
fi
if [ "$eth0_status" = "down" ] && [ "$wlan0_status" = "down" ] ;
    then
       CURRDEV=
fi
if [ "$CURRDEV" = "eth0" ] ;
    then
       run-parts "/opt/portmapper/30"
       echo "Current device Ethernet"
elif [ "$CURRDEV" = "wlan0" ] ;
     then
       run-parts "/opt/portmapper/31"
       echo "Current device WiFi"
else
   echo "No changes need to be made to port mappings"
fi

脚本目前可以工作,但为了减少负载,我需要进行变量检查,以防止每次脚本运行时都执行运行部分。我只需要在设备从一个设备切换到另一个设备时运行一次,而不是每次我通过运行脚本声明端口映射时都重复运行。

我在 /opt/portmapper/30|31 目录中有一个端口映射脚本示例,其中目录 30 或 31 分别是以太网设备设置 eth0 或 wlan0。

 #!/bin/sh
    exec java -jar /opt/portmapper/portmapper.jar -lib org.chris.portmapper.router.sbbi.SBBIRouterFactory -add -protocol TCP -internalPort 443 -externalPort 443 -ip 10.0.0.30 ; sleep 2 ;
 fi

我最初是从 /etc/NetworkManager/dispatcher.d/ 中的这个工作脚本中提取的 - else 部分不起作用。我也必须解决这个问题。请注意,此部分有 [[]] 而不是 []。如果我在这里放置单个括号,脚本将不起作用。

 #!/bin/bash
 # Enable/disable wlan0 depending on eth0 and wlan0 current state
 eth0_status=$(cat /sys/class/net/eth0/operstate)
 wlan0_status=$(cat /sys/class/net/wlan0/operstate)
 if [[ "$eth0_status" = "up" ]];
     then
      nmcli con down id "wlan0"
 elif [[ "$wlan0_status" = "down" ]] && [[ "$eth0_status" = "down" ]];
   then
    nmcli con up id "wlan0"
 else
    nmcli nm eth0 on
    nmcli nm wlan0 off
 fi

答案1

基本上,您想要的是在run-parts每次使用的接口发生变化时更改端口映射,避免run-parts在使用的接口自上次run-parts运行以来没有发生变化时运行。

您需要将最后一个正在使用的接口的状态存储在某处,并且每次运行脚本时都以某种方式将当前正在使用的接口与该状态进行比较;此脚本就是这样做的(/opt/mapper/prevdev每次发生更改时它都会更新当前正在使用的接口):

#!/bin/sh
eth0_status=$(cat /sys/class/net/eth0/operstate)
wlan0_status=$(cat /sys/class/net/wlan0/operstate)
touch /opt/portmapper/prevdev # Creates /opt/portmapper/prevdev if it doesn't exist
prevdev=$(cat /opt/portmapper/prevdev)
if [ "$eth0_status" = "up" ] && [ "$wlan0_status" = "down" ]; then # We're currently on Ethernet
    currdev=eth0
fi
if [ "$eth0_status" = "down" ] && [ "$wlan0_status" = "up" ]; then # We're currently on WiFi
    currdev=wlan0
fi
if [ "$currdev" != "$prevdev" ]; then # The interface in use has changed since the last time we checked
    if [ "$currdev" = "eth0" ]; then # We're currently on Ethernet
        run-parts /opt/portmapper/30
        echo 'Current device and portmappings are on Ethernet'
        echo 'eth0' >/opt/portmapper/prevdev # Updates /opt/mapper/prevdev
    else # We're currently on WiFi
        run-parts /opt/portmapper/31
        echo 'Current device and portmappings are on WiFi'
        echo 'wlan0' >/opt/portmapper/prevdev # Updates /opt/mapper/prevdev
    fi
else
    echo 'No changes need to be made to port mappings'
fi

相关内容