我正在尝试编写一个脚本来使用 PowerShell 进行测试和应用。测试应包括通过 UDP 向远程服务器发送字符串,然后读取该服务器的响应并对结果执行某些操作。我需要的唯一帮助是脚本的中间两个步骤(“发送字符串”,然后“接收响应”):
- 在端口 UDP 5000 上将字符串“ABCDEFG”发送到服务器 10.10.10.1
- 接收来自服务器 10.10.10.1 的响应
我对 PowerShell 比较熟悉,但这是我第一次处理套接字,所以我处于陌生的领域,而且我似乎无法理解帖子中找到的几个示例。
不久前,我编写了一个简单的 PowerShell 脚本来发送 UDP 数据报。看:http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html这会让你成功一半。不过我从来没有做过另一半并写过服务器端!
<#
.SYNOPSIS
Sends a UDP datagram to a port
.DESCRIPTION
This script used system.net.socckets to send a UDP
datagram to a particular port. Being UDP, there's
no way to determine if the UDP datagram actually
was received.
for this sample, a port was chosen (20000).
.NOTES
File Name : Send-UDPDatagram
Author : Thomas Lee - [email protected] /cdn-cgi/l/email-protection
Requires : PowerShell V2 CTP3
.LINK
http://www.pshscripts.blogspot.com
.EXAMPLE
#>
###
# Start of Script
##
# Define port and target IP address
# Random here!
[int] $Port = 20000
$IP = "10.10.1.100"
$Address = [system.net.IPAddress]::Parse($IP)
# Create IP Endpoint
$End = New-Object System.Net.IPEndPoint $address, $port
# Create Socket
$Saddrf = [System.Net.Sockets.AddressFamily]::InterNetwork
$Stype = [System.Net.Sockets.SocketType]::Dgram
$Ptype = [System.Net.Sockets.ProtocolType]::UDP
$Sock = New-Object System.Net.Sockets.Socket $saddrf, $stype, $ptype
$Sock.TTL = 26
# Connect to socket
$sock.Connect($end)
# Create encoded buffer
$Enc = [System.Text.Encoding]::ASCII
$Message = "Jerry Garcia Rocks`n"*10
$Buffer = $Enc.GetBytes($Message)
# Send the buffer
$Sent = $Sock.Send($Buffer)
"{0} characters sent to: {1} " -f $Sent,$IP
"Message is:"
$Message
# End of Script
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)