我的Bash脚本无法缓存项目

时间:2018-01-19 10:36:09

标签: bash

我尝试使用我发现的Bash脚本并稍加修改以自动更新GoDaddy上的DNS设置。

它有点有用,但是我没有得到回声"没有IP地址,程序退出"因为我没想到第28行正在创建缓存文件。有人能告诉我发生了什么吗?我正在使用Raspbian。非常感谢提前!

#/bin/bash

# This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection.
#
# Original PowerShell script by mfox: https://github.com/markafox/GoDaddy_Powershell_DDNS
# Ported to bash by sanctus
# Added AAAA record by Binny Chan
#
# Improved to take command line arguments and output information for log files by pollito
#
# First go to GoDaddy developer site to create a developer account and get your key and secret
#
# https://developer.godaddy.com/getstarted
#
# Be aware that there are 2 types of key and secret - one for the test server and one for the production server
# Get a key and secret for the production server

# Check an A record and a domain are both specified on the command line.
if [ $# -ne 2 ]; then
    echo "usage: $0 type a_record domain_name"
    echo "usage: $0 AAAA www my_domain"
    exit 1
fi

# Set A record and domain to values specified by user
name=$1     # name of A record to update
domain=$2   # name of domain to update
cache=/tmp/.mcsync.$name.$domain.addr
[ -e $cache ] && old=`cat $cache`

# Modify the next two lines with your key and secret
key="key"      # key for godaddy developer API
secret="secret"   # secret for godaddy developer API

headers="Authorization: sso-key $key:$secret"

#echo $headers
        # Get public ip address there are several websites that can do this.
        ret=$(curl -s GET "http://ipinfo.io/json")
        currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

# Check empty ip address or not
if [ -z "$currentIp" ]; then
        echo $name"."$domain": $(date): no ip address, program exit"
        exit 1
fi
# Check cache ip, if matched, program exit
if [ "$old" = "$currentIp" ]; then
        echo $name"."$domain": $(date): address unchanged, program exit $currentIp"
        echo "IPs equal. Exiting..."
        exit
else
        echo $name"."$domain": $(date): currentIp:" $currentIp
fi

#Get dns ip
result=$(curl -s -k -X GET -H "$headers" \
 "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
        dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")

echo $name"."$domain": $(date): dnsIp:" $dnsIp

# ip not match
if [ "$dnsIp" != $currentIp ];
 then
        echo $name"."$domain": $(date): IPs not equal. Updating."
        request='{"data":"'$currentIp'","ttl":3600}'
        #echo $request
        nresult=$(curl -i -k -s -X PUT \
 -H "$headers" \
 -H "Content-Type: application/json" \
 -d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
        #echo $nresult
        echo $name"."$domain": $(date): IPs not equal. Updated."
fi

1 个答案:

答案 0 :(得分:0)

不,该消息位于第44行,由于$currentIp为空,因此在第39行检索:读取curl请求对" http://ipinfo.io/json&#34的响应;

否则只需删除缓存文件

rm /tmp/.mcsync.$name.$domain.addr

其中$name$domain被替换为脚本的第一个和第二个参数。