実行中のスクリプトのログがHTML形式で表示されることがあります。

実行中のスクリプトのログがHTML形式で表示されることがあります。

現在NASで実行されている00:10または再起動時に起動されるスクリプトがあります。私が望むように進行していることを確認するために出力を記録してきました。私はIPをログに報告したいと思ったときに、通常のログと>>現在のログに記録するさまざまな方法を試しました。2>&1 | tee -a結果はある時点でHTMLとして表示されますが、開いたり閉じたりしても、Updated IP is <!DOCTYPE html>現在のスクリプトは実行中です。set -x、結果は同じです。時には損傷するまでに数時間かかり、時には回復することもあります。常にこれを行うことができるIPアドレスが表示されているようです。住所をピリオドで区切って数字だけを使ってみました。

フルスクリプトは

 #!/bin/bash

#Uncomment the three below if debugging is needed
set -x
#set -v
#trap "set +x; sleep .1; set -x" DEBUG

#Set some variables
scripthome="/var/services/homes/admin/script"
logfile="/var/services/homes/admin/script/logs/ddns.log"
logfile2="/var/services/homes/admin/script/logs/ddns2.log"
bakfile="/var/services/homes/admin/script/logs/ddns.bak"
bakfile2="/var/services/homes/admin/script/logs/ddns2.bak"
waittime="1800" # time between updates in seconds
now=$(date +%s) #current time in seconds
ipadd="192.168.0.100" #just give it a valid address for now
ipadd2="192.168.0.100" #just give it a valid address for now
ipad="192.168.0.100" #getting odd results in the log so going to have two variable, one of which does not have the . removed
ipad2="192.168.0.100" #getting odd results in the log so going to have two variable, one of which does not have the . removed
runagain=$((now + waittime))
username="myusername"
password="mypassword"
#starttime=$(date +%H:%M)
endtime="23:00"
cd $scripthome || end
#Start & update the current IP
ipadd=$(curl ifconfig.co) #can also use curl wtfismyip.com/text
ipadd2=$ipadd
#remove the periods, easier to compare as an interger
ipad=$ipadd
ipad2=$ipadd2
ipadd="${ipadd//./}"
ipadd2="${ipadd2//./}"
counter=0
updates=0

mv $logfile $bakfile
mv $logfile2 $bakfile2
touch $logfile
touch $logfile2

{
date
echo
echo "next running intial curl, check logfile2"
echo
} 2>&1   |  tee -a $logfile

echo url="https://api.dynu.com/nic/update?username=$username&password=$password" | curl -k -o $logfile2 -K -

{
echo
date
echo "intial curl has run" 
echo "updated here, pausing before enter loop"
} 2>&1   |  tee -a $logfile
sleep $waittime

# need to delay a little otherwise sometimes after the sleep at the end of the loop we're not checking Ip everytime
waittime=$((waittime - 5))

while :
do
now=$(date +%s) #current time in seconds
echo "Current value of now is" "$now" "Current value of runagain is" $runagain "if now is greater we should enter control loop" 2>&1   |  tee -a $logfile
    if  [[ "$now" > "$runagain" ]]
    then
    echo "now greater than runagain time" 2>&1   |  tee -a $logfile
    ipadd=$(curl ifconfig.co)
    ipad=$ipadd
        ipadd="${ipadd//./}"
        {
        echo "Updated IP is $ipad" 
        echo "Compare IP is $ipad2"
        } 2>&1   |  tee -a $logfile
          if [[ "$ipadd" -ne "$ipadd2" ]]
          then
          echo "IP addresses do not match update needed" 2>&1   |  tee -a $logfile
          echo url="https://api.dynu.com/nic/update?username=$username&password=$password" | curl -k -o $logfile2 -K -
            date 2>&1   |  tee -a $logfile2 
          date 2>&1   |  tee -a $logfile
          echo "updated here IP addresses" 2>&1   |  tee -a $logfile
          ipadd2=$ipadd
          ipad2=$ipadd2
          ((updates++))
          else
          echo "IP addresses match no update is needed" 2>&1   |  tee -a $logfile
          fi
          echo "updated variable and loop" 2>&1   |  tee -a $logfile
    fi
now=$(date +%s) #current time in seconds
runagain=$((now + waittime))
currenttime=$(date +%H:%M)
          if [[ $endtime < $currenttime ]] 
          then
{
echo
echo "Exiting reached" $endtime
echo "Final variables are"
echo "ipad is $ipad"
echo "ipadd is $ipadd"
echo "ipad2=$ipad2"
echo "ipadd2=$ipadd2"
echo
} 2>&1   |  tee -a $logfile
          break
          fi
          sleep $((waittime +5))
          {
          date
          echo "loops completed =" $counter
          echo "Latest IP is $ipad"
          echo "Compare IP is $ipad2"
          echo "IP has been updated " $updates " times."
          echo
          } 2>&1   |  tee -a $logfile
((counter++))
done
exit

完全なログファイルです。

+ date
Wed May 27 08:32:05 +08 2020
+ echo

+ echo 'next running intial curl, check logfile2'
next running intial curl, check logfile2
+ echo

+ echo

+ date
Wed May 27 08:32:07 +08 2020
+ echo 'intial curl has run'
intial curl has run
+ echo 'updated here, pausing before enter loop'
updated here, pausing before enter loop
Current value of now is 1590541327 Current value of runagain is 1590541323 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 09:32:09 +08 2020
+ echo 'loops completed =' 0
loops completed = 0
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590543129 Current value of runagain is 1590543124 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 10:02:11 +08 2020
+ echo 'loops completed =' 1
loops completed = 1
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590544931 Current value of runagain is 1590544926 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 10:32:13 +08 2020
+ echo 'loops completed =' 2
loops completed = 2
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590546733 Current value of runagain is 1590546728 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 11:02:14 +08 2020
+ echo 'loops completed =' 3
loops completed = 3
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590548534 Current value of runagain is 1590548529 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 11:32:16 +08 2020
+ echo 'loops completed =' 4
loops completed = 4
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590550336 Current value of runagain is 1590550331 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 12:02:19 +08 2020
+ echo 'loops completed =' 5
loops completed = 5
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590552139 Current value of runagain is 1590552134 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 12:32:21 +08 2020
+ echo 'loops completed =' 6
loops completed = 6
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590553941 Current value of runagain is 1590553936 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 13:02:23 +08 2020
+ echo 'loops completed =' 7
loops completed = 7
+ echo 'Latest IP is 201.192.96.188'
Latest IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590555743 Current value of runagain is 1590555738 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Application Error</title>
<style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
<script async src='\''/cdn-cgi/bm/cv/1284585713/api.js'\''></script></head>
<body>
<iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
<script type="text/javascript">(function(){window['\''__CF$cv$params'\'']={r:'\''599d2ef79d230000'\'',m:'\''84f41f65048b9d761b2b03ee2dba081e6fc5bc4b-1590555744-1800-ASehXqTGhFflf2FlurpjHewkGmp+xKuyFS6UqmwG34OrGiv4KMhbB9mjRD87nIr3FuSERG+Rx/OoGxPEAGHY48Cjc7iUDVBCDzugeAj7gV818FW7z10oWbSu/hYdlF8ILQ=='\'',s:[0xdc18430849,0xe865472e4f],}})();</script></body>
</html>'
Updated IP is <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Application Error</title>
<style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
<script async src='/cdn-cgi/bm/cv/1284585713/api.js'></script></head>
<body>
<iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
<script type="text/javascript">(function(){window['__CF$cv$params']={r:'599d2ef79d230000',m:'84f41f65048b9d761b2b03ee2dba081e6fc5bc4b-1590555744-1800-ASehXqTGhFflf2FlurpjHewkGmp+xKuyFS6UqmwG34OrGiv4KMhbB9mjRD87nIr3FuSERG+Rx/OoGxPEAGHY48Cjc7iUDVBCDzugeAj7gV818FW7z10oWbSu/hYdlF8ILQ==',s:[0xdc18430849,0xe865472e4f],}})();</script></body>
</html>
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop
+ date
Wed May 27 13:32:24 +08 2020
+ echo 'loops completed =' 8
loops completed = 8
+ echo 'Latest IP is <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Application Error</title>
<style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
<script async src='\''/cdn-cgi/bm/cv/1284585713/api.js'\''></script></head>
<body>
<iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
<script type="text/javascript">(function(){window['\''__CF$cv$params'\'']={r:'\''599d2ef79d230000'\'',m:'\''84f41f65048b9d761b2b03ee2dba081e6fc5bc4b-1590555744-1800-ASehXqTGhFflf2FlurpjHewkGmp+xKuyFS6UqmwG34OrGiv4KMhbB9mjRD87nIr3FuSERG+Rx/OoGxPEAGHY48Cjc7iUDVBCDzugeAj7gV818FW7z10oWbSu/hYdlF8ILQ=='\'',s:[0xdc18430849,0xe865472e4f],}})();</script></body>
</html>'
Latest IP is <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Application Error</title>
<style media="screen">
          html,body,iframe {
            margin: 0;
            padding: 0;
          }
          html,body {
            height: 100%;
            overflow: hidden;
          }
          iframe {
            width: 100%;
            height: 100%;
            border: 0;
          }
        </style>
<script async src='/cdn-cgi/bm/cv/1284585713/api.js'></script></head>
<body>
<iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
<script type="text/javascript">(function(){window['__CF$cv$params']={r:'599d2ef79d230000',m:'84f41f65048b9d761b2b03ee2dba081e6fc5bc4b-1590555744-1800-ASehXqTGhFflf2FlurpjHewkGmp+xKuyFS6UqmwG34OrGiv4KMhbB9mjRD87nIr3FuSERG+Rx/OoGxPEAGHY48Cjc7iUDVBCDzugeAj7gV818FW7z10oWbSu/hYdlF8ILQ==',s:[0xdc18430849,0xe865472e4f],}})();</script></body>
</html>
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
+ echo 'IP has been updated ' 0 ' times.'
IP has been updated  0  times.
+ echo

Current value of now is 1590557544 Current value of runagain is 1590557539 if now is greater we should enter control loop
now greater than runagain time
+ echo 'Updated IP is 201.192.96.188'
Updated IP is 201.192.96.188
+ echo 'Compare IP is 201.192.96.188'
Compare IP is 201.192.96.188
IP addresses match no update is needed
updated variable and loop

大きな問題ではありませんが、私が見逃した部分についてのアドバイスが最も役に立ちます。

ベストアンサー1

私の推論の誤りを指摘してくれたKusaranandaに感謝します。今の問題の解決策があります。エレガントではありませんが、私の問題を解決し、結果として他の人に役立つ可能性があるため、回答として投稿しています。

簡単に言うと、外部IPを取得できる9つのソースを選択し、関数を呼び出す最初のスクリプトの個々のステートメントを置き換える関数を作成しました。理想的には、毎回他の選択肢を使用しない限り、常に最初の選択肢を使用するため、これを達成する簡単な方法はありません。

#!/bin/bash
# Going to be saved in the scripts directory as function_get_ip.sh then can be included in another script
# just by sourcing this one e.g after the variable definitions in the other script have the line
# source function_get_ip.sh

#define a function to get the current external IP
RetrieveIP1="curl wtfismyip.com/text"
RetrieveIP2="curl ifconfig.me"
RetrieveIP3="curl ifconfig.co"
RetrieveIP4="curl 'https://api.ipify.org?format=txt'"
RetrieveIP5="curl http://wooledge.org/myip.cgi"
RetrieveIP6="curl http://CheckIP=.dyndns.org  2> /dev/null| perl -pe  's,.*Address: (\d+\.\d+\.\d+\.\d+).*,$1,'"
RetrieveIP7="wget -qO - http://ipecho.net/plain | xargs echo"
RetrieveIP8="wget -qO - icanhazip.com"
RetrieveIP9="wget -qO - ipv4bot.whatismyipaddress.com"
CheckIP=0

function get_ip {
                                  IP=$($RetrieveIP1) 
                                  if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
                                  then
                                  CheckIP=1
                                  else
                              IP=$($RetrieveIP2) 
                              if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
                              then
                              CheckIP=2
                              else
                          IP=$($RetrieveIP3) 
                          if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
                          then
                          CheckIP=3
                          else
                      IP=$($RetrieveIP4) 
                      if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
                      then
                      CheckIP=4
                      else
                  IP=$($RetrieveIP5) 
                  if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
                  then
                  CheckIP=5
                  else
              IP=$($RetrieveIP6) 
              if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
              then
              CheckIP=6
              else
          IP=$($RetrieveIP7) 
          if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
          then
          CheckIP=7
          else
      IP=$($RetrieveIP8) 
      if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
      then
      CheckIP=8
      else
  IP=$($RetrieveIP9) 
  if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
  then
  CheckIP=9
                  fi
                fi
              fi
            fi
          fi  
        fi
      fi
    fi
  fi
}

成功すると、この関数を呼び出すとゼロ以外の値で$ CheckIPが出力され、アドレスを含む$ IPが出力されます。

おすすめ記事