1

我正在尝试使用 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  

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

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

谢谢!

4

1 回答 1

3

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

# 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
于 2015-12-01T00:23:45.523 回答