scripts/nodejs/myip.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-09-28 15:45:37 +02:00
#!/usr/bin/env node
// Require the dns module
var dns = require("dns");
2015-09-28 15:45:37 +02:00
// Function for IPv4
var myip4 = function () {
// Set DNS servers to OpenDNS IPv4
var servers4 = dns.setServers(["208.67.222.222", "208.67.220.220"]);
// Check where myip.opendns.com resolves to and I think this is a
// function that takes arguments err and addresses.
dns.resolve4("myip.opendns.com", function (err, addresses) {
// if err contains a truthy value, an error has occurred
if (err) throw err;
// Print the first thing in array addresses as it has our IPv4
// address.
console.log(addresses[0]);
});
};
2015-09-28 15:45:37 +02:00
// IPv6 part is broken because of node bug.
// https://github.com/nodejs/node/issues/894
2015-09-28 15:45:37 +02:00
2015-09-28 15:54:55 +02:00
/*
// Function for IPv6
var myip6 = function () {
2015-09-28 15:45:37 +02:00
// Set DNS servers to OpenDNS IPv6
var servers6 = dns.setServers(["2620:0:ccc::2", "2620:0:ccd::2"])
2015-09-28 15:45:37 +02:00
// Check where myip.opendns.com resolves to and I think this is a function
// that takes arguments err and addresses.
dns.resolve6('myip.opendns.com', function (err, addresses) {
2015-09-28 15:54:55 +02:00
2015-09-28 19:01:52 +02:00
// if err contains a truthy value, an error has occurred
2015-09-28 15:54:55 +02:00
if (err) throw err;
2015-09-28 15:45:37 +02:00
2015-09-28 15:54:55 +02:00
// Print the first thing in array addresses as it has our IPv4
// address.
console.log(addresses[0]);
});
}
2015-09-28 15:45:37 +02:00
*/
// Call the functions
myip4();
//myip6();