PowerShell/Scripts/send-tcp.ps1

40 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-02-09 19:30:45 +01:00
#!/bin/powershell
2020-12-29 15:14:21 +01:00
<#
.SYNTAX ./send-tcp.ps1 [<target-IP>] [<target-port>] [<message>]
.DESCRIPTION sends a TCP message to the given IP address and port
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
2020-12-09 10:22:41 +01:00
2021-01-02 11:08:59 +01:00
param([string]$TargetIP = "", [int]$TargetPort = 0, [string]$Message = "")
2020-12-09 10:22:41 +01:00
try {
2020-12-25 11:30:43 +01: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()
2020-12-25 11:30:43 +01:00
write-output "Done."
2020-12-09 10:22:41 +01:00
exit 0
} catch {
2020-12-25 11:30:43 +01:00
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:22:41 +01:00
exit 1
}