我有一个在 00:10 或重新启动时启动的脚本,当前在我的 NAS 上运行。为了确保它按照我想要的方式进行,我一直在记录输出。我尝试了各种写入日志的方法,即正常的>>
和当前的,2>&1 | tee -a
当我希望将 IP 报告到日志中时,结果在某个时刻是相同的,它显示为 HTMLUpdated 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
感谢库萨拉南达指出我推理中的错误。我有一个解决方案来解决我眼前的问题。我将其作为答案发布,因为虽然不优雅,但它确实解决了我的问题,并可能因此帮助其他人。
简而言之,我创建了一个函数,可以选择 9 个可能的源来获取外部 IP,代替我调用该函数的第一个脚本中的各个语句。理想情况下,因为它总是使用第一个选择,除非我想每次使用不同的选择,但我看不到实现这一目标的简单方法。
#!/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