Permalink
Browse files

Added a few more examples. Privatized variables in API objects.

  • Loading branch information...
Trakkasure committed May 27, 2011
1 parent 0791651 commit 7cbdae2d7d95f17be2f4ec6297c0a1032299a100
Showing with 518 additions and 396 deletions.
  1. +23 −0 examples/getInterfacesAndRoutes.js
  2. +3 −5 examples/{getIp.js → getIpList.js}
  3. +57 −0 examples/watchIPList.js
  4. +432 −386 lib/index.js
  5. +3 −5 package.json
@@ -0,0 +1,23 @@
+api=require('../lib/index.js');
+
+c1=new api('192.168.0.1','admin','');
+c1.closeOnDone(true);
+c1.connect(function(c) {
+ var o=this.openChannel();
+ o.closeOnDone(true);
+ o.write(['/interface/print'],function(channel){
+ console.log('Getting Interfaces');
+ channel.once('done',function(p,chan) {
+ p=api.parseItems(p);
+ p.forEach(function(i){console.log(JSON.stringify(i))});
+ });
+ });
+ o.write('/ip/route/print',function(channel){
+ console.log('Getting routes');
+ channel.on('done',function(p,chan) {
+ console.log('Routes:');
+ p=api.parseItems(p);
+ p.forEach(function(i){console.log(JSON.stringify(i))});
+ });
+ });
+});
@@ -1,11 +1,12 @@
var api = require('../lib/index.js');
-var connection = new api('192.168.0.1','admin','password');
+var connection = new api('192.168.0.1','admin','');
connection.connect(function(conn) {
var chan=conn.openChannel();
-
+ conn.closeOnDone(true);
chan.write('/ip/address/print',function() {
+ chan.closeOnDone(true);
chan.on('done',function(data) {
var parsed = api.parseItems(data);
@@ -14,9 +15,6 @@ connection.connect(function(conn) {
console.log('Interface/IP: '+item.interface+"/"+item.address);
});
- chan.close();
- conn.close();
-
});
});
});
View
@@ -0,0 +1,57 @@
+api=require('../lib/index.js');
+c1=new api('192.168.0.1','admin','');
+
+var ipList={};
+function ipOutput(p){
+ console.log(JSON.stringify(p));
+ var v=api.parseItems(p)[0]; // Returns a row of items, but we only need one.
+ var t=ipList[v['.id']];
+
+ console.log(JSON.stringify(v));
+ if (v['.dead']) { // if it was removed...
+ if (!t) t={name:"Unknown"}; // we don't have this ID in our list.
+ console.log('IP: '+t.address+' deleted');
+ delete ipList[v['.id']];
+ } else {
+ var c=[]
+ if (!t) ipList[v['.id']]=v;
+ else
+ Object.keys(v).forEach(function(k){
+ if (v[k]!=t[k]) {
+ if (c.length==0) c.push('changed ');
+ c.push(" ("+k+')'+t[k]+' to '+v[k]+' ');
+ ipList[v['.id']][k]=v[k];
+ }
+ });
+ console.log('IP: address '+v['address']+c.join("\n"));
+ }
+ return true;
+}
+
+function connCallback(connection) {
+ var o=connection.openChannel();
+ o.addListener('trap',function(e){
+ console.log('There was an error: '+e);
+ });
+ o.write('/ip/address/getall',function(channel) {
+ channel.once('done', function(p) {
+ p=api.parseItems(p);
+ p.forEach(function(p) {
+ console.log('Loaded: ('+p['.id']+')'+p.address);
+ ipList[p['.id']]=p;
+ });
+ });
+ });
+ o.write('/ip/address/listen',function(channel){
+ console.log('Listening to ip changes.');
+ console.log('Press CTRL-C to stop listening.');
+ channel.on('done',function(){console.log('ip listen done')});
+ channel.addListener('read',ipOutput); // report when an IP is being addeed.
+ });
+}
+c1.addListener('trap',function(e) {
+ console.log('Connection caught a trap: '+e['message']);
+});
+
+c1.connect(connCallback);
+
Oops, something went wrong.

0 comments on commit 7cbdae2

Please sign in to comment.