我有一些管理多个Linux机器的Windows软件。
我想添加使用WOL(Wake On Lan)为所有盒子供电的功能,所以我写了这个UdpClient扩展,它增加了一个重载。
WakeFunction (请参阅intellisense )。该代码是从在不同地方发现的一些现有C#示例进行翻译(并简化)的。
Imports System.Net.Sockets
''' <summary>
''' An extended UdpClient Class with WOL WakeFunction
''' </summary>
Public Class WOLClass
Inherits UdpClient
Public Sub New() : End Sub
Public Sub New(IPAddress As String, NetMask As String)
Me.IPAddress = IPAddress
Me.NetMask = NetMask
End Sub
Private Sub SetClientToBroadcastMode()
If Me.Active Then
Me.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, False)
End If
End Sub
Public Property IPAddress As New String(String.Empty)
Public Property NetMask As New String(String.Empty)
''' <summary>
''' Send a magic packet to the entire subnet based on IP and NetMask properties to the MAC Address and Port
''' </summary>
''' <param name="MACAddress">The MAC Address of the machine to waken</param>
''' <param name="Port">Remove port on destination system (default = 9)</param>
Public Function WakeFunction(MACAddress As String, Optional Port As Long = 9L) As Boolean
If Me.IPAddress.Length <> 0 AndAlso Me.NetMask.Length <> 0 Then
Return WakeFunction(Me.IPAddress, Me.NetMask, MACAddress, Port)
Else
Throw New Exception("IPAddress and NetMASK properties must be set before calling this overload")
End If
End Function
''' <summary>
''' Sends a magic packet to the entire subnet based on IP and NetMask with the target set to the MAC Address
''' </summary>
''' <param name="IPAddress">The IP Address to which the Magic Packet is directed (can be a broadcast address)</param>
''' <param name="NetMask">The Network Mask of the subnet</param>
''' <param name="MacAddress">The targets MAC address as xx:xx:xx:xx:xx</param>
''' <param name="Port">Remote port on destination system (default = 9)</param>
'''
Public Function WakeFunction(IPAddress As String, NetMask As String, MacAddress As String, Optional Port As Long = 9L) As Boolean
Dim BrdAddress As Net.IPAddress = GetBroadcastAddress(Net.IPAddress.Parse(IPAddress).ToString, NetMask)
Dim Counter As Integer = 0
Dim bytes(1023) As Byte
Using client As New WOLClass
client.Connect(BrdAddress, Port)
client.SetClientToBroadcastMode()
For I As Integer = 0 To 5
Counter = I
bytes(Counter) = &HFF
Next
Dim MacPieces As String() = MacAddress.Split({":"}, StringSplitOptions.RemoveEmptyEntries)
For I As Integer = 0 To 15
For J As Integer = 0 To 5
Counter += 1
bytes(Counter) = Byte.Parse(MacPieces(J), Globalization.NumberStyles.HexNumber)
Next
Next
Try
client.Send(bytes, 1024I)
Return True
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Using
End Function
''' <summary>
''' Returns the subnets broadcast address based on the provided IP Address and Subnet Mask
''' </summary>
''' <param name="IPAddress">Any IP in the subnet (can even be the current broadcast address)</param>
''' <param name="NetMask">The NetMask of the the subnet</param>
''' <returns></returns>
Private Function GetBroadcastAddress(IPAddress As String, NetMask As String) As Net.IPAddress
Dim ThisAddress As Net.IPAddress = Net.IPAddress.Parse(IPAddress)
Dim ThisMask As Net.IPAddress = Net.IPAddress.Parse(NetMask)
Dim IPPieces As String() = IPAddress.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
Dim MaskPieces As String() = NetMask.Split({"."}, StringSplitOptions.RemoveEmptyEntries)
Dim IPBytes As Byte() = ThisAddress.GetAddressBytes
Dim MaskBytes As Byte() = ThisMask.GetAddressBytes
Dim BroadcastAddress(3) As Byte
For I As Integer = 0 To (IPBytes.Length - 1)
BroadcastAddress(I) = CByte((IPBytes(I) Or (MaskBytes(I) Xor 255)))
Next
Return New Net.IPAddress(BroadcastAddress)
End Function
End Class
用法示例
Using wc As New WOLClass("192.168.1.178", "255.255.255.0")
If wc.WakeFunction("ac:1f:6b:20:11:30") Then
MsgBox("Magic Packet Sent", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information)
End If
End Using