When we need to use sockets (or netcat nc
) to connect to a server, here are some snippets, in 4 different languages.
Python
1 2 3 4 5 6 7 8 9 10 11 12 13
| import socket from telnetlib import Telnet
s = socket.socket() s.connect(('YOUR IP', PORT)) s.send('Hello world!\n') print "> " + s.recv(1024)
t = new Telnet() t.s = s t.interact() s.close()
|
1 2 3 4 5 6 7 8 9
| from pwn import *
r = remote('YOUR IP', PORT) r.send("Hello world!\n") print "> " + r.recv() print r.recvuntil("END\n")
r.interactive()
|
Ruby
1 2 3 4 5 6
| require 'socket'
a = TCPSocket.new('YOUR IP', PORT) a.write "Hello world!" puts "> " + a.recv(1024) a.close
|
NodeJS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var net = require('net');
var client = new net.Socket(); client.connect(3333, '127.0.0.1', function() { client.write('Hello world!'); });
client.on('data', function(data) { console.log('> ' + data); client.destroy(); });
client.on('close', function() { console.log(''); });
|