Я пытаюсь выполнить команду при запуске systemd, пока здесь нет проблем, но команда не выполняется так, как ожидалось.
Это часть кода, которая не будет работать так, как я хочу:
#!/bin/bash
set -x
############ CONFIGURATION ############
OWRT_UNAME="root" # username to connect to openwrt
# for the ssh password generate a key pair
OWRT_IP_ADDR="10.0.0.1" # Router address to connect
#############
QBITTORRENT_HOST="localhost" # qbittorrent machine?
QBITTORRENT_USERNAME="admin" # Username for qbittorrent remote machine
QBITTORRENT_PASSWORD="adminadmin" # Password for qbittorrent remote machine
QBITTORRENT_PORT="8081" # Port for qbittorrent webui
############ CONFIGURATION ############
LOG_ENABLE=0
LOG_PATH="/tmp"
if [[ $LOG_ENABLE == 1 ]]; then
if [[ ! -d $LOG_PATH ]]; then
mkdir $LOG_PATH
fi
if [[ ! -w "$LOG_PATH/${0##*/}.log" ]]; then
touch "$LOG_PATH/${0##*/}.log"
fi
fi
[[ $LOG_ENABLE == 1 ]] && echo "########## $(date) ##########" >> "$LOG_PATH/${0##*/}.log"
QBT_BIN="$(command -v qbt)"
SSH_BIN="$(command -v ssh)"
JQ_BIN="$(command -v jq)"
if [[ -z "$QBT_BIN" ]]; then # If no qbt found
[[ "$LOG_ENABLE" == "1" ]] && echo "Error, no qbt found, please install it from https://github.com/fedarovich/qbittorrent-cli" >> "$LOG_PATH/${0##*/}.log"
exist
fi
if [[ -z "$SSH_BIN" ]]; then # If no qbt found
[[ $LOG_ENABLE == 1 ]] && echo "Error, no ssh found, please install it" >> "$LOG_PATH/${0##*/}.log"
exist
fi
REMOTE_PIA_PORT=$($SSH_BIN $OWRT_UNAME@$OWRT_IP_ADDR 'cat /tmp/pia-port-forward')
if [[ -n $REMOTE_PIA_PORT ]]; then # I get the pia port
#ACTUAL_QBITTORRENT_PORT=$($QBT_BIN server settings connection --username "$QBITTORRENT_USERNAME" --password "$QBITTORRENT_PASSWORD" --url "http://${QBITTORRENT_HOST}:$QBITTORRENT_PORT" | awk '{print $9}') # get the actual qBittorrent port
ACTUAL_QBITTORRENT_PORT=$($QBT_BIN server settings connection --username "$QBITTORRENT_USERNAME" --password "$QBITTORRENT_PASSWORD" --url "http://${QBITTORRENT_HOST}:$QBITTORRENT_PORT" --format json | $JQ_BIN '.ListenPort') # get the actual qBittorrent port
if [[ $ACTUAL_QBITTORRENT_PORT == $REMOTE_PIA_PORT ]]; then # pia port is the same that that the qBittorrent port
[[ $LOG_ENABLE == 1 ]] && echo "qBittorrent port is already the good one, nothing to do, see you later!" >> "$LOG_PATH/${0##*/}.log"
else # qBittorrent use a different port
[[ $LOG_ENABLE == 1 ]] && echo "qBittorrent port is not the right one, I'll change it" >> "$LOG_PATH/${0##*/}.log"
sleep 1
$QBT_BIN server settings connection -p "$REMOTE_PIA_PORT" --username "$QBITTORRENT_USERNAME" --password "$QBITTORRENT_PASSWORD" --url "http://${QBITTORRENT_HOST}:$QBITTORRENT_PORT"
sleep 1
fi
else # I can't find the pia port
[[ $LOG_ENABLE == 1 ]] && echo "I can't find the PIA port from $PIA_EXT_PORT make sure that exist and is reachable" >> "$LOG_PATH/${0##*/}.log"
fi
Если я выполняю эту команду в сеансе замазки, у меня нет проблем
+ OWRT_UNAME=root
+ OWRT_IP_ADDR=10.0.0.1
+ QBITTORRENT_HOST=localhost
+ QBITTORRENT_USERNAME=admin
+ QBITTORRENT_PASSWORD=adminadmin
+ QBITTORRENT_PORT=8081
+ LOG_ENABLE=0
+ LOG_PATH=/tmp
+ [[ 0 == 1 ]]
+ [[ 0 == 1 ]]
++ command -v qbt
+ QBT_BIN=/usr/bin/qbt
++ command -v ssh
+ SSH_BIN=/usr/bin/ssh
++ command -v jq
+ JQ_BIN=/usr/bin/jq
+ [[ -z /usr/bin/qbt ]]
+ [[ -z /usr/bin/ssh ]]
++ /usr/bin/ssh root@10.0.0.1 'cat /tmp/pia-port-forward'
+ REMOTE_PIA_PORT=46786
+ [[ -n 46786 ]]
++ /usr/bin/qbt server settings connection --username admin --password adminadmin --url http://localhost:8081 --format json
++ /usr/bin/jq .ListenPort
+ ACTUAL_QBITTORRENT_PORT=46786
+ [[ 46786 == 46786 ]]
+ [[ 0 == 1 ]]
Но если я перезапускаю службу и запускается сценарий, я вижу это
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + OWRT_UNAME=root
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + OWRT_IP_ADDR=10.0.0.1
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + QBITTORRENT_HOST=localhost
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + QBITTORRENT_USERNAME=admin
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + QBITTORRENT_PASSWORD=adminadmin
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + QBITTORRENT_PORT=8081
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + LOG_ENABLE=0
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + LOG_PATH=/tmp
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ 0 == 1 ]]
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ command -v qbt
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + QBT_BIN=/usr/bin/qbt
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ command -v ssh
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + SSH_BIN=/usr/bin/ssh
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ command -v jq
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + JQ_BIN=/usr/bin/jq
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ -z /usr/bin/qbt ]]
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ -z /usr/bin/ssh ]]
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ /usr/bin/ssh root@10.0.0.1 'cat /tmp/pia-port-forward'
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + REMOTE_PIA_PORT=46786
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ -n 46786 ]]
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ /usr/bin/qbt server settings connection --username admin --password adminadmin --url http://localhost:8081 --format json
Mar 14 16:04:30 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: ++ /usr/bin/jq .ListenPort
Mar 14 16:04:33 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: Cannot assign requested address
Mar 14 16:04:33 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + ACTUAL_QBITTORRENT_PORT=
Mar 14 16:04:33 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ '' == 46786 ]]
Mar 14 16:04:33 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + [[ 0 == 1 ]]
Mar 14 16:04:33 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + sleep 1
Mar 14 16:04:34 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: + /usr/bin/qbt server settings connection -p 46786 --username admin --password adminadmin --url http://localhost:8081
Mar 14 16:04:36 Qnap qBittorrent_OPENWRT_Pia_Port.sh[27486]: Cannot assign requested address
Я не могу понять, почему я получаю эту ошибку, и ACTUAL_QBITTORRENT_PORT назначен неправильно. Я не программист, поэтому я сделал этот небольшой скрипт со своими собственными знаниями и некоторыми исследованиями, возможно, есть лучший способ сделать это, особенно часть jq, возможно, есть лучший способ для анализа json данных (может быть список тоже) но это другая история, теперь я должен исправить эту маленькую проблему.
Есть идеи?