使用bash变量替换curl命令中的IP

时间:2018-08-22 20:48:40

标签: bash dns cloudflare

我目前正在尝试制作一个与Cloudflare API交互的DDNS脚本,以捕获我的IP地址中的更改并自动修复Web服务器的IP地址更改。到目前为止,一切工作正常,但我无法将$ IP正确放入curl语句中。我首先从bash脚本中运行python脚本以获取ip地址,然后在bash脚本中运行curl语句。这就是python脚本的样子(它返回一个IP地址,例如“ 1.1.1.1”,其中包含引号,因为curl命令需要引号)

#!/usr/bin/python3

import subprocess as sp

def main():
    command = "dig +short myip.opendns.com @resolver1.opendns.com";
    ip = sp.check_output(command, shell=True).decode('utf-8').strip('\n');
    ip_tmp = ip;
    ip_tmp = '"' + ip + '"';
    ip = ip_tmp;
    print(ip);


if __name__ == "__main__":
    main();

bash脚本如下所示:

#!/bin/bash

IP=$("./getIP.py")

curl -X PUT "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id" \
     -H "X-Auth-Email: example.com" \
     -H "X-Auth-Key: authkey" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"example.com","content":$IP,"ttl":120,"proxied":true}'

我试图让python脚本只返回数字,然后在bash脚本中添加引号,现在反之亦然,我似乎无法使其正常工作。变量替换为ip地址附近的引号后,最后一行应该看起来像这样:

'{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":true}'

2 个答案:

答案 0 :(得分:2)

您的json结构周围的单引号可防止变量扩展。

您有一些现成的选项。

在json中/周围转义的丑陋引号。

"{\"type\":\"A\",\"name\":\"example.com\",\"content\":$IP,\"ttl\":120,\"proxied\":true}"

让python将这些数据写入文件,并告诉curl使用该文件作为发布数据的来源。

curl -X PUT "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id" \
 -H "X-Auth-Email: example.com" \
 -H "X-Auth-Key: authkey" \
 -H "Content-Type: application/json" \
 --data @file_you_wrote_your_json_to.json

使用python请求或urllib模块将请求发布到云弹。

更新main()函数以返回IP,而不是打印IP。

my_ip = main()
url = "https://api.cloudflare.com/client/v4/zones/zone_id/dns_records/dns_id"

myheaders = {
    "X-Auth-Email": "example.com",
    "X-Auth-Key": "authkey",
    "Content-Type": "application/json"
}

myjson = {
    "type":"A",
    "name":"example.com",
    "content":my_ip,
    "ttl":120,
    "proxied":true
}

requests.put(url, headers=myheaders, data=myjson)

答案 1 :(得分:0)

更好的是,只需执行bash。 Cloudflare DDNS on github.

拍摄一张动态A记录ID:

curl -X GET "https://api.cloudflare.com/client/v4/zones/**Zone ID** \
                    /dns_records?type=A&name=dynamic" \
     -H "Host: api.cloudflare.com" \
     -H "User-Agent: ddclient/3.9.0" \
     -H "Connection: close" \
     -H "X-Auth-Email: example@example.com" \
     -H "X-Auth-Key: "**Authorization key**" \
     -H "Content-Type: application/json"

设置作业(* * * * *)以设置动态A记录:

#/usr/bin/env sh

AUTH_EMAIL=example@example.com
AUTH_KEY=** CF Authorization  key **
ZONE_ID=** CF Zone ID **
A_RECORD_NAME="dynamic"
A_RECORD_ID=** CF A-record ID from cloudflare-dns-id.sh **

IP_RECORD="/tmp/ip-record"
RECORDED_IP=`cat $IP_RECORD`
PUBLIC_IP=$(curl --silent https://api.ipify.org) || exit 1

if [ "$PUBLIC_IP" = "$RECORDED_IP" ]; then
    exit 0
fi

echo $PUBLIC_IP > $IP_RECORD

RECORD=$(cat <<EOF
{ "type": "A",
  "name": "$A_RECORD_NAME",
  "content": "$PUBLIC_IP",
  "ttl": 180,
  "proxied": false }
EOF
)
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID \
             /dns_records/$A_RECORD_ID" \
     -X PUT \
     -H "Content-Type: application/json" \
     -H "X-Auth-Email: $AUTH_EMAIL" \
     -H "X-Auth-Key: $AUTH_KEY" \
     -d "$RECORD"