mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / Simple_servers.mdwn
1 After reading Brian Hatch's [Nmap Version Detection Rocks][nmap], I
2 wanted to set up a simple socket-connection test.  Here are my notes:
3
4 Start a plain-text socket echoing incoming text using [netcat][]:
5
6     a$ nc -l -p 8080
7
8 The `-l` (listen) switches netcat into server mode.  I was a bit
9 confused by the `<host>` and `<port>` arguments to `nc -l`.  It turns
10 out that they do not specify which address netcat binds to; they limit
11 the *connecting* host.  Something like
12
13     a$ nc -l -p 8080 b.example.net 12345
14
15 will only accept connections originating from port `12345` on
16 `b.example.net`.
17
18 Echo text to that port
19
20     b$ echo 'hi there' | nc -q 1 a.example.net 8080
21
22 To connect from a specific port, use the `-p` option.
23
24     b$ echo 'hi there' | nc -q 1 -p 12345 a.example.net 8080
25
26 The `-q 1` tells netcat to quit after an EOF is detected.  When the
27 client quits, the connection breaks, and the server goes down on its
28 own.  If you want netcat to stay up you'll have to restart it:
29
30     $ while nc -l -p 8080; do :; done
31
32 The `:` is Bash's noop.
33
34 If you just want a simple telnet-style connection use:
35
36     b$ nc a.example.net 8080
37
38 For a secure connection, use [OpenSSL][] (see [[X.509_certificates]]
39 for more on generating keys and certificates):
40
41     a$ openssl s_server -key key.pem -cert cert.pem -accept 8080
42
43 Connect with
44
45     b$ echo 'hi there' | openssl s_client -connect a.example.net:8080
46
47 The OpenSSH client automatically closes on EOF, but the server stays
48 open for new connections.  You'll have to kill it manually when you're
49 done.
50
51 If you don't like OpenSSL, you can also use [stunnel][] for SSL
52 connections.  Examples are all over.  [This post][DB] by Dustin Breese
53 seems good.
54
55 Also note that with the `crypt` USE flag, Gentoo will install netcat
56 with an [AES][] patch by [Mixter][], which allows
57
58     $ nc -k <password> -l -p
59     $ nc -k <password> <options> <host> <port>
60
61 AES is a symmetric-key encryption standard, so you don't have to go
62 through all the rigmarole of setting up SSL keys and certs for a
63 one-off connection.
64
65 [nmap]: http://www.hackinglinuxexposed.com/articles/20031006.html
66 [netcat]: http://nc110.sourceforge.net/
67 [OpenSSL]: http://www.openssl.org/
68 [stunnel]: http://stunnel.mirt.net/
69 [DB]: http://dustinbreese.blogspot.com/2008/09/stunnel.html
70 [AES]: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
71 [Mixter]: http://mixter.void.ru/
72
73 [[!tag tags/linux]]
74 [[!tag tags/tools]]