Guest User

Untitled

a guest
Sep 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. var http = require('http');
  2. var linode = require('linode-api');
  3.  
  4. var my_api_key = '';
  5. var domain_id = 0;
  6. var resource_id = 0;
  7.  
  8. var linode_client = new (linode.LinodeClient)(my_api_key);
  9.  
  10. var options = {
  11. host : 'jsonip.com',
  12. port : 80,
  13. path : '/',
  14. method : 'GET'
  15. };
  16.  
  17. var req = http.request(options, function(res) {
  18.  
  19. res.on('data', function(chunk) {
  20.  
  21. var response_obj = JSON.parse('' + chunk);
  22. var current_ip = response_obj.ip;
  23.  
  24. console.log (current_ip);
  25.  
  26. var linode_params = {
  27. domainid : domain_id,
  28. resourceid : resource_id
  29. };
  30.  
  31. console.log('Looking up last known IP...');
  32.  
  33. // lookup previous value, so we know if we should send the update
  34. linode_client.call('domain.resource.list', linode_params, function(err, lookup_res) {
  35.  
  36. var last_ip = lookup_res[0].TARGET;
  37.  
  38. console.log(last_ip);
  39.  
  40. if (last_ip != current_ip) {
  41.  
  42. console.log('DNS is stale. Updating now...');
  43.  
  44. linode_params.target = current_ip;
  45. linode_client.call('domain.resource.update', linode_params, function (err, update_res) {
  46. if (err) throw err;
  47. console.log(update_res);
  48. });
  49.  
  50. } else {
  51.  
  52. console.log('DNS is up to date');
  53. }
  54. });
  55.  
  56. });
  57.  
  58. });
  59.  
  60.  
  61. console.log('Looking up current IP...')
  62.  
  63. req.end();
Add Comment
Please, Sign In to add comment