在 PowerShell 中通过 UDP 发送和接收数据 [英] Send and Receive data via UDP in PowerShell

查看:39
本文介绍了在 PowerShell 中通过 UDP 发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写脚本以使用 PowerShell 进行测试和应用程序.测试应该包括通过 UDP 向远程服务器发送一个字符串,然后从该服务器读取响应并使用结果做一些事情.我需要的唯一帮助是脚本的中间两个步骤(发送字符串",然后是接收响应"):

  1. 在端口 UDP 5000 上将字符串ABCDEFG"发送到服务器 10.10.10.1
  2. 从服务器 10.10.10.1 接收响应

我对 PowerShell 比较熟悉,但这是我第一次不得不处理套接字,所以我在陌生的水域,我似乎无法理解我在帖子中找到的几个例子.

解决方案

前段时间,我编写了一个简单的 PowerShell 脚本来发送 UDP 数据报.请参阅:http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html 这会让你成功.我从来没有做过另一半,也没有写过这个的服务器端!

<块引用><预><代码><#.概要向端口发送 UDP 数据报.描述此脚本使用 system.net.sockets 发送 UDP到特定端口的数据报.作为UDP,有无法确定 UDP 数据报是否实际收到了.对于此示例,选择了一个端口 (20000)..笔记文件名:发送UDP数据报作者 : Thomas Lee - tfl@psp.co.uk要求:PowerShell V2 CTP3.关联http://www.pshscripts.blogspot.com.例子#>#### 脚本开始###定义端口和目标IP地址#这里随机![int] $Port = 20000$IP = "10.10.1.100"$Address = [system.net.IPAddress]::Parse($IP)# 创建 IP 端点$End = 新对象 System.Net.IPEndPoint $address, $port# 创建套接字$Saddrf = [System.Net.Sockets.AddressFamily]::InterNetwork$Stype = [System.Net.Sockets.SocketType]::Dgram$Ptype = [System.Net.Sockets.ProtocolType]::UDP$Sock = 新对象 System.Net.Sockets.Socket $saddrf, $stype, $ptype$Sock.TTL = 26# 连接到套接字$sock.Connect($end)# 创建编码缓冲区$Enc = [System.Text.Encoding]::ASCII$Message = "Jerry Garcia Rocks`n"*10$Buffer = $Enc.GetBytes($Message)# 发送缓冲区$Sent = $Sock.Send($Buffer){0} 个字符发送至:{1}" -f $Sent,$IP消息是:"$消息# 脚本结束

I am trying to write a script to test and application using PowerShell. The test should consist of sending a string to a remote server over UDP, then reading the response from that server and doing something with the result. The only help I need is with the middle two ('send string', then 'receive response') steps of the script:

  1. Send string "ABCDEFG" to server 10.10.10.1 on port UDP 5000
  2. Receive response from server 10.10.10.1

I am relatively familiar with PowerShell, but this is my first time having to deal with sockets, so I am in unfamiliar waters, and I can't seem to make sense of the few examples I have found on posts.

解决方案

Some time back, I wrote a simple PowerShell script to send a UDP Datagram. See: http://pshscripts.blogspot.co.uk/2008/12/send-udpdatagramps1.html which will get you half way there. I never did do the other half and write the server side of this though!

<#  
.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 - tfl@psp.co.uk 
    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 

这篇关于在 PowerShell 中通过 UDP 发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆