PowerShell/Scripts/send-tcp.ps1

45 lines
1.3 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-16 16:50:10 +02:00
Sends a TCP message to an IP address and port
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-30 10:49:30 +01:00
This PowerShell script sends a TCP message to the given IP address and port.
2021-10-16 16:50:10 +02:00
.PARAMETER TargetIP
Specifies the target IP address
.PARAMETER TargetPort
Specifies the target port number
.PARAMETER Message
Specifies the message to send
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./send-tcp 192.168.100.100 8080 "TEST"
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-30 10:49:30 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-12-09 10:22:41 +01:00
2021-07-15 15:51:22 +02:00
param([string]$TargetIP = "", [int]$TargetPort = 0, [string]$Message = "")
2020-12-09 10:22:41 +01:00
try {
2021-07-15 15:51:22 +02:00
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" }
2020-12-09 10:22:41 +01:00
$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()
2021-08-24 20:46:03 +02:00
"✔️ Done."
2021-09-27 10:09:45 +02:00
exit 0 # success
2020-12-09 10:22:41 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:22:41 +01:00
exit 1
}