Skip to content

Instantly share code, notes, and snippets.

@rc1
Created September 15, 2014 14:16
Show Gist options
  • Select an option

  • Save rc1/44525d22483bbffc176c to your computer and use it in GitHub Desktop.

Select an option

Save rc1/44525d22483bbffc176c to your computer and use it in GitHub Desktop.
Stefan Demo
var sys = require('sys')
var exec = require('child_process').exec;
var child;
var vpnClientAddrs;
// We can use function pull ups which helps keep things clean.
// The pull up means we can use the function here even though it is below
getIpAddresses( doSomethingWithResults );
// finds external IP adresses of all clients by matching
// IP addresses after "P-t-P:" in ifconfig
// can't seem to do this with os.networkInterfaces() - it only
// seems to have the IP of this machine
function getIpAddresses( callback ) {
return exec("ifconfig | grep -P -o \"(?<=P-t-P:)(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\" ", function (error, stdout, stderr) {
vpnClientAddrs = stdout.split("\n");
// By convention, normally an error is the first argument of all callback
callback( error, vpnClientAddrs );
});
}
// An example
function doSomethingWithResults( err, results ) {
if ( err ) {
console.error( 'shit, that failed', err );
return;
}
console.log( 'That worked' );
console.log( results );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment