실행 중인 스크립트의 로그가 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
}

성공하면 이 함수를 호출하면 0이 아닌 값으로 $CheckIP가 출력되고 주소가 포함된 $IP가 출력됩니다.

관련 정보