Category Archives: Tech

tech

Sorting a list of CIDR IP addresses

You may have a list of IP addresses which follow CIDR format, i.e. 111.12.23.45/16 and which you wish to sort. Here is an algorithm to do so in Microsoft Excel:

Step 1. Paste all IP addresses in to a single column A in your Excel sheet.

Step 2. Remove the CIDR part of the IP address in column A by applying the following formula to the first row in Column B:

=LEFT(A1,FIND("/",A1)-1)

This will turn an IP address, such as 111.12.23.45/16 in to 111.12.23.45.

Step 3. Once you get a valid IP address in Column B, double click on the bottom-right corner of the cell to propagate for formula to the rest of the rows in Column B.

Step 4. Select the entire Column B and then paste again using Paste Special… Values. This will remove all the formulas and retain only the IP values.

Step 5. Apply the following formula in Column C in order to make sure each octet of your IP address has three digits:

=TEXT(LEFT(B1,FIND(".",B1,1)-1),"000") & "." & TEXT(MID(B1,FIND( ".",B1,1)+1,FIND(".",B1,FIND(".",B1,1)+1)-FIND(".",B1,1)-1),"000") & "." & TEXT(MID(B1,FIND(".",B1,FIND(".",B1,1)+1)+1,FIND(".",B1, FIND(".",B1,FIND(".",B1,1)+1)+1)-FIND(".",B1,FIND(".",B1,1)+1)-1), "000") & "." & TEXT(RIGHT(B1,LEN(B1)-FIND(".",B1,FIND(".",B1,FIND( ".",B1,1)+1)+1)),"000")
[*] https://www.extendoffice.com/documents/excel/4946-excel-sort-ip-address.html

This will convert all your IPs (without CIDR notation) in to a set of numbers with the following format: XXX.XXX.XXX.XXX. That is to say, each octet will contain exactly three digits. In cases where an octet originally has only two digits, then a zero will be prepended. In the case where the octet originally has only one digit, then two zeros will be prepended.

For example, our original IP: 111.12.23.45/16 will now look like: 111.012.023.045

Step 5. Apply the same thing you did in Step 4. but to this new column C, so that only the values remain.

You should now have three columns in your Excel sheet. The first column is a list of your original IPs in CIDR format. The second is a list of the same IPs without the CIDR notation. The third is a list of the same IPs where each octet has precisely three digits.

Step 6. The final step is to sort the list by column 3: Select the whole sheet by clicking on the top-right corner of the sheet where the rows and columns meet. Go to Menu Item: ‘Data’… click on the ‘Sort’ icon, and select Column 3.

One caveat for using this algorithm:

This algorithm does not take into account the CIDR values when sorting.

‘Check WordPress Update’ plugin for Nagios — PHP 8 compatibility

If you’re using Nagios to monitor your WordPress installations, you may be using the ‘Check WordPress Update’ plugin by jinjie found here:

https://exchange.nagios.org/directory/Plugins/CMS-and-Blog-Software/Wordpress/Check-WordPress-Update/details

We recently updated our PHP version on some of our servers to version 8, and this broke the old version of the plugin. The new plugin has been updated and is compatible with PHP version 8.

The incompatibility was with the last line in the code:

echo $status . '#' . implode(';', $text);

The issue arises because of the order of the arguments for the implode function. The old signature, used for PHP versions up to 7.4 was:

implode(array $array, string $separator): string

whereas the new signature for PHP versions greater than 7.4 is:

implode(string $separator, array $array): string

If you’re on a machine which hosts multiple PHP versions and want to just use one version of the plugin, you can use this little modification to check the current PHP version:

//Test if we're using php version 8 or greater.  This fix is needed or a fatal error will occur.
if (version_compare(phpversion(), '8', '<')) {
    // php version is below 8
    echo $status . '#' . implode($text, ';');
} else {
    echo $status . '#' . implode(';', $text);
}

Windows Keyboard Shortcuts!

I just came across this Tweet by @Rainmaker1973, which is sharing a video by someone called lklogic on TikTok. I found it very useful. I’ve used most of these, but didn’t know about Control D to set to the Default Font Set or that you could align text via a shortcut with Control L and R.

You may want the text version, so I recreated it here:

  • Control A = Select All
  • Control B = Bold
  • Control C = Copy
  • Control D = Default Font Set
  • Control E = Center
  • Control F = Find
  • Control G = Go to
  • Control H = Replace
  • Control I = Italic
  • Control J = Justify
  • Control K = Hyperlink
  • Control L = Alight Text to Left
  • Control M = Hanging Indent
  • Control N = New Document
  • Control O = Open
  • Control P = Print
  • Control Q = Add Space after Paragraph
  • Control R = Align Text to Right
  • Control S = Save As
  • Control T = Left Indent
  • Control U = Underline
  • Control V = Paste
  • Control W = Close
  • Control X = Cut
  • Control Y = Repeat / Undo
  • Control Z = Undo

PureText Plugin: Paste raw text only!

Hey folks. One of my Windows pet peeves is that it always copies formatting whenever you use Control-C to copy text. I almost never want to paste formatting anywhere. In fact, sometimes the formatting interferes and it wastes time because I have to either remove it, or remember to first paste in TextPad and then copy again from there.

I was thinking of writing a Windows plugin to only paste raw-text, but it looks like someone named Steve Miller has already come up with one. It’s called PureText. You can download it here:

https://stevemiller.net/puretext/

It’s available from the Microsoft Store, or from that link.

Unfortunately, you can’t replace Control-V as the standard Hotkey for PureText, as this conflicts with Windows. You can use Win or Alt.

Be sure to check out the Options!…

Using grep to query the nameservers of a list of domains

Let’s say you have a text file containing a list of domains you want to query using whois. Our goal is to query each domain in the list an extract the nameservers for each domain.

This can be achieved using a bash script with a combination of whois, grep, and tr.

Examine the following code:

#!/bin/bash
input="domainlist.txt"
while IFS= read -r line
do
        WHOISOUTPUT=$(whois $line | grep "Name Server" | tr -d '\n')
        echo "$line," "$WHOISOUTPUT"
        sleep 3
done < "$input"

This script instructs bash to run a whois command on each line of the input file. The variable $line will contain the domain name, grep will pick out the lines containing “Name Server”, and tr -d ‘\n’ will remove the new lines from the preceding text in the pipe so the output will appear on one line.

So your output will look something like:

domain.com,    Name Server: ns1.nameserver.com    Name Server: ns2.nameserver.com
domain2.com,    Name Server: NS1.MYDNS.com    Name Server: ns2.MYDNS.com

We are Grepping “Name Server” because that follows the output format outlined by ICANN https://www.icann.org/resources/pages/approved-with-specs-2013-09-17-en#whois

Please be aware that sometimes whois servers can return non-standard formats.

The ‘Sleep 3’ statement is there just to be polite and not hammer nameservers.

If you pipe the output of this script to a text file and save it, you can open it as a CSV file.