使用 powershell 脚本发送 SNMP 陷阱
Send SNMP trap with powershell script
我正在尝试使用 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
一切都表明成功,我认为我的问题是定义发送陷阱的位置(如果有人能提供一个很棒的例子!)
有没有人愿意分享任何帮助或资源?
谢谢!
端口 ($objSnmpTrap.Port
) 应该是 162,默认的 SNMP Trap 接收器 (或 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
我正在尝试使用 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
一切都表明成功,我认为我的问题是定义发送陷阱的位置(如果有人能提供一个很棒的例子!)
有没有人愿意分享任何帮助或资源?
谢谢!
端口 ($objSnmpTrap.Port
) 应该是 162,默认的 SNMP Trap 接收器 (或 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