Posts
Blog Post Archive With Hugo
I took this code from The Drone Ely’s blog.
First, I created a file layouts/_default/archive.html with the following contents:
{{ define "main" }} <section> {{ $type := "posts" }} {{ $.Scratch.Set "count" 1 }} {{ range (.Site.RegularPages.GroupByDate "2006") }} {{ if and (gt .Key 1) (gt (where .Pages "Type" $type) 0) }} {{ range (where .Pages "Type" $type) }} {{ if (eq ($.Scratch.Get "count") 1) }} {{ $.Scratch.Set "count" 0 }} <h1> {{ .
Posts
Certbot Without Port 80
Is a web server still a web server if it has the HTTP port 80 blocked? A security team somewhere probably thinks so and has the web server reachable only on HTTPS port 443 for security. To install the LetsEncrypt certificates on the web server, the validation involves copying a certain file to a path within the public_html directory, followed by a validation by attempting to read that file on HTTP port 80.
Posts
Generating Certificates for Multiple Hosts
When generating a bunch of certificates with a self-signed CA, a loop is the way to go:
#Config for openssl sudo mkdir -p /etc/pki/CA sudo touch /etc/pki/CA/index.txt echo "1000" | sudo tee /etc/pki/CA/serial sudo sed -i 's/# copy_extensions = copy/copy_extensions = copy/' /etc/pki/tls/openssl.cnf #Generate a CA certificate, loop over the CSR and signing subjprefix='/C=AE/ST=Dubai/L=Bur Dubai/O=Pocket Theories/OU=Web' mydomain='pockettheories.com' openssl req -newkey rsa:2048 -new -x509 -sha256 -extensions v3_ca -out ca.cert -keyout ca.key -subj "${subjprefix}/CN=certificateauthority" -nodes -days 3650 for iterhost in web0 web1 web2 db0 db1 db2 nlb0 nlb1 nlb2 cdn do openssl req -sha256 -nodes -newkey rsa:2048 -keyout ${iterhost}.
Posts
Automating Scp Password Without Sshpass
On RedHat Enterprise Linux, sshpass isn’t available in the standard package repositories (it needs the EPEL repos) so, when working on air-gapped servers, an alternative is to build a script around “expect”. Here’s a re-usable script:
#Install "expect" which is a dependency #sudo yum install -y expect #!/usr/bin/expect -f # Usage: scp.exp <hostname> <username> <password> <local_source_file> <destination_file> set server [lrange $argv 0 0] set name [lrange $argv 1 1] set pass [lrange $argv 2 2] set src [lrange $argv 3 3] set dest [lrange $argv 4 4] spawn scp -oStrictHostKeyChecking=no -oCheckHostIP=no $src $name@$server:$dest match_max 100000 expect "*?
Posts
Setting Up Jekyll on Windows
Although Jekyll is not officially supported on Windows, it still can be run. Here is how I set it up for this blog.
Start by uninstalling any old versions of Ruby that are installed, and install a recent stable version Eg. Ruby 2.7.5 with DevKit.
The RubyInstaller.org website lists Ruby version 3 for download, but pick Ruby 2.7 because it is more stable, having been tested by more users.
Posts
Sparse Files in Linux
Sparse files in Linux provide the ability to avoid allocating disk space for the null bytes within a file. A common example of sparse files is the /var/log/lastlog file, which keeps track of the last login for a user, while keeping the file organized by UIDs.
To create a spare file of our own, let’s first create a file with zeroes:
dd bs=512 count=20 if=/dev/zero of=/tmp/myfile1.dat ls -lh /tmp/myfile1.dat ls -s /tmp/myfile1.
Posts
Shell Sort in Python
Shell sort, by Donald Shell, works by taking a gap that is n/2 elements apart (rounded down), compares each element with an element that is away by a distance of the gap, and then takes half the gap on the next iteration. When the gap is zero, the elements are sorted.
The following is an implementation of Shell sort in Python.
def shell_sort(a_list): gap = int(len(a_list)/2) while gap > 0: for i in range(len(a_list)-gap): # 01234 5/2 0-2,1-3,2-4 # 0123 4/2 0-2,1-3 if i+gap < len(a_list) and a_list[i] > a_list[i+gap]: a_list[i], a_list[i+gap] = a_list[i+gap], a_list[i] gap = int(gap/2) my_list = [6, 3, 5, 4, 1, 1, 2] shell_sort(my_list) print(my_list)
Posts
Insertion Sort in Python
In an insertion sort, when processing elements to be sorted, the elements are inserted among the previously processed elements in the desired position. This is repeated from the second element to the last element.
The following is an implementation of the insertion sort algorithm in Python:
def insert_sort(a_list): for i in range(1, len(a_list)): val_idx = -1 for j in range(i): if a_list[j] < a_list[i]: val_idx = j if val_idx is not None: tmp_val = a_list[i] a_list.
Posts
Selection Sort in Python
A selection sort involves identifying the smallest element to be sorted and swapping it with the first position in the collection, then identifying the next smallest element and swapping it with the next position in the collection, and repeating till all the elements have been sorted.
The following is an in-place implementation of selection sort in Python:
def select_sort(a_list): for i in range(len(a_list) - 1): val_idx = None for j in range(i, len(a_list) - 1): if a_list[j] > a_list[j + 1]: val_idx = j+1 if val_idx is not None: a_list[i], a_list[val_idx] = a_list[val_idx], a_list[i] my_list = [6, 3, 5, 4, 1, 1, 2] select_sort(my_list) print(my_list)
Posts
Bubble Sort in Python
A bubble sort, as the name implies, involves using a bubble of 2 elements within which a swap occurs if the elements are not in the correct order. Each iteration of the bubble sort performs n-1 comparisons. By repeating this bubbling for n-1 iterations, we get the input data sorted.
The bubble sort algorithm is simple enough to implement an in-place sort in just 4 lines of Python:
def bubble_sort(input_list): # TODO Implement a flag to check if there were any swaps in the first iteration; # if there were none (i.