之前问电信拿了公网IP,但是每隔一段时间IP地址就会变,所以就打算写一个脚本每天定时检测IP地址有没有变化,如果有变化就自动更新。
https://console.cloud.tencent.com/cam/capi
登录链接,自己生成自己生成一个API
'Domain':'域名'
写上你的域名secretId
和secretKey
,填上你刚刚获取到API的ID和keynode 文件名.js
”RecordId“:'--------'
,这就是你那条记录的ID// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher const tencentcloud = require('tencentcloud-sdk-nodejs'); const DnspodClient = tencentcloud.dnspod.v20210323.Client; const clientConfig = { credential: { secretId: 'SecretId', secretKey: 'SecretKey', }, region: '', profile: { httpProfile: { endpoint: 'dnspod.tencentcloudapi.com', }, }, }; const client = new DnspodClient(clientConfig); const params = { // 这里写上你的域名 'Domain': '域名' }; client.DescribeRecordList(params).then( (data) => { console.log(data); }, (err) => { console.error('error', err); } );
流程我说一下,先通过https://api.ipify.org/?format=json
获取到公网IP,然后判断IP与DNSPod原本记录的是否相同,如果不同则更新。
clientConfig
和params1
和params2
,找里面的信息改成你自己的// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher const tencentcloud = require('tencentcloud-sdk-nodejs'); const request = require('request') const DnspodClient = tencentcloud.dnspod.v20210323.Client; const clientConfig = { credential: { secretId: 'secretId', secretKey: 'secretKey', }, region: '', profile: { httpProfile: { endpoint: 'dnspod.tencentcloudapi.com', }, }, }; const client = new DnspodClient(clientConfig); const params1 = { 'Domain': '域名', 'RecordId': 域名记录的ID }; const params2 = { 'Domain': '域名', 'SubDomain': '主机记录', 'RecordType': 'A', 'RecordLine': '默认', 'Value': '', 'RecordId': 域名记录的ID }; new Promise((resolve, reject)=>{ request('https://api.ipify.org/?format=json',function(err, res, body){ params2.Value = body.match(/'ip':'(.+)'\}$/i)[1].toString() resolve() }) }).then(res => { return new Promise((resolve, reject)=>{ client.DescribeRecord(params1).then( (data) => { console.log('new:', data.RecordInfo.Value, 'old:' ,params2.Value); if(!data.RecordInfo.Value === params2.Value){ // reject() resolve() } }, (err) => { console.error('error', err); } ) }).then(res => { return new Promise((resolve, reject)=>{ client.ModifyRecord(params2).then( (data) => { console.log(data); }, (err) => { console.error('error', err); } ) }) }) })
创建一个index.bat的文件,到时候直接执行就好
// index.bat
node 文件.js
将任务添加到任务计划程序中
联系客服