Curl Dump Response Headers to STDOUT and Ignore Response Body

Tags: May 7, 2018 6:48 AM
0 comments

Goals

Return only HTTP response header when opening a web page using curl. This is useful when we are interested in processing response headers only.

Command

We will utilize /dev/stdout and /dev/null to achieve what we want.

$ curl https://notes.rioastamal.net -D /dev/stdout -o /dev/null --silent
HTTP/2 200
date: Sun, 06 May 2018 23:42:37 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=d6347d57f364b276150034b241b19cdb01525650157; expires=Mon, 06-May-19 23:42:37 GMT; path=/; domain=.rioastamal.net; HttpOnly
expires: Sun, 06 May 2018 23:42:37 GMT
cache-control: private, max-age=0
last-modified: Sun, 06 May 2018 23:42:05 GMT
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 416f4deb6f51a320-HKG

Reference

Share on Facebook Twitter

Generate Random String in Shell Using /dev/urandom

Tags: May 3, 2018 8:18 AM
0 comments

Goals

Generate random string in Shell and using /dev/urandom as the source. This random string typically useful to be used as encryption key.

Implementation

We will use combination of tr and head to generate 32 random characters. Command below will only output alphanumeric and some characters symbol only.

$ </dev/urandom tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>[email protected][\]^_`{|}~' | head -c 32 && echo
}s9s2c8W7aZlI:yg<{bg&-<7YnyJEk.u

On Mac OS X system you may need to define LC_ALL=C environment variable as shown below.

$ LC_ALL=C </dev/urandom tr -dc 'A-Za-z0-9!"#$%&()*+,-./:;<=>[email protected][\]^_`{|}~' | head -c 32 && echo
f(s_TPj*.H3Z/s[*:zLe[=9&0$FF"*8[

References

Share on Facebook Twitter

How to Flatten Multidimensional Array in PHP

Tags: April 3, 2018 6:03 AM
0 comments

Goals

Turn PHP multi-dimensional array into one dimensional array (Flatten).

Solution

We will use Standard PHP Library (SPL) to tackle the problem. Assume we have an array like below.

$origin = [
    'Level 1',
    '_2_' => [
        'Level 2',
        '_3_' => [
            'Level 3',
            '_4_' => [
                'Level 4',
                '_5_' => [
                    'Level 5'
                 ]
            ]
        ]
   ],
   'Another Level 1',
   '_2_1' => [
       'Another Level 2'
   ]
];

Turn it into one dimensional array by using SPL RecursiveIteratorIterator and RecursiveArrayIterator class.

Share on Facebook Twitter

How to Remove Multiple Redis Cache

Tags: January 25, 2017 9:07 PM
0 comments

Goals

Remove multiple Redis cache key using one liner command.

Steps

Assume keys that we want to delete are keys beginning with 'laravel:' prefix.

$ redis-cli KEYS 'laravel:*'
1) "laravel:featured:2bf46b40ed15"
2) "laravel:list:5509f57fdaef"
3) "laravel:list:2bf46b40ed15"
4) "laravel:list:e1bf5bfc2ab8"
5) "laravel:list-total-rec:adc81409d693"
6) "laravel:list-total-rec:e1bf5bfc2ab8"
7) "laravel:promotion-list:adc81409d693"
8) "laravel:list:d1f68333e3bb"
9) "laravel:list:c6a45c5cf5c5"

Just pipe the output above to xargs command. By default redis-cli will use raw format when STDOUT is not tty, which in case is true for xargs.

$ redis-cli KEYS 'laravel:*' | xargs redis-cli DEL
(integer) 9

Reference

Share on Facebook Twitter

Redis: How to Increase File Descriptor Limits

Tags: November 25, 2016 8:06 PM
0 comments

Problem

When you run redis server it complains can not set maximum open files because it has reached the OS max file descriptor limits. Here is the sample output.

$ ./bin/redis-server
28436:C 25 Nov 20:10:03.978 # Warning: no config file specified, using the default config. In order to specify a config file use ./bin/redis-server /path/to/redis.conf
28436:M 25 Nov 20:10:03.979 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
28436:M 25 Nov 20:10:03.979 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
28436:M 25 Nov 20:10:03.979 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
[...CUT...]

When you try to increase the maximum file descriptor using ulimit as root by issuing sudo it returns an error.

$ sudo ulimit -n 65000
sudo: ulimit: command not found

Wow, WTF is that? ulimit is a shell built so giving sudo an instruction to run a command called ulimit will not work. It will the same as statement below.

Share on Facebook Twitter

How to Fix No Sound After Mute and Unmute on XFCE

Tags: October 7, 2016 7:36 PM
0 comments

Problem

There is no sound after doing mute then unmute on XFCE 4 Ubuntu 14.04.

Solution

Try running amixer command to see the status of the Master sound.

$ amixer get Master
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 65536 [100%] [off]
  Front Right: Playback 65536 [100%] [off]

In above result the status of Front Left and Front Right is [off]. Meaning it is still muted even has been unmuted from the XFCE panel. Try to toggle the switch to make it [on].

Share on Facebook Twitter

How to Extract Specific Directory from Tarball

Tags: September 24, 2016 9:30 PM
0 comments

Problem

We have huge file of gzipped tarball and we want to extract only specific directory from the tarball.

Solution

Make sure the pattern we want to extract by searching it first. As an example we want to extract directory named johndoe-website, but we did not know the full pattern of the directory.

$ tar tvf the-archive.tar.gz | grep johndoe-website
home/sites/clients/johndoe-website/javascripts/main.js
home/sites/clients/johndoe-website/styles/main.css
home/sites/clients/johndoe-website/index.html
From the output above we knew that the pattern of the directory is home/sites/clients/johndoe-website. Command below will extract johndoe-website from the archive and strip the 3 leading directories.
$ tar xvf the-archive.tar.gz --strip-components=3 -C /destination/path home/sites/clients/johndoe-website
Command above works in GNU Tar and BSD Tar (Mac OS X).

Share on Facebook Twitter