adam.nz aboutpostsprojectscontact
Talking to TCP Services

Talking to TCP Services by Adam Shand

It's easy to forget how to telnet to a port and talk to a daemon.

HTTP

HTTP 1.1 - Without authentication

# telnet maxx.shmoo.com 80 
Trying 205.134.188.171...
Connected to maxx.shmoo.com.
Escape character is '^]'.

GET / HTTP/1.1
Host: adam.shand.net

HTTP/1.1 200 OK
Date: Wed, 18 Mar 2009 22:02:05 GMT
Server: Apache/2.2.9 (Debian) DAV/2 PHP/5.2.6-1+lenny2 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8g
Last-Modified: Sun, 20 Jul 2008 08:06:39 GMT
ETag: "2a810d8-131a-45270120561c0"
Accept-Ranges: bytes
Content-Length: 4890
Content-Type: text/html; charset=UTF-8

HTTP 1.1 - With failed authentication

# telnet maxx.shmoo.com 80 
Trying 67.207.134.156...
Connected to maxx.shmoo.com.
Escape character is '^]'.

GET / HTTP/1.1
Host: maxx.shmoo.com
Authorization: Basic YWRhbTpzZWNyZXQK=

HTTP/1.1 401 Authorization Required
Date: Wed, 18 Mar 2009 22:03:52 GMT
Server: Apache/2.2.8 (Debian) PHP/5.2.5-3 with Suhosin-Patch
WWW-Authenticate: Basic realm="maxx.shmoo.com"
Content-Length: 516
Content-Type: text/html; charset=iso-8859-1

HTTP 1.1 - With successful authentication

# telnet maxx.shmoo.com 80 
Trying 192.168.17.179...
Connected to maxx.shmoo.com.
Escape character is '^]'.

GET /nothere HTTP/1.1
Host: maxx.shmoo.com
Authorization: Basic YWRhbTpzZWNyZXQK=

HTTP/1.1 404 Not Found
Date: Wed, 18 Mar 2009 22:07:53 GMT
Server: Apache/1.3.34 (Debian) mod_perl/1.29 DAV/1.0.3 PHP/5.2.0-8+etch11
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

HTTPS

HTTPS 1.1 - Without authentication

# openssl s_client -quiet -host maxx.shmoo.com -port 443
depth=0 C = US, ST = Maryland, L = Beltsville, O = The Shmoo Group, CN = maxx.shmoo.com, emailAddress = gdead@shmoo.com
verify error:num=18:self signed certificate
verify return:1
depth=0 C = US, ST = Maryland, L = Beltsville, O = The Shmoo Group, CN = maxx.shmoo.com, emailAddress = gdead@shmoo.com
verify error:num=10:certificate has expired
notAfter=Oct 22 17:37:18 2017 GMT
verify return:1
depth=0 C = US, ST = Maryland, L = Beltsville, O = The Shmoo Group, CN = maxx.shmoo.com, emailAddress = gdead@shmoo.com
notAfter=Oct 22 17:37:18 2017 GMT
verify return:1

GET / HTTP/1.1 Host: adam.shand.net HTTP/1.1 200 OK Date: Thu, 16 Jan 2020 21:20:01 GMT Server: Apache/2.4.18 (Ubuntu) Last-Modified: Sat, 17 Dec 2016 00:13:59 GMT ETag: "d-543cf902bd308" Accept-Ranges: bytes Content-Length: 13 Content-Type: text/html
Where's Wik? ^D

Note that the 404 response for the authenticated example is because I deliberated specified a URL that doesn't exist.

IMAP

Need to remember details of how to list, retrieve and delete messages.

# telnet localhost 143
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK dovecot ready.
AA CAPABILITY
* CAPABILITY IMAP4rev1 SORT THREAD=REFERENCES MULTIAPPEND UNSELECT LITERAL+ IDLE CHILDREN LISTEXT LIST-SUBSCRIBED STARTTLS AUTH=PLAIN
AA OK Capability completed.
AD LOGIN adam secret
AD OK Logged in.
A4 LIST "" *
* LIST (\HasChildren) "." INBOX
* LIST (\HasNoChildren) "." "Foo"
* LIST (\HasChildren) "." "Bar"
A5 SELECT Foo
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 194 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1047965829] UIDs valid
* OK [UIDNEXT 272] Predicted next UIDa5 OK [READ-WRITE] Select completed.
AE logout
* BYE Logging out
AE OK Logout completed.
Connection closed by foreign host.

NNTP

# telnet localhost 119
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
200 news NNTPCache server V3.0.1 [see www.nntpcache.org] (c) 1996-2002 Julian Assange <proff@iq.org> May 27 2004 ready (posting ok, 72154 groups available).
215
24hoursupport.helpdesk 730507 600741 y
...<snip>...
gmane.comp.java.dna.devel 15 1 m
.
listgroup gmane.comp.java.dna.devel
211 Article list follows
1...
<snip>...
15
.
GROUP gmane.comp.java.dna.devel
211 15 1 15 gmane.comp.java.dna.devel
article 12
220 12 <c76j3b$neq$1@sea.gmane.org> article
Path: main.gmane.org!not-for-mail
From: Mauro Talevi <mauro.talevi-hQ+s5KbX5YmGglJvpFV4uA@public.gmane.org>
Newsgroups: gmane.comp.java.dna.devel
Subject: Re: Release 1.1
Date: Tue, 04 May 2004 00:09:13 +0100
Lines: 10
...<snip>....
quit
205Connection closed by foreign host.

POP3

# telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK dovecot ready.
user adam
+OK
pass secret
+OK Logged in.
list
+OK scan listing follows
1 2667
2 2763
...<snip> ...
1120 2490
1121 122715
1122 2750
.
retr 1120
+OK 2490 octets
Return-Path: <support@...>
X-Original-To: adam@...
Delivered-To: adam@...
Received: from flexo.sydneywireless.com (flexo.sydneywireless.com [202.93.176.34])
by ronin.spack.org (Postfix) with ESMTP id E257C2C103
for <adam@...>; Sun, 13 Jun 2004 16:54:12 -0700 (PDT)
Received: (qmail 1173 invoked by uid 0); 13 Jun 2004 23:54:04 -0000
Date: 13 Jun 2004 23:54:04 -0000
Message-ID: <20040613235404.1172.qmail@flexo.sydneywireless.com>
To: adam@...
Subject: Your new certificate for ssl.spack.org
From: support@...

-----BEGIN CERTIFICATE-----
MIIFRDCCAyygAwIBAgIDAJG5MA0GCSqGSIb3DQEBBAUAMHkxEDAOBgNVBAoTB1Jv
...<snip> ...
5o4Wey39C/U=
-----END CERTIFICATE-----
.
dele 1120
+OK Marked to be deleted.
quit
+OK Logging out, messages deleted.
Connection closed by foreign host.

SMTP

SMTP without authentication

# telnet localhost 25 
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 ronin.spack.org ESMTP We own your foo.
ehlo ronin.spack.org
250-ronin.spack.org
250-PIPELINING
250-SIZE 10240000
250-VRFY250-ETRN
250-XVERP
250 8BITMIME
MAIL FROM: adam@domain.com
250 Ok
RCPT TO: adam@otherdomain.com
250 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
Subject: there must be a blank line between the end of the headers and the start of the message body

this is the message body, type a period on a line by itself to end the message body.
.

250 Ok: queued as 9AA8D2C125
quit
221 Bye
Connection closed by foreign host.

SMTP with STARTLS and SMTPAUTH (AUTH LOGIN)

On the below server there are two authentication modes available: AUTH LOGIN and AUTH PLAIN. Depending on which you choose the AUTH line looks different.

Below is an example of AUTH LOGIN. Note the weird 334 lines, they are base64 encoded "Username:" and "Password:" prompts. After each prompt, enter a base64 encoded username/password (without the trailing ='s).

# echo relay@spack.org | base64
cmVsYXlAc3BhY2sub3JnCg==

# echo super.secret | base64
c3VwZXIuc2VjcmV0Cg==

# swaks -tls --server mera.spack.org --to adam@hotmail.com --from relay@spack.org --auth plain --auth-user relay@spack.org --header-X-Test "test email"

Password:super.secret

=== Trying mera.spack.org:25…
=== Connected to mera.spack.org.
-> STARTTLS
<- 220 2.0.0 Ready to start TLS
~> EHLO puka.local
<~ 250-mera.spack.org
<~ 250-AUTH PLAIN LOGIN
<~ 250-AUTH=PLAIN LOGIN
~> AUTH LOGIN
<~ 334 VXNlcm5hbWU6
~> cmVsYXlAc3BhY2sub3JnCg
<~ 334 UGFzc3dvcmQ6
~> c3VwZXIuc2VjcmV0Cg
<~ 235 2.7.0 Authentication successful
~> MAIL FROM:relay@mera.spack.org
<~ 250 2.1.0 Ok
~> RCPT TO:adam@hotmail.com
<~ 250 2.1.5 Ok
~> DATA
<~ 354 End data with .
~> Date: Wed, 28 Apr 2021 15:21:18 +1200
~> To: adam@hotmail.com
~> From: relay@mera.spack.org
~> Subject: test Wed, 28 Apr 2021 15:21:18 +1200
~> Message-Id: 20210428152118.017093@puka.local
~> X-Mailer: swaks v20201014.0
~> X-Test: test email
~>
~> This is a test mailing
~>
~> .
<~ 250 2.0.0 Ok: queued as 689F1BF700
~> QUIT
<~ 221 2.0.0 Bye === Connection closed with remote host.

Alternatively AUTH PLAIN authenticates on a single line with a null character delimited, base64 encoded username / password combination (eg. \0adam\0secret). I haven't figured out how to make this work via telnet yet.

tutorial posted on 31 Dec 1999 in #nerding & #teaching

Copyheart 1994–2024 Adam Shand. Sharing is an act of love.