Companionway

Meeting challenges with success

Sometimes It Is The Obvious

This was one of those “problems” that nagged me for many hours until the obvious dawned on me. I wrote a python script to grab the temperature and humidity from a DHT22 sensor and then write the output with the proper syntax to send to my monitoring program [xymon]. The wrapper bash script ran every 5 minutes and used a redirection to write out a file ie:

*/5 * * * * /usr/local/bin/temphum.sh >/tmp/temphum.dat

The wrapper script grabs output and sends it to the server. Having this wrapper script lets me run the python script independently for testing or for curiosity.


Always Use Visudo

“familiarity breeds contempt”

I get very comfortable with using the vim editor (take cover… flame way ensuing) so comfortable that using any other editor leaves me just a bit unnerved. I constantly use it to update sudoers. The best way to modify sudoers is to add a supplimental inclusion file under /etc/sudoers.d/whatevername - so the other day I needed to add an entry to allow the user for my simple monitoring program (xymon) to be able to run “sudo ufw status” without a password and report back to the monitor server.

I fired up

sudo vim /etc/sudoers.d/xymon

and started to add the needed line:

xymon ALL = NOPASSWD: /usr/sbin/ufw status

but I got halfway through and could not remember the exact location


Recursively Replace Text

Recently I had to do a find and replace text on multiple files recursively. I had to look up how to do it to remind myself of the exact syntax. If I had to look it up, you have to suffer seeing it in my blog post… [smile]

find ./ -name \*.md -print0 | xargs -0 sed -i 's/header-img/img/'

Be careful here. I used the -0 option to deal with any filename that have spaces in the name BUT it really requires that the find command contain the -print0 option otherwise things will not operate in the manner expected.


Switched to Hugo

I am swtiching from jekyll to hugo for flat file for a Content Managment System (CMS).

alt text
goHugo.io

Here is why:

  • It comes as a binary file that runs on pretty much any platform. A single file… without a list of dependencies
  • I could stop with the last reason and it would be enough… but I have to say that the file tree used in hugo is simple, and simple is beautiful. As Einstein once said:

“Make things as simple as possible, but not simpler.”

  • It is written in googles go language… so it is fast and portable.
  • It is closely tied to git so it can easily be used on github and for version maintence.
  • it builds a clean directory [./public/] for the full site deploy and this can be rsync’d to where ever you like. or … just git push it to hithub and use something like netlify.com to host from your github!
  • No frustrating version incompatiblities (or at least very minimal) - unlike jekyll which seemed to break on me every few months.
  • hugo is relatively mature and has a huge developer backing.
  • configurable and changed without challenges
  • lots of themes to choose from
  • as the Hugo websites states - it makes the web fun again…
  • Did I mention that it is a single executable file?

Reference: gohugo.io

Enjoy -g-


Converting Images

I have fought with trying to remember the syntax for Imagemagik’s convert command for years. I finaly caved in [from my normal position of “if you can’t do it from the commandline, then you are a wimp” and sought out a gui to help me quickly convert images to use on a website. Or to just roll a picture 90 or 180 degrees. The easy solution and a standard ubuntu repository file is “converseen”. It can do batch conversions or on selected files. Gets the job done without the challenge of looking up all the arguments for “convert”.


shell/bash uppercase arguments

turning your commandline arguments into uppercase

To make a long story short…

#!/bin/bash
PATTERN=$(echo $1 | tr [:lower:] [:upper:])
# or
PATTERN=$(echo $1 | tr [a-z] [A-Z])
# or
PATTERN=$(echo $1 | awk '{print toupper($0)}')

echo "Your \$1 argument is now uppercase: $1"

To make a short story long…

Just to let you know a little more about me and reveal a few more shells tricks along the way.

I am a very conservative investor - always have been but the low interest rates over the recent past has forced me to try a squeeze a little more out of the stock market also known as a “a gentleman’s gambling parlor of greedy people”. To do this I have been using covered secure puts and covered calls (did I mention that I am conservative?). So what does this have to do with the title here?


The GrovePi kit

Make the raspberry pi GPIO pins quick and easy to use

Check out GrovePi

These kits make working with the Raspberry Pi GPIO pins so simple and clean. I just ordered one of these kits and I will try to write up a review after I work it over.

If you are interested in building a quick and easy raspberry pi for experimenting with the “Internet of Things” (aka IoT) platform - this board and kit looks like it will get you there in minutes. Approx: $89 - includes board, and lots of sensors/accessories with wires and connectors.


How to print the remainder of a line using awk

... eliminate the first X columns ...

#How to print remainder of line using awk

eliminate the first X columns…

Here is your challenge you have a list of cronjobs and you want to just execute the scheduled backup command with its arguments for a test run.

Using one command line, how would you do it?

Here is a sample root cronjob and I want to run the first entry with its arguments as a test run:

# m h  dom mon dow   command
 0 16  *   *   * /data/share/home/geoffm/dev/utils/bak-it.sh -i /usr/local/etc/include.lst -e /usr/local/etc/exclude.lst -t /data/bak/`hostname` -s 7 >/dev/null 2>&1
  5 0  *   *   * /data/share/motion/arch-files.sh >/dev/null 2>&1
*/15 *  *   *   * /data/share/home/geoffm/grab-pic.sh >/dev/null 2>&1
####### below are WIP or OLD ########
# 16 6  *   *   * /usr/sbin/lynis -c --auditor "automated" --cronjob --quiet >/dev/null 2>&1
#*/5 *  *   *   * /usr/local/bin/watch-proc.sh -f /usr/local/etc/watch-proc.conf -n geoffm@localhost >/dev/null 2>&1
#*/5 * * * * /data/share/home/geoffm/ping-pong.sh "ping -c2 192.168.1.55" "ssh 192.168.1.1 reboot" >/dev/null
#  7 * * * /usr/local/sbin/postfix_report.sh &> /dev/null

Creating that special character in vim

... digraphs to the rescue

Entering special characters into vim

I use vim as my editor. Despite this first sentence, this is not a beginning volley to an editor flame war. I will save that for another day.

Ever editor has its own unique features and vim definitely no stranger to that group. One feature I use quite a bit is what vim calls digraphs.

There are thousands of characters other than the standard keyboard character that can be displayed with vim. You may already know that you can use CTRL-v <enter> to force a linefeed into even a DOS filetype. Actually `CTRL-v ascii### will force special characters from a long list of choices.

But I prefer using CTRL-K followed by two semi-meaningful (slightly resembles the intended special character) to create any one of literally thousands of special characters.


git inside, git better, git stronger

... exploring git inside and out

… exploring git from the inside out …

###Preface

Apologies up front - this is long... very long, but there is food for the brain here. I will discuss git for personal use. Using it with many other contributors is an extension of the principle covered here. For simplicity, I am only going to discuss you working alone and only using a single "branch" of code for each project - again, for simplicity. Additionally we are going to examine the git "database" (stored as compressed SHA-1 named files/objects) as we go. We can gain a better understanding by reviewing the changes to the guts of your personal git repository. We will not cover branches, tags, or the git daemon in this discussion.

###Why git? (skip this if you are less than patient)

Many years ago I used backups of my projects or just documents, notes, etc. All worked well until it didn’t. If you make a mistake and back it up guess what,