Crear una comunicación entre un cliente en PowerShell de Windows y un servidor en Node.js utilizando TCP/IP
Servidor en Node.js
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 | var net = require('net'); var HOST = '127.0.0.1'; var PORT = 6969; // Create a server instance, and chain the listen function to it // The function passed to net.createServer() becomes the event handler for the 'connection' event // The sock object the callback function receives UNIQUE for each connection net.createServer(function(sock) { // We have a connection - a socket object is assigned to the connection automatically console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort); // Add a 'data' event handler to this instance of socket sock.on('data', function(data) { console.log('DATA ' + sock.remoteAddress + ': ' + data); // Write the data back to the socket, the client will receive it as data from the server sock.write('You said "' + data + '"'); }); // Add a 'close' event handler to this instance of socket sock.on('close', function(data) { console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort); }); }).listen(PORT, HOST); console.log('Server listening on ' + HOST +':'+ PORT); |
Cliente en PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ##Client $port=6969 $TcpClient=New-Object System.Net.Sockets.TcpClient([IPAddress]::Loopback, $port) $GetStream = $TcpClient.GetStream() $StreamWriter = New-Object System.IO.StreamWriter $GetStream $positions='Hi' $StreamWriter.Write($positions) $StreamWriter.Dispose() $GetStream.Dispose() $TcpClient.Dispose() |
Relación entre puertos TCP y procesos (construir un objeto con propiedades personalizadas)
1 | Get-NetTCPConnection | Select-Object LocalAddress,LocalPort,OwningProcess,@{n='Nombre proceso';e={Get-Process -id ($_ | Select-Object -ExpandProperty OwningProcess) | Select-Object -ExpandProperty Name}} |
Enviar una imagen entre un cliente y un servidor con Java (Sockets TCP)
Servidor
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 | import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import java.nio.ByteBuffer; public class Server { public static void main(String[] args) { int port=2050; try { ServerSocket serverSocket = new ServerSocket(port); Socket clientSocket = serverSocket.accept(); InputStream inputStream = clientSocket.getInputStream(); System.out.println(inputStream); byte[] imageAr = new byte[62100]; inputStream.read(imageAr); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr)); System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis()); ImageIO.write(image, "jpg", new File("C:\\Users\\juan\\Desktop\\recono\\coche2.png")); inputStream.close(); clientSocket.close(); serverSocket.close(); } catch (UnknownHostException e){ System.out.println(e); } catch (IOException e) { System.out.println(e); } } } |
Cliente
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 | import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import java.nio.ByteBuffer; public class Client { public static void main(String[] args) { int port=2050; try { Socket clientSocket = new Socket("localhost",port); OutputStream outputStream = clientSocket.getOutputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); BufferedImage image = ImageIO.read(new File("C:\\Users\\juan\\Desktop\\recono\\coche.png")); ImageIO.write(image, "jpg", byteArrayOutputStream); byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array(); System.out.println(byteArrayOutputStream.size()); outputStream.write(byteArrayOutputStream.toByteArray()); Thread.sleep(2000); outputStream.close(); clientSocket.close(); } catch (UnknownHostException e){ System.out.println(e); } catch (IOException e) { System.out.println(e); } catch (InterruptedException e) { e.printStackTrace(); } } } |
Realizar conexiones con Java
Red Analizar direcciones IP para detectar puertos abiertos (versión 1) Analizar direcciones IP para detectar puertos abiertos (versión 2) TCP Comprobar si un puerto TCP está abierto Server and client (Sockets TCP) Multiserver and clients (Sockets TCP and threads) Enviar una imagen entre un cliente y un servidor con Java (Sockets TCP) Enviar mensajes constantemente entre cliente y servidor con Java UDP Server and client (Datagram Socket UDP) MulticastSocket (Datagram Socket UDP) URL Conectar con una URL en Java Conectar con una URL en Java cambiando el User-Agent Realizar petición HTTP mediante el método POST utilizando librerías de Apache
Comprobar constantemente si está abierto un puerto TCP
1 2 3 4 5 6 | while(1) { Get-NetTCPConnection | Select-Object LocalAddress,RemotePort,OwningProcess| Where-Object {$_.RemotePort -eq "443"} Start-Sleep -Seconds 5 clear } |
Curso de Java con ejemplos
Procesos Create operating system processes Create and destroy operating system processes Crear un proceso utilizando Runtime Crear un proceso utilizando ProcessBuilder Lanzar un programa del sistema operativo como argumento desde la línea de comandos Ejecutar y guardar el comando ‘arp’ utilizando ProcessBuilder Hilos Crear un hilo implementando el interfaz Runnable Crear un hilo heredando de la clase Thread Automatizar Obtener la posición del ratón en la pantalla con Java Ver la resolución de pantalla con Java Mover el ratón a una posición con Java Mover el ratón con duración a varias posiciones de la pantalla con Java Hacer clic en […]
Analizar el rendimiento de Linux realizando una conexión SSH desde PowerShell en Windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | New-SSHSession -ComputerName 192.168.1.162 -Credential (Get-Credential) #analizar.txt: #uptime #dmesg #vmstat #mpstat -P ALL #pidstat #iostat -xz #free -m #sar -n DEV #sar -n TCP,ETCP #ps gc analizar.txt | %{ $_ $resultado=Invoke-SSHCommand -Index 0 $_ "---------------------------------------------------------------" | Out-File resultado.txt -Append $resultado.Output | Out-File resultado.txt -Append Start-Sleep -Seconds 5 } |
Crear una comunicación entre un cliente en PowerShell de Windows y un servidor en Bash de Linux utilizando TCP/IP
Bash
1 2 3 | #Servidor #Abrir el puerto 5010 para establecer comunicación nc -l 5010 |
PowerShell
1 2 3 4 5 6 | #Cliente $ip=[IPAddress]"192.168.1.162" $TcpClient=New-Object System.Net.Sockets.TcpClient($ip, "5010") $mensaje=New-Object System.IO.StreamWriter $TcpClient.GetStream() $mensaje.Write("hola----------") $mensaje.Dispose() |
Crear una comunicación entre un cliente en Bash de Linux y un servidor en PowerShell de Windows utilizando TCP/IP
PowerShell
1 2 3 4 5 6 7 8 9 10 11 | #Server $ip=[IPAddress]"0.0.0.0" $TcpListener=New-Object System.Net.Sockets.TcpListener (New-Object System.Net.IPEndPoint($ip,"2052")) $TcpListener.Start() while($true) { $mensaje=(New-Object System.IO.StreamReader ($TcpListener.AcceptTcpClient().GetStream())).ReadLine() $mensaje } $TcpListener.Stop() |
Bash
1 2 | #Enviar hola al servidor que tiene la IP 192.168.1.157 y el puerto 2052 echo "hola" > /dev/tcp/192.168.1.157/2052 |