我目前正在尝试在 c9.io 开发环境中获取适用于 node.js 库 node-snmpjs 的基本示例代码。
示例代码在这里:
https://github.com/wesolows/node-snmpjs或
https://npmjs.org/package/snmpjs
var os = require('os');
var snmp = require('snmpjs');
var logger = require('bunyan');
var log = new logger({
name: 'snmpd',
level: 'info'
});
var agent = snmp.createAgent({
log: log
});
agent.request({ oid: '.1.3.6.1.2.1.1.5', handler: function (prq) {
console.log("Request received");
var nodename = os.hostname();
var val = snmp.data.createData({ type: 'OctetString',
value: nodename });
snmp.provider.readOnlyScalar(prq, val);
} });
agent.bind({ family: 'udp4', port: 161 });
我尝试将绑定端口更改为: parseInt(process.env.PORT)
当我尝试对 c9 在运行时吐出的地址和端口执行 snmpget 时,它会失败。我也试过:8080。8080 是 c9 的控制台输出总是说映射到 process.env.PORT。我还尝试了 snmpget 中的端口 80。
作为后备方案,我还尝试使用 c9 进行基本的 telnet、套接字连接,但无法正常工作......似乎根本问题可能是我不知道正在使用什么 IP 和端口.
- - - - - - - - - - - - - 编辑 - - - - - - - - - - - -
我在 agent.bind 行上尝试了以下变体:
agent.bind({ family: 'udp4', port: parseInt(process.env.PORT), addr: process.env.IP});
和
agent.bind({ family: 'udp4', port: 17000, addr: process.env.IP});
输出:
Your code is running at 'http://node-dev1.thaspius.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
"127.12.254.129:17000"
ex-c9-node47.prod.rhcloud.com
{"name":"snmpd","hostname":"ex-c9-node47.prod.rhcloud.com","pid":32758,"component":"snmp-agent","level":30,"msg":"Bound to 127.12.254.129:17000","time":"2013-07-31T17:59:56.376Z","v":0}
snmpget 结果:
snmpget -v 2c -c any 127.12.254.129:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from 127.12.254.129:17000.
snmpget -v 2c -c any ex-c9-node47.prod.rhcloud.com:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from ex-c9-node47.prod.rhcloud.com:17000.
snmpget -v 2c -c any node-dev1.thaspius.c9.io:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from node-dev1.thaspius.c9.io:17000.