Permalink
Browse files

new structure

  • Loading branch information...
1 parent 47d9ce6 commit 5e4b8a7f94a9694c6f922a9b38678b8b2466398a @danielzzz committed Nov 23, 2011
Showing with 130 additions and 0 deletions.
  1. +15 −0 .gitignore
  2. +12 −0 examples/example.js
  3. +6 −0 index.js
  4. +44 −0 lib/ping-pcap.js
  5. +36 −0 lib/ping-sys.js
  6. +17 −0 package.json
  7. BIN ping-0.1.0.tgz
View
15 .gitignore
@@ -0,0 +1,15 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
View
12 examples/example.js
@@ -0,0 +1,12 @@
+//-------- example -----------------------
+
+var ping = require('../index');
+
+var hosts = ['192.168.1.1', 'google.com', 'yahoo.com'];
+hosts.forEach(function(host){
+ ping.sys.probe(host, function(isAlive){
+ var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
+ console.log(msg);
+ });
+});
+
View
6 index.js
@@ -0,0 +1,6 @@
+var ping = {};
+
+ping.sys = require('./lib/ping-sys');
+//ping.pcap = require('./lib/ping-pcap');
+
+module.exports = ping;
View
44 lib/ping-pcap.js
@@ -0,0 +1,44 @@
+/**
+* LICENSE MIT
+* (C) Daniel Zelisko
+* http://github.com/danielzzz/node-ping
+*
+* A poor man's ping for node.js
+* It uses UDP_scanning (as node is not able to generate iCPM packets)
+* http://en.wikipedia.org/wiki/Port_scanner#UDP_scanning
+* it may not work correct for hosts that silently drop UDP traffic on their firewall
+* you need at pcap version 0.1.9 or higher
+*
+*/
+
+var sys = require("util"),
+pcap = require('pcap');
+
+var addr = process.argv[3] || 'localhost';
+setInterval(function() {probe(addr)}, 1000);
+
+
+function probe(addr) {
+ sys.puts('sending a probe to ' + addr);
+ var dgram = require('dgram');
+ var message = new Buffer("Some bytes");
+ var client = dgram.createSocket("udp4");
+ client.send(message, 0, message.length, 21111, addr);
+ client.close();
+}
+
+// create a pcap session
+pcap_session = pcap.createSession(process.argv[2] || 'eth0', "");
+
+
+// listen for packets, decode them, and feed the simple printer
+pcap_session.addListener('packet', function (raw_packet) {
+ var packet = pcap.decode.packet(raw_packet);
+ if (packet.link && packet.link.ip && packet.link.ip.saddr==addr) {
+ packet.link && packet.link.ip && sys.puts(packet.link.ip.saddr + " is alive");
+ }
+});
+
+//-------- example ------------------------
+
+
View
36 lib/ping-sys.js
@@ -0,0 +1,36 @@
+/**
+* LICENSE MIT
+* (C) Daniel Zelisko
+* http://github.com/danielzzz/node-ping
+*
+* a simple wrapper for ping
+*
+*/
+
+var sys = require('util'),
+ spawn = require('child_process').spawn;
+
+
+function probe(addr, cb) {
+ var ls = spawn('/bin/ping', ['-n', '-w 2', '-c 1', addr]);
+ /*ls.stdout.on('data', function (data) {
+ //sys.print('stdout: ' + data);
+ });
+
+ ls.stderr.on('data', function (data) {
+ //sys.print('stderr: ' + data);
+ });*/
+
+ ls.on('exit', function (code) {
+ var result = (code === 0 ? true : false);
+ cb && cb(result);
+ });
+}
+
+exports.probe = probe;
+
+
+
+
+
+
View
17 package.json
@@ -0,0 +1,17 @@
+{
+ "author": "danielzzz <daniel@zelisko.net> (http://daniel.zelisko.net)",
+ "name": "ping",
+ "description": "a simple wrapper for ping",
+ "version": "0.1.0",
+ "homepage": "http://github.com/danielzzz/node-ping",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/danielzzz/node-ping.git"
+ },
+ "engines": {
+ "node": "~0.6.2"
+ },
+ "dependencies": {},
+ "devDependencies": {},
+ "main":"index.js"
+}
View
BIN ping-0.1.0.tgz
Binary file not shown.

0 comments on commit 5e4b8a7

Please sign in to comment.