PowerShell TCP Message packets for script development _powershell

Source: Internet
Author: User
Tags getstream

In the last article, we created the Test-tcpport function in the Psnet package to detect whether the specified IP port is open, and most people think of it as having to send and receive TCP message packets via PowerShell. This article will describe how to create functions receive-and tcpmessagesend-tcpmessage for TCP message packs in the Psnet package. To take the idea of the Psnet toolset we created in the previous article, after we have defined the name of the function, we create the corresponding. ps1 file for the two functions to be placed in the $env:psspace/psnet/tcpop/.

Next, add the following two statements to the $ENV:PSSPACE/PSNET/PSNET.PSM1 to introduce the two function files in the toolset:

Copy Code code as follows:

. $env:P Sspace/psnet/tcpop/receive-tcpmessage.ps1
. $env:P Sspace/psnet/tcpop/send-tcpmessage.ps1

The following code is then added to the. ps1 file that is created, respectively:

Copy Code code as follows:

===== FileName: receive-tcpmessage.ps1=====
Function Receive-tcpmessage
{
Param ([Validatenotnullorempty ()]
[INT] $Port)
Try
{
$EndPoint = New-object System.Net.IPEndPoint ([System.net.ipaddress]::loopback, $Port)
$Socket = New-object System.Net.Sockets.TCPListener ($EndPoint)
$Socket. Start ()
$Socket = $Socket. AcceptTcpClient ()
$EncodedText = New-object System.Text.ASCIIEncoding
$Stream = $Socket. GetStream ()
$Buffer = New-object system.byte[] $Socket. receivebuffersize
while ($Bytes = $Stream. Read ($Buffer, 0, $Buffer. Length))
{
$Stream. Write ($Buffer, 0, $Bytes)
Write-output $EncodedText. GetString ($Buffer, 0, $Bytes)
}
$Socket. Close ()
$Socket. Stop ()
}
catch{}
}

Copy Code code as follows:

===== FileName: send-tcpmessage.ps1=====
Function Send-tcpmessage
{
Param ([Validatenotnullorempty ()]
[String] $EndPoint,
[INT] $Port,
[String] $Message)

$IP = [System.net.dns]::gethostaddresses ($EndPoint)
$Address = [System.Net.IPAddress]::P arse ($IP)
$Socket = New-object System.Net.Sockets.TCPClient ($Address, $Port)
$Stream = $Socket. GetStream ()
$Writer = New-object System.IO.StreamWriter ($Stream)
$Writer. AutoFlush = $true
$Writer. NewLine = $true
$Writer. Write ($Message)
$Socket. Close ()
}

After saving the code to the corresponding file in the specified directory, start two PowerShell processes, and import the psnet Module separately:

Copy Code code as follows:

Import-module $env:P sspace\psnet

Either specify parameters when you start PowerShell, or create a batch script with the following statements to start loading the PowerShell process for the specified module.

Copy Code code as follows:

Start%windir%\system32\windowspowershell\v1.0\powershell.exe-noexit-command "Import-module '%PSSpace%\PSNet '"

First in one of the PowerShell windows, the specified port listens with the Receive-tcpmessage function on the development port, waiting for data to be accepted

Copy Code code as follows:

Receive-tcpmessage 8080

Send data to the above port in another window:

Copy Code code as follows:

Send-tcpmessage 127.0.0.1 8080 "This a message Send from psnet!"

You will find that the process sends a message, and the previous process receives the corresponding message, and if there are spaces in the message that need double quotes, include ensuring that the PowerShell interpretation engine knows this is a complete parameter. These two small functions, though simple in function, and the message is sent in ASCII, this is to show you the effect of sending and receiving, there is still a problem in the real environment, but these two functions will play a very important role in the subsequent functions, Using PowerShell to send TCP and receive TCP messages will be used and will be improved in subsequent articles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.