-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisten.ps1
61 lines (53 loc) · 1.96 KB
/
listen.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Function Receive-TCPMessage {
Param (
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[int] $Port
)
Process {
Try {
# Set up endpoint and start listening
$endpoint = new-object System.Net.IPEndPoint([ipaddress]::any,$port)
$listener = new-object System.Net.Sockets.TcpListener $EndPoint
$listener.start()
While ($true){
# Wait for an incoming connection
$data = $listener.AcceptTcpClient()
# Stream setup
$stream = $data.GetStream()
$bytes = New-Object System.Byte[] 1024
# Read data from stream and write it to host
while (($i = $stream.Read($bytes,0,$bytes.Length)) -ne 0){
$EncodedText = New-Object System.Text.ASCIIEncoding
$data = $EncodedText.GetString($bytes,0, $i)
$data = [string]$data.trim()
if($data -match 'Jesus'){
Write-Host $data -ForegroundColor Red
}elseif($data -match 'Diego'){
Write-Host $data -ForegroundColor Green
}
if($data.trim() -match 'close'){
echo 'cerrado'
$stream.close()
$listener.stop()
exit
}elseif($data.trim() -match 'cls'){
cls
}
}
# Close TCP connection and stop listening
$stream.close()
}
}
Catch {
"Receive Message failed with: `n" + $Error[0]
}
finally{
$listener.stop()
Write-host "Listener Closed Safely"
}
}
}
cls
Receive-TCPMessage -Port 29800