• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > PowerShell脚本开发之收发UDP消息包

PowerShell脚本开发之收发UDP消息包

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了powershell脚本,powershell脚本下载,powershell脚本代码,powershell执行脚本,powershell 运行脚本等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在上篇文章中,在PSNet工具集中创建了Send-TCPMessage和Receive-TCPMessage两个函数实现了通过PowerShell收发TCP消息包的功能,有了TCP包的发送和接收,自然少不了UDP消息包的发送和接收,本文将会介绍通过PowerShell发送和接收UDP消息包的方法。

为了能跟之前的PSNet程序集匹配,继续基于此程序集进行扩展,在$env:PSSpace\PSNet下创建UDPOp目录,在其中创建Receive-UDPMessage.ps1和Send-UDPMessage.ps1两个文件,代码稍后插入。

在$env:PSSpace\PSNet\PSNet.psm1中添加对上述两个脚本文件的引用,代码如下:

. $env:PSSpace/PSNet/TCPOp/Receive-UDPMessage.ps1
. $env:PSSpace/PSNet/TCPOp/Send-UDPMessage.ps1
</div>

需要注意的是这两行代码需要在PSNet.psm1中已经存在的Export-ModuleMember -Function *代码之前,以便能保证被引入的脚本文件中包含的函数均可以作为该程序集模块的成员注册,以便能够被PowerShell进程识别。

下面附上代码:

 =====文件名:Receive-UDPMessage.ps1=====
Function Receive-UDPMessage
{
    param ( [ValidateNotNullOrEmpty()]
    [int] $Port )

    try
    {
        $EndPoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Loopback,$Port)
        $UDPClient = New-Object System.Net.Sockets.UDPClient($Port)
        $ReceiveMessage = $UDPClient.Receive([System.Management.Automation.PSReference]$EndPoint)
        [System.Text.Encoding]::ASCII.GetString($ReceiveMessage)
    }
    catch{}
}
</div>

        =====文件名:Send-UDPMessage.ps1=====
Function Send-UDPMessage
{
    param ( [ValidateNotNullOrEmpty()]
    [string] $EndPoint,
    [int] $Port,
    [string] $Message )

    $IP = [System.Net.Dns]::GetHostAddresses($EndPoint)
    $Address = [System.Net.IPAddress]::Parse($IP)
    $EndPoints = New-Object System.Net.IPEndPoint($Address,$Port)
    $Socket = New-Object System.Net.Sockets.UDPClient
    $EncodedText = [Text.Encoding]::ASCII.GetBytes($Message)
    $SendMessage = $Socket.Send($EncodedText,$EncodedText.Length,$EndPoints)
    $Socket.Close()
}
</div>

启动两个PowerShell进程分别导入PSNet模块:

Import-Module $env:PSSpace\PSNet
</div>

首先运行Receive-UDPMessage对端口进行监听:

Receive-UDPMessage 8080
</div>

然后通过Receive-UDPMessage函数对上面指定的端口发送消息:

Send-UDPMessage 127.0.0.1 8080 "This is a UDP Message Send from PSNet!"
</div>

发送之后会在前面的窗口中显示消息内容。

本文中的实例中为了显示发送效果,字符串传输过程中是ASCII字符的形式传送的,但是实际情况中通常是十六进制数据形式,在后续的文章中我们将会对本文的实例进行改进以便适应实际工作中的需要。

</div>

您可能想查找下面的文章:

  • PowerShell因为在此系统中禁止执行脚本的解决方法
  • PowerShell时间记录脚本
  • Powershell实现编写和运行脚本
  • PowerShell脚本反引号用法实例:随时随地给代码换行
  • Powershell脚本的4种执行权限介绍
  • PowerShell中获取当前运行脚本路径的方法
  • PowerShell实现动态获取当前脚本运行时消耗的内存
  • PowerShell实现的文件同步脚本分享
  • Powershell实现获取电脑序列号功能脚本分享
  • Powershell脚本中使用条件断点实例

相关文章

  • Windows Powershell 复制数组
  • Windows Powershell 进行数学运算
  • PowerShell脚本实现网卡DHCP自动获取IP地址、设置静态IP地址的方法
  • PowerShell函数中限制数组参数个数的例子
  • 探索PowerShell(五) PowerShell基础知识
  • Windows Powershell 命令集 cmdlets
  • Powershell学习笔记--使用正则表达式查找文件
  • Windows Powershell 环境变量
  • PowerShell查询和删除打印任务操作代码实例
  • Powershell生成Windows密码算法简单学习

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Powershell 获取特定的网页信息的代码
    • PowerShell定义函数参数的2种方法和传参方法实例
    • Powershell中使用WMI工具例子
    • PowerShell DSC组件 xExchange 发布
    • 探索PowerShell(十四) 使用WMI对象的方法
    • Powershell脚本中使用条件断点实例
    • Powershell中请求WebServices并以JSON格式输出结果
    • PowerShell中给函数参数设置帮助信息的例子
    • Powershell小技巧之使用WMI查询插上的U盘
    • Windows Powershell对象转换成文本

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有