2022-12-04 10:40:18 +01:00
|
|
|
## The *send-tcp.ps1* Script
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script sends a TCP message to the given IP address and port.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
```powershell
|
2023-05-26 12:20:18 +02:00
|
|
|
/home/mf/Repos/PowerShell/Scripts/send-tcp.ps1 [[-TargetIP] <String>] [[-TargetPort] <Int32>] [[-Message] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
-TargetIP <String>
|
|
|
|
Specifies the target IP address
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-TargetPort <Int32>
|
|
|
|
Specifies the target port number
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value 0
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-Message <String>
|
|
|
|
Specifies the message to send
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 3
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
```powershell
|
|
|
|
PS> ./send-tcp 192.168.100.100 8080 "TEST"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Notes
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
## Related Links
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2022-11-17 20:02:26 +01:00
|
|
|
## Source Code
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Sends a TCP message to an IP address and port
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script sends a TCP message to the given IP address and port.
|
|
|
|
.PARAMETER TargetIP
|
|
|
|
Specifies the target IP address
|
|
|
|
.PARAMETER TargetPort
|
|
|
|
Specifies the target port number
|
|
|
|
.PARAMETER Message
|
|
|
|
Specifies the message to send
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./send-tcp 192.168.100.100 8080 "TEST"
|
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$TargetIP = "", [int]$TargetPort = 0, [string]$Message = "")
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($TargetIP -eq "" ) { $TargetIP = read-host "Enter target IP address" }
|
|
|
|
if ($TargetPort -eq 0 ) { $TargetPort = read-host "Enter target port" }
|
|
|
|
if ($Message -eq "" ) { $Message = read-host "Enter message to send" }
|
|
|
|
|
|
|
|
$IP = [System.Net.Dns]::GetHostAddresses($TargetIP)
|
|
|
|
$Address = [System.Net.IPAddress]::Parse($IP)
|
|
|
|
$Socket = New-Object System.Net.Sockets.TCPClient($Address,$TargetPort)
|
|
|
|
$Stream = $Socket.GetStream()
|
|
|
|
$Writer = New-Object System.IO.StreamWriter($Stream)
|
|
|
|
$Message | % {
|
|
|
|
$Writer.WriteLine($_)
|
|
|
|
$Writer.Flush()
|
|
|
|
}
|
|
|
|
$Stream.Close()
|
|
|
|
$Socket.Close()
|
|
|
|
|
|
|
|
"✔️ Done."
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2021-11-08 21:36:42 +01:00
|
|
|
*Generated by convert-ps2md.ps1 using the comment-based help of send-tcp.ps1*
|