#TIL : Telnet server through SSL or TLS

I often use telnet and netcat to debug my TCP server and client. But these tool only support plain connection, mean every data transfer between server and client is unencrypted and unsafe.

So if you want to achieve the same result through secure connection (SSL or TLS), use this command

1
2
3
$ openssl s_client -host example.com -port 443
$ # or short syntax
$ openssl s_client -connect example.com:443

I made a function named telnets in .bash_profile to make it easier to use

1
2
3
function telnets() {
openssl s_client -host "$1" -port "$2"
}

then I just type this on bash shell within same syntax of telnet

1
$ telnets github.com 443

TIP: To hide detail of certificates, add -quiet flag into command

More info, check openssl s_client -h

#TIL : Curl extract info from verbose mode

Curl is great tool to do thing with HTTP in command line interface. Sometimes your want to get extra info from HTTP response and put in a variable. Here is the way :

Using --write-out is magical option help you to write out all info you want, or put it in a variable.

Example :

1
2
3
$ code=$(curl --write-out %{response_code} --silent --output /dev/null https://khanhicetea.com)
$ echo $code # get http response status code
200
1
2
3
$ tracetime=$(curl --write-out "%{time_namelookup} %{time_connect} %{time_appconnect} %{time_pretransfer} %{time_redirect} %{time_starttransfer} %{time_total}" --silent --output /dev/null https://khanhicetea.com)
$ echo $tracetime # Trace all timing of http connection (in seconds)
0.068 0.097 0.370 0.370 0.000 0.720 0.721

Use case :

Below code is cron bash script that checks if http response code equals :

  • 502 (Bad Gateway), then restart the backend server (nginx -> apache2)
  • not 200 then restart the frontend server (nginx)
1
2
3
4
5
#!/bin/bash

code=$(curl --write-out %{response_code} --silent --output /dev/null http://example.com)
[ $code -eq 502 ] && sudo systemctl restart httpd || echo "everything works fine"
[ ! $code -eq 200 ] && sudo systemctl restart nginx || echo "everything works fine"

More info variables, you can check it here

#TIL : Internal Variables in BASH

$PWD

Your current working directory, so you don’t have to use CWD=$(pwd)

$OLDPWD

Your previous working directory

Note : You can jump directly to it by the command cd -

$SECONDS

The number of seconds the script has been running

You can use it for profiling or limiting timeout

1
2
3
4
5
6
7
8
TIME_LIMIT=60
START=$SECONDS
while [ $(($SECONDS - START)) -le "$TIME_LIMIT" ]
do
## Your work here
done

echo "It takes $SECONDS seconds to get here !"

$RANDOM

Get random integer number, for getting random name or just roll a dice ;)

lucky boy

#TIL : Simple HTTP server function helper

I use python3 (3.4+) to create a bash function to help me start quickly a simple http server on specified port

1
2
3
4
5
function server() {
local port="${1:-8000}" # Getting port number
google-chrome "http://127.0.0.1:$port" # Open URL in browser, could change to firefox --new-tab "http://127.0.0.1:$port"
python3 -m http.server $port --bind 127.0.0.1
}