<p>PowerShellでM-SEARCHを実装する例(chat-gpt活用)</p>
<p>UPnP機器を探すためのM-SEARCHメッセージをマルチキャストアドレスに送信して、レスポンスを受信する。セキュリティ資格の勉強でどうしてもイメージをつかみたくて調べた結果</p>
<p>実際にこのコードで動かしてみると、自宅のUpNP対応機器からレスポンスが返ってきた。</p>
<pre class="wp-block-preformatted"># Define the M-SEARCH message
$searchMessage = "M-SEARCH * HTTP/1.1`r`n" +
"Host: 239.255.255.250:1900`r`n" +
"ST: upnp:rootdevice`r`n" +
"Man: ssdp:discover`r`n" +
"MX: 3`r`n" +
"`r`n"
# Create a UDP client
$client = New-Object System.Net.Sockets.UdpClient
# Bind the client to the local endpoint
$localEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
$client.Client.Bind($localEP)
# Set the multicast TTL to 1 (for local network only)
$client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::MulticastTimeToLive, 1)
# Join the multicast group
$multicastAddress = [System.Net.IPAddress]::Parse("239.255.255.250")
$client.JoinMulticastGroup($multicastAddress)
# Send the M-SEARCH message to the multicast address
$remoteEP = New-Object System.Net.IPEndPoint($multicastAddress, 1900)
$bytes = [System.Text.Encoding]::ASCII.GetBytes($searchMessage)
$client.Send($bytes, $bytes.Length, $remoteEP)
# Receive responses from the UPnP devices
$timeout = 5000 # 5 seconds
$startTime = [System.DateTime]::Now
while (([System.DateTime]::Now - $startTime).TotalMilliseconds -lt $timeout) {
if ($client.Available -gt 0) {
$remoteEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
$responseBytes = $client.Receive([ref]$remoteEP)
$responseMessage = [System.Text.Encoding]::ASCII.GetString($responseBytes)
# TODO: Process the response message
Write-Host "Response received from $($remoteEP.Address):`n$responseMessage`n"
}
}
# Close the UDP client
$client.Close()
</pre>
PowerShellでM-SEARCHを実装する例(chat-gpt活用)
UPnP機器を探すためのM-SEARCHメッセージをマルチキャストアドレスに送信して、レスポンスを受信する。セキュリティ資格の勉強でどうしてもイメージをつかみたくて調べた結果
実際にこのコードで動かしてみると、自宅のUpNP対応機器からレスポンスが返ってきた。
# Define the M-SEARCH message
$searchMessage = "M-SEARCH * HTTP/1.1`r`n" +
"Host: 239.255.255.250:1900`r`n" +
"ST: upnp:rootdevice`r`n" +
"Man: ssdp:discover`r`n" +
"MX: 3`r`n" +
"`r`n"
# Create a UDP client
$client = New-Object System.Net.Sockets.UdpClient
# Bind the client to the local endpoint
$localEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
$client.Client.Bind($localEP)
# Set the multicast TTL to 1 (for local network only)
$client.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::MulticastTimeToLive, 1)
# Join the multicast group
$multicastAddress = [System.Net.IPAddress]::Parse("239.255.255.250")
$client.JoinMulticastGroup($multicastAddress)
# Send the M-SEARCH message to the multicast address
$remoteEP = New-Object System.Net.IPEndPoint($multicastAddress, 1900)
$bytes = [System.Text.Encoding]::ASCII.GetBytes($searchMessage)
$client.Send($bytes, $bytes.Length, $remoteEP)
# Receive responses from the UPnP devices
$timeout = 5000 # 5 seconds
$startTime = [System.DateTime]::Now
while (([System.DateTime]::Now - $startTime).TotalMilliseconds -lt $timeout) {
if ($client.Available -gt 0) {
$remoteEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
$responseBytes = $client.Receive([ref]$remoteEP)
$responseMessage = [System.Text.Encoding]::ASCII.GetString($responseBytes)
# TODO: Process the response message
Write-Host "Response received from $($remoteEP.Address):`n$responseMessage`n"
}
}
# Close the UDP client
$client.Close()
ライセンス:本記事のテキスト/コードは特記なき限り
CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。
コメント