普通宽带实现固定IP

固定IP价格太贵,研究几天通过某信宽带拨号上网获取的动态公网IP(一般分配是私网IP,可以打电话给客服让改公网IP),然后在域名添加个A记录,通过不断监测本地公网IP更新到A记录实现。

配置需求:

1.宽带一条,拨号可以得到公网IP

2.域名一个,什么域名都可以

3.注册一个cloudflare帐号,免费版就行。阿里域名解析服务也可以,没有测试

4.一台可以运行shell的机器

方法:

将域名托管转移到cloudflare,在cloudflare服务里dns服务添加一台A记录,填上你本地的公网IP。

使用cloudflare的API更新公网IP,需要提供cloudflare的zone id 和 key,使用api获取域名A记录序列号。

curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records?type=A&name=example.com&content=127.0.0.1&page=1&per_page=20&order=type&direction=desc&match=all" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json"

运行上面命令会返回

{
  "success": true,
  "errors": [],
  "messages": [],
  "result": [
    {
      "id": "372e67954025e0ba6aaa6d586b9e0b59",
      "type": "A",
      "name": "example.com",
      "content": "198.51.100.4",
      "proxiable": true,
      "proxied": false,
      "ttl": 120,
      "locked": false,
      "zone_id": "023e105f4ecef8ad9ca31a8372d0c353",
      "zone_name": "example.com",
      "created_on": "2014-01-01T05:20:00.12345Z",
      "modified_on": "2014-01-01T05:20:00.12345Z",
      "data": {}
    }
  ],
  "result_info": {
    "page": 1,
    "per_page": 20,
    "count": 1,
    "total_count": 2000
  }
}

返回id就是添加的A记录的标示符

下一步定时更新公网IP到cloudflare

#获取公网IP
a1=$(curl -s ipecho.net/plain)

#更新到cloudflare
curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"example.com","content":"'"$a1"'","ttl":120,"proxied":false}'

返回如下说明就已经成功了:

{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "372e67954025e0ba6aaa6d586b9e0b59",
    "type": "A",
    "name": "example.com",
    "content": "198.51.100.4",
    "proxiable": true,
    "proxied": false,
    "ttl": 120,
    "locked": false,
    "zone_id": "023e105f4ecef8ad9ca31a8372d0c353",
    "zone_name": "example.com",
    "created_on": "2014-01-01T05:20:00.12345Z",
    "modified_on": "2014-01-01T05:20:00.12345Z",
    "data": {}
  }
}

这样就可以通过域名访问了,然后自己在路由设置好映射就可以了。

cloudflare API参考地址https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record

以上个人自写,如有违规请联系作者修改。建议也可以留言

猜你喜欢

转载自blog.csdn.net/gejian1208/article/details/86677306