使用powershell脚本发送SNMP陷阱

时间:2015-03-24 20:36:27

标签: powershell snmp snmp-trap

我正在尝试使用powershell脚本发送SNMP陷阱(它必须是一个powershell脚本,它将在Windows服务器上运行)。我有一个测试环境运行,有一个陷阱接收器。

我一直在关注这个教程http://www.activexperts.com/network-component/howto/snmpts/powershell10/,但我还没有开始工作。

下面是我的代码,脚本运行正常

$objSnmpTrapManager = new-object -comobject AxNetwork.DnsServer #create object
# Create a SnmpTrapOut instance
$objSnmpTrapManager = new-object -comobject AxNetwork.SnmpTrapManager
$objSnmpTrap     = new-object -comobject AxNetwork.SnmpTrap
$objSnmpObject   = new-object -comobject AxNetwork.SnmpObject
$objConstants    = new-object -comobject AxNetwork.NwConstants

# Initialize SNMP
$objSnmpTrapManager.Initialize()
$res = "Initialize, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription( $objSnmpTrapManager.LastError ) + ")"
Write-Host $res
If($objSnmpTrapManager.LastError -ne 0 )
{
  exit
}

# Get Host, community name and optionally a MIB file
$strHostName    = "*******"
$strCommunity   = "public"

# Set trap properties
$objSnmpTrap.Clear()
$objSnmpTrap.Host     = $strHostName
$objSnmpTrap.Community = $strCommunity
$objSnmpTrap.Port     = 80 #is this the port that my trap reciever is looking at? or should it be the default 162

# Add first variable to trap
$objSnmpObject.Clear()
$objSnmpObject.OID   = ".1.3.6.1.2.1.1.5.0"
$objSnmpObject.Type  = $objConstants.nwSNMP_TYPE_OCTETSTRING
$objSnmpObject.Value = "test"
$objSnmpTrap.AddObject($objSnmpObject)

# Send the trap.
$objSnmpTrapManager.Send($objSnmpTrap)
$res = "Send, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription($objSnmpTrapManager.LastError) + ")"
Write-Host $res

# Shutdown SNMP
$objSnmpTrapManager.Shutdown()
$res = "Shutdown, result: " + $objSnmpTrapManager.LastError + " (" + $objSnmpTrapManager.GetErrorDescription($objSnmpTrapManager.LastError) + ")"
Write-Host $res  

一切都说成功,我认为我的问题是定义发送陷阱的位置(如果有人可以提供一个很棒的例子!)

有没有人有他们愿意分享的任何帮助或资源?

谢谢!

1 个答案:

答案 0 :(得分:3)

端口($objSnmpTrap.Port)应该是162,默认的SNMP陷阱接收器(或 trapsink )端口 - 除非您在测试环境中明确地将其更改为端口80:< / p>

# Get Host, community name and optionally a MIB file
$strHostName    = "receiver.test.environment.example"
$strCommunity   = "public"

# Set trap properties
$objSnmpTrap.Clear()
$objSnmpTrap.Host     = $strHostName
$objSnmpTrap.Community = $strCommunity
$objSnmpTrap.Port = 162
相关问题