#TIL : Run shell command in all hosts

To run a shell command in all hosts, you can use the module name raw and provide shell command to module args.

Example:

  • To list all CPU model name of hosts
1
$ ansible all -m raw -a "cat /proc/cpuinfo | grep 'model name'"

#TIL : Can not run downloaded binary inside alpine linux because of missing shared libs

Alpine linux becomes the most base image for docker images because it’s lightweight and handful package manager apk. Sometimes, you create an image that downloads the binary file but can not execute it. It shows something like this:

1
/entrypoint.sh: line ***: [your binary]: not found

The problem is your binary built within shared libraries, so it can’t run without shared libraries dependencies. To findout which libraries is missing, use this

1
$ ldd [your binary path]

This is sample result

1
2
3
4
5
6
7
8
/usr/local/bin # ldd hugo
/lib64/ld-linux-x86-64.so.2 (0x7fa852f2a000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fa852f2a000)
Error loading shared library libstdc++.so.6: No such file or directory (needed by hugo)
libdl.so.2 => /lib64/ld-linux-x86-64.so.2 (0x7fa852f2a000)
libm.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fa852f2a000)
Error loading shared library libgcc_s.so.1: No such file or directory (needed by hugo)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fa852f2a000)

So we need to install libstdc++ and libc6-compat before run the binary

1
RUN apk add --no-cache libstdc++ libc6-compat

#TIL : Critical notice of string DOM manipulation using jQuery

Sometimes you want to manipulate the HTML DOM elements inside as a string, then you found a lot of HTML parser or DOM library from the Internet (but it supports only NodeJS). How to do it in a browser ?

The answer is “jQuery is your best friend in browser environment” :)

Then you try this :

1
2
3
4
5
const content = jQuery('<p><strong>Hello</strong></p><p>from</p><p><strong>KhanhIceTea</strong></p>');
content.find('p > strong').each(function(i, ele) {
$(ele).css('color', 'red');
});
console.log(content.html());

What you expected

1
<p><strong style="color: red;">Hello</strong></p><p>from</p><p><strong style="color: red;">KhanhIceTea</strong></p>

But the console print

1
<strong>Hello</strong>

SURPRISE !?? JQUERY SUCKS ?

Nope ! The reason is simple, DOM data structure is a tree. And, any tree has a root, right ??? Now you understand the problem ? Then we fix it below

1
2
3
4
5
6
7
8
const html_content = '<p><strong>Hello</strong></p><p>from</p><p><strong>KhanhIceTea</strong></p>';
// Wrap all elements into one root element (div)
const content = jQuery('<div />').append(html_content);
// or const content = jQuery('<div>' + html_content + '</div>');
content.find('p > strong').each(function(i, ele) {
$(ele).css('color', 'red');
});
console.log(content.html());

#TIL : SSH to docker host in Docker for Mac

When you need to debug the docker host of your docker server inside macOS. You can connect to its tty screen by

1
$ screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

Then type ENTER to enter the screen and start debugging docker host.

To disconnect the screen, press “Ctrl + A > Ctrl + " and confirm “yes”.

#TIL : View function source in developer tool console

If you are in console of developer tool and you want to know what the function does, you can view its source by:

  1. call .toSource() of variable or function name in Firefox

Example :

1
2
3
4
>> function hello(name) { return "Hello " + name; }

>> hello.toSource();
<- "function hello(name) { return \"Hello \" + name; }"
  1. click ‘show function definition’ in Chrome

show function definition

#TIL : Make cron job runs every seconds

Cron is a UNIX service that helps user run command periodically. And crontab is simple tool to setup cron job as user, just type in the command crontab -e then setup your cron schedule.

Btw, sometimes you want to run the cronjob every seconds (5, 10 or 20 seconds), but crontab only support every minute. How to achieve your goal without using another tool ?

I got an idea that we can use the sleep command to make it done. So this is solution.

This below is crontab rule that run command every 10 seconds.

* * * * * [command]
* * * * * sleep 10 && [command]
* * * * * sleep 20 && [command]
* * * * * sleep 30 && [command]
* * * * * sleep 40 && [command]
* * * * * sleep 50 && [command]

It’s simple, right ? ;)

#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