PowerShell send and receive TCP message packets
Https://www.cnblogs.com/fuhj02/archive/2012/10/16/2725609.html
In the previous article, we created the Test-tcpport function in the Psnet package to probe whether the specified port of the specified IP is open, and what most people think of after detecting the port is the need to send and receive TCP message packets through PowerShell. This article will describe how to create functions receive-and tcpmessagesend-tcpmessage for TCP message packets in the Psnet package. In order to take the idea of the Psnet toolset we created in the previous chapter, after determining the name of the function, create the. ps1 file corresponding to the above two functions in the $env:psspace/psnet/tcpop/.
Next, add the following two statements in $ENV:PSSPACE/PSNET/PSNET.PSM1 to introduce the two function files into the toolset:
. $env:P Sspace/psnet/tcpop/receive-tcpmessage.ps1
. $env:P Sspace/psnet/tcpop/send-tcpmessage.ps1
Then add the following code in the. ps1 file that you created separately:
=====File name: 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{}
}
: Lin ali and start the sys file name messages otn loopback
PowerShell send and receive TCP message package
https://www.cnblogs.com/fuhj02/archive/2012/10/16/2725609.html
In the last article, we created the Test-TCPPort function in the PSNet package to detect whether the specified port of the specified IP is open. After detecting the port, most people may think of the need to send and receive TCP message packets through PowerShell. This article It will describe how to create functions Receive- and TCPMessageSend-TCPMessage for TCP message packets in the PSNet package. In order to inherit the idea of the PSNet tool set we created in the previous article, after determining the function name, create the .ps1 files corresponding to the above two functions and place them in $env:PSSpace/PSNet/TCPOp/.
Next, add the following two statements in $env:PSSpace/PSNet/PSNet.psm1 to introduce the above two function files in the tool set:
. $env:PSSpace/PSNet/TCPOp/Receive-TCPMessage.ps1
. $env:PSSpace/PSNet/TCPOp/Send-TCPMessage.ps1
Then add the following codes to the created .ps1 files:
=====File name: 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{}
}
=====File name: Send-TCPMessage.ps1=====
Function Send-TCPMessage
{
param ([ValidateNotNullOrEmpty()]
[string] $EndPoint,
[int] $Port,
[string] $Message)
$IP = [System.Net.Dns]::GetHostAddresses($EndPoint)
$Address = [System.Net.IPAddress]::Parse($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 the two PowerShell processes separately and import the Psnet Module separately:
Import-module $env:P sspace\psnet
Either specify parameters when you start PowerShell, or create a batch script with the following statements to start the PowerShell process that loads the specified module.
Start%windir%\system32\windowspowershell\v1.0\powershell.exe-noexit-command "Import-module '%PSSpace%\PSNet '"
First, in one of the PowerShell windows, specify the port with the Receive-tcpmessage function to listen on the development port, waiting for the data to be accepted
Receive-tcpmessage 8080
Send data to the above port in a different window:
Send-tcpmessage 127.0.0.1 8080 "This a Message Send from psnet!"
Will find that the process sends a message, the previous process can receive the corresponding message, if there are spaces need to use double quotation marks including to ensure that the PowerShell interpretation engine knows that this is a complete parameter. Although these two small functions are simple, and the message is sent in ASCII way, this is to show you the effect of sending and receiving, in the real environment is still problematic, but these two functions will play an 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.
Author: Pay the Navy
Source: http://fuhj02.cnblogs.com
Copyright: This article is owned by the author and the blog Park
Reprint: Welcome reprint, in order to preserve the author's passion for Creation, please "reprint" according to the requirements, thank you
Requirements: This statement must be retained without the consent of the author; the original text must be connected in the article and the content is guaranteed to be complete! Otherwise the legal responsibility!
Personal website: http://txj.shell.tor.hu/
PowerShell send and receive TCP message packets