Perl: Count days until payday in a one-liner

Pay day is once a month on the 7th. Can you calculate the number of days until payday using a Perl one-liner in less than a hundred characters? I wasn't able to do it, but some creative Redditors were:

perl -E '$_=-1;for($t=time;$d!=7;$t+=86400){$_++;$d=(localtime($t))[3]}say'
Leave A Reply

Mixing privacy channels and CTCSS/DCS for GMRS radios

Two way radios can utilize privacy channels to limit the amount of chatter on a given channel. Many consumer grade radios use the term "privacy channels" to mean CTCSS or DCS coding. My Baofeng radios allow setting CTCSS/DCS directly so it's important to have a mapping between generic "privacy channels" and the raw codes.

Radios using the "common" settings of 1 through 121 include: Motorola, Uniden, and Backcountry Access.

Common CTCSS Freq
1 67.0
2 71.9
3 74.4
4 77.0
5 79.7
6 82.5
7 85.4
8 88.5
9 91.5
10 94.8
11 97.4
12 100.0
13 103.5
14 107.2
15 110.9
16 114.8
17 118.8
18 123.0
19 127.3
20 131.8
21 136.5
22 141.3
23 146.2
24 151.4
25 156.7
26 162.2
27 167.9
28 173.8
29 179.9
30 186.2
31 192.8
32 203.5
33 210.7
34 218.1
35 225.7
36 233.6
37 241.8
38 250.3
Common DCS Code Baofeng
39 023 D023N
40 025 D025N
41 026 D026N
42 031 D031N
43 032 D032N
44 043 D043N
45 047 D047N
46 051 D051N
47 054 D054N
48 065 D065N
49 071 D071N
50 072 D072N
51 073 D073N
52 074 D074N
53 114 D114N
54 115 D115N
55 116 D116N
56 125 D125N
57 131 D131N
58 132 D132N
59 134 D134N
60 143 D143N
61 152 D152N
62 155 D155N
63 156 D156N
64 162 D162N
65 165 D165N
66 172 D172N
67 174 D174N
68 205 D205N
69 223 D223N
70 226 D226N
71 243 D243N
72 244 D244N
73 245 D245N
74 251 D251N
75 261 D261N
76 263 D263N
77 265 D265N
78 271 D271N
79 306 D306N
80 311 D311N
81 315 D315N
82 331 D331N
83 343 D343N
84 346 D346N
85 351 D351N
86 364 D364N
87 365 D365N
88 371 D371N
89 411 D411N
90 412 D412N
91 413 D412N
92 423 D423N
93 431 D431N
94 432 D432N
95 445 D445N
96 464 D464N
97 465 D465N
98 466 D466N
99 503 D503N
100 506 D506N
101 516 D516N
102 532 D532N
103 546 D546N
104 565 D565N
105 606 D606N
106 612 D612N
107 624 D624N
108 627 D627N
109 631 D631N
110 632 D632N
111 654 D654N
112 662 D662N
113 664 D664N
114 703 D703N
115 712 D712N
116 723 D723N
117 731 D731N
118 732 D732N
119 734 D734N
120 743 D743N
121 754 D754N

Table borrowed and simplified from k0tfu.org

Leave A Reply

Perl: Generate random UUID

I needed simple and portable way to generate a random v4 UUID in Perl, so I ripped out various pieces of UUID::Tiny and came up with this.

sub gen_uuid {
    my $uuid = '';
    for ( 1 .. 4 ) {
        $uuid .= pack 'I', int(rand(2 ** 32));
    }

    substr $uuid, 6, 1, chr( ord( substr( $uuid, 6, 1 ) ) & 0x0f | 0x40 );

    return join '-',
        map { unpack 'H*', $_ }
        map { substr $uuid, 0, $_, '' }
        ( 4, 2, 2, 2, 6 );
}
Leave A Reply

Getting to know your family questions

  1. What kind of student were you in HS? Jock? Nerd? Straight A student?
  2. What was the most trouble you got in to as a kid?
  3. What was the most trouble your kids got in to?
  4. What is your favorite memory of your parents from when you were a kid? Traditions? Favorite meals?
  5. What was your favorite family vacation?
  6. What was your first job after HS?
  7. Did you want to have a big family yourself?
  8. Who is your parents favorite child?
  9. Where did you meet your significant other?
  10. What was your college experience like?
  11. What was the worst thing you ever did to your siblings as a kid? Or they did to you?
  12. Who has an interesting story I should talk to?
Leave A Reply

The unborn are a convenient group of people to advocate for

"The unborn are a convenient group of people to advocate for. They never make demands of you; they are morally uncomplicated, unlike the incarcerated, addicted, or the chronically poor; they don’t resent your condescension or complain that you are not politically correct; unlike widows, they don’t ask you to question patriarchy; unlike orphans, they don’t need money, education, or childcare; unlike aliens, they don’t bring all that racial, cultural, and religious baggage that you dislike; they allow you to feel good about yourself without any work at creating or maintaining relationships; and when they are born, you can forget about them, because they cease to be unborn. You can love the unborn and advocate for them without substantially challenging your own wealth, power, or privilege, without re-imagining social structures, apologizing, or making reparations to anyone. They are, in short, the perfect people to love if you want to claim you love Jesus, but actually dislike people who breathe. Prisoners? Immigrants? The sick? The poor? Widows? Orphans? All the groups that are specifically mentioned in the Bible? They all get thrown under the bus for the unborn."

Methodist Pastor David Barnhart

Leave A Reply - 1 Reply

PHP: Using __halt_compiler()

PHP has a mechanism that allows you stop the compiler on a given line. This allows you to put non-PHP code after that line without affecting your code. You can put data, JSON, HTML, etc after the __halt_compiler() line and then read it with your script. Here is a simple function that reads the text after your __halt_compiler()

// Borrowed from https://gist.github.com/ziadoz/5286233
function get_halt_str() {
    return file_get_contents(__FILE__, false, null, __COMPILER_HALT_OFFSET__ + 1);
}

See also: Perl version

Leave A Reply

Perl: The __DATA__ construct

Perl has a unique feature where if it sees a line that contains __DATA__ the parser will stop there as if the file ended. This allows you put non-perl code after your __DATA__ line: text, json, HTML, etc. Perl will even allow you to read the text after the __DATA__ like it's a normal file handle. This function will read all the text after your __DATA__ block.

sub get_data_str {
    local $/ = undef; # Slurp mode
    return readline(DATA);
}

Note: Perl also recognizes __END__ but that text is not readable.

See also: PHP version

Leave A Reply

Vim encryption

Vim has simple built in encryption. If you have a text file you want to encrypt you can run the commands:

:set cryptmethod=blowfish2
:X

Vim will prompt you for a password, and then the next time you save the file it will be encrypted automatically. When you open the file it will prompt you for the password. If you enter the wrong password you'll see a bunch of gibberish.

Leave A Reply

Perl: hash to ini

I'm a big fan of .ini files because they're human readable, and also very machine readable. I wrote a quick function to convert a hashref to a simple .ini string.

my $str = hash_to_ini({ data => { 'name' => 'scott', animal => 'dog' }});
sub hash_to_ini {
    my $x   = $_[0];
    my $ret = '';

    foreach my $key (sort(keys(%$x))) {
        my $val = $x->{$key};

        if (ref($val) eq "HASH") {
            $ret .= "[$key]\n";
            foreach my $k (sort(keys(%$val))) { $ret .= "$k = " . $val->{$k} . "\n"; }
        } else { $ret .= "$key = $val\n" }
    }

    $ret =~ s/\n\[/\n\n[/; # Add a space between sections
    return $ret;
}

I also wrote a version in PHP

function hash_to_ini($x) {
    $ret = '';

    foreach (array_keys($x) as $key) {
        $val = $x[$key];

        if (is_array($val)) {
            $ret .= "[$key]\n";
            foreach (array_keys($val) as $k) { $ret .= "$k = " . $val[$k] . "\n"; }
        } else { $ret .= "$key = $val\n"; }
    }

    $ret = preg_replace("/\n\[/", "\n\n[", $ret);
    return $ret;
}

Note: see also parse_ini()

Leave A Reply

Perl: Method to send a simple SMTP message

This is a quick method to send an SMTP email message using on core Perl modules.

use Net::SMTP;
use Time::Piece;

my $smtp_server = "mail.server.com";
my $smtp        = Net::SMTP->new($smtp_server, Timeout => 3, Hello => 'hostname.server.com');

my $err;
my $ok = send_email('to@domain.com', 'from@domain2.com', 'Test subject', '<b>HTML</b> body', \$err);

if (!$ok) { print "Error: $err" }
sub send_email {
    my ($to, $from, $subject, $html_body, $err) = @_;

    $smtp->mail($from);
    my $ok = $smtp->to($to);

    if ($ok) {
        # Ghetto strip tags
        my $text_body = $html_body =~ s/<[^>]*>//rgs;
        my $sep       = time() . "-$smtp_server";

        my $headers  = "To: $to\n";
        $headers    .= "From: $from\n";
        $headers    .= "Subject: $subject\n";
        $headers    .= "Date: " . localtime->strftime() . "\n";
        $headers    .= "Message-ID: <" . time() . "\@$smtp_server>\n";
        $headers    .= "Content-type: multipart/alternative; boundary=\"$sep\"\n\n";
        $headers    .= "This is a multi-part message in MIME format\n\n";

        # Text version
        $headers .= "--$sep\n";
        $headers .= "Content-Type: text/plain\n\n";
        $headers .= "$text_body\n\n";

        # HTML version
        $headers .= "--$sep\n";
        $headers .= "Content-Type: text/html\n\n";
        $headers .= "$html_body\n\n";

        # Closing separator
        $headers .= "--$sep--\n";

        $smtp->data();
        $smtp->datasend($headers);
        $smtp->dataend();
    } else {
        $$err = $smtp->message();
    }

    return $ok;
}
Leave A Reply

Bash: Quick backup

Often I will need to take a quick backup of a directory or file before I make some changes. Sometimes I will need multiple backups, so I include the date/time in the output filename. After doing it manually enough I wrote a quick bash function that will handle creating the backup file, and naming it appropriately. Put these in your ~/.bashrc or copy and paste to your terminal

# Quick tar/file commands
qtar() { C=$(basename $1); O=/var/tmp/quicktar-$C-$(date +%F_%H-%M).tar.gz; tar -cvpaf $O $*; echo; ls -sh $O; }
qfb()  { C=$(basename $1); O=/var/tmp/quickfile-$C-$(date +%F_%H-%M).gz; gzip $1 -c > $O; ls -sh $O; }

Then to run it simply invoke it like this:

# Multiple files or directories
qtar file1.txt file2.txt
qtar /path/to/dir
qtar files/

# One file
qfb /etc/dhcp/dhcpd.conf
qfb /etc/php.ini

This will make an appropriately named file in /var/tmp/ with the files/dirs you listed tarred and gzipped up. It's not perfect, there are some corner cases that will break it, but I needed something super simple to drop on remote servers for quicky backups.

Update: Less common, but I also whipped up a zip option.

qzip() { C=$(basename $1); O=/var/tmp/quickzip-$C-$(date +%F_%H-%M).zip; zip -9vr $O $*; ls -sh $O; }

Update: I learned that Vim has built in diff support, even inside of .gz files. This means you can do vim -d /etc/orig.txt /var/tmp/quickfile-orig.txt-2022-08-03_08-13.gz and it will show you the diff.

Leave A Reply

Subway: Sub of the Day

Sub of the day for my local Subway.

Day Sandwich
Monday Sweet Onion Teriyaki
Tuesday Spicy Italian
Wednesday Oven Roasted Turkey
Thursday Italian BMT
Friday Tuna
Saturday Black Forest Ham
Sunday Meatball
Leave A Reply

Characters and places in Heretics of Dune

Notes I took while reading Heretics of Dune. Information augmented with data from Dune Wiki.

Item Description
Hedley Tuek High Priest, titular ruler of Rakis
Miles Teg Mentat
Taraza Mother Superior
Sheena Young girl, can command worms, family dies
Darwi Odrade Bene Gessirit, tries to seduce Teg
Gammu Name by which the planet Giedi Prime went after the fall of House Harkonnen
Rakis Renamed Arrakis
Bashar Military rank of the Sardukar
Leave A Reply

Linux: Regenerate SSH host keys

Fedora and CentOS automatically regenerate SSH host keys on bootup if the key files are missing. This makes it easy to trigger regeneration as you simply remove the keys, and reboot the server. Other distributions are not quite as forgiving and require manual intervention. These are the steps I've used on Debian to get updated host keys.

rm /etc/ssh/ssh_host_*
ssh-keygen -f /etc/ssh/ssh_host_rsa_key     -N '' -q -t rsa
ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key   -N '' -q -t ecdsa
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -q -t ed25519

ls /etc/ssh/*key* -lsh

systemctl restart sshd
Leave A Reply - 1 Reply

Updated my Javascript Unixtime tool

I updated and modernized my Javascript Unixtime tool. If you have to work with Unixtimes it's a really handy tool to process them in a more human friendly fashion.

Leave A Reply