这些脚本范例可示范 SNMP API 的使用方法。这些范例都是以 JScript 写成。
SnmpRqst.Initialize 方法会返回 ComResult 对象,告知初始化是否成功。
此脚本会使用 Failed 方法来侦测错误,并在初始化失败时用 GetErrorMsg 记录错误信息:
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
或者使用设备地址和 SNMP 认证 ID 进行初始化:
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var sAddress = "192.168.3.1";
var nCredentialID = 1;
var oComResult = oSnmpRqst.Initialize2(sAddress, nCredentialID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.2.1.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure.Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success.Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
oComResult = oSnmpRqst.SetPort(1234);
oComResult = oSnmpRqst.SetTimeoutMs(5000); // 5 second timeout
var oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.2.1.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure.Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success.Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var sOid = "1.3.6.1.2";
//get the next 10 objects
for (i=0; i<10; i++)
{
var oSnmpResponse = oSnmpRqst.GetNext(sOid);
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure.Error=" + oSnmpResponse.GetErrorMsg);
break;
}
else
{
sOid = oSnmpResponse.GetOid;
Context.LogMessage(sOid + "=" + oSnmpResponse.GetValue);
}
}
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = Context.GetProperty("DeviceID");
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
// Get three objects in one packet:
var oSnmpResponse = oSnmpRqst.GetMultiple("1.3.6.1.2.1.1.1.0,1.3.6.1.2.1.1.2.0,1.3.6.1.2.1.1.3.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure.Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
var sXML = oSnmpResponse.GetPayload;
var objXMLDocument = new ActiveXObject("Microsoft.XMLDOM");
objXMLDocument.async = false;
objXMLDocument.loadXML(sXML);
var oVarBinds = objXMLDocument.getElementsByTagName("SnmpVarBind");
// For each variable binding, log OID=VALUE
for (var i=0; i<oVarBinds.length; i++)
{
Context.LogMessage(oVarBinds(i).getAttribute("sOid") + "=" + oVarBinds(i).getAttribute("sValue"));
}
}
<注意> 从 WhatsUp Gold v14 开始,您可使用内建的 SNMP Set 操作设定 SNMP 值。如需详细信息,请参阅使用 SNMP Set 操作。
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var oSnmpResponse = oSnmpRqst.Set("1.3.6.1.4.1.9.2.9.9.0", 'i', 2); /* reload */
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure.Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success.Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
另请参阅 |