Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created September 1, 2017 04:54
Show Gist options
  • Save leizongmin/2817aada702c249ed8d1ee93721540a7 to your computer and use it in GitHub Desktop.
Save leizongmin/2817aada702c249ed8d1ee93721540a7 to your computer and use it in GitHub Desktop.
DNSPod 更新域名解析记录
/**
* DNSPOD 动态更新域名
*
* 参考:https://gist.github.com/chuangbo/833369
* https://www.dnspod.cn/docs/records.html#dns
*/
const os = require('os');
const request = require('request');
const login_token = '12345,xxxxxx';
const domain = 'example.com';
const sub_domain = 'xyz';
const record_id = '316693516';
const record_line = '默认';
const record_type = 'A';
const mx = '0';
const ttl = '120';
const format = 'json';
function getIP() {
try {
return os.networkInterfaces().wlan0.find(v => v.family === 'IPv4').address;
} catch (err) {
console.error(err);
}
}
function update() {
const value = getIP();
if (!value) {
return console.error('无法正确获取IP地址');
}
const data = { domain, sub_domain, record_id, record_line, record_type, mx, ttl, value };
console.log('更新IP地址为%j...', data);
request({
method: 'POST',
url: 'https://dnsapi.cn/Record.Modify',
form: { login_token, format, ...data },
}, (err, res, body) => {
if (err) {
console.error(err);
return console.error('更新DNS记录出错');
}
console.log(body);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment