Byte Jar - Software Lessons Learned the Hard Way
A public service catalog of solutions to annoying software development problems, or
a sporadically updated rant chamber hosted by the code grunts of a tiny software company. Thanks for tuning in.
by Bishop Bettini
22-Jul-2008 @ 10:48 EDT
We recently held a shell scripting training session for one of our clients. The students brought in a sample shell script for discussion on managing its complexity. The script contained a lot of SQL under fairly deep nested conditions, making a simple top-to-bottom read impossible. We discussed the usual tactics of compartmentalization, including a separate message catalog and functionalization, but because they could not use external files, the options were limited.
So, I introduced a method to use a shell script as its own data source -- in the tradition of Perl =data sections -- that greatly simplified the logic of their shell scripts.
by Bishop Bettini
22-Jan-2008 @ 09:57 EST
We're on a tight deadline here, working on getting a new client up and running. One need I had while building their data importer was the ability to submit the files via the web (using AJAX, nonetheless, as the 5 files are 10MB+), then initiate a background import. I didn't want to go through the hassle of forking with full process control (including the inevitable step of installing the pcntl library), so I present the poor man's fork():
<?php
$command = '/bin/sleep 10 && /bin/touch /tmp/background.check.txt';
exec("($command) >/dev/null 2>&1 &");
?>
Your pipes are closed in $command, so make sure that you have some way to push output (if appropriate). My Importer uses a subscription model, so all I needed to do was add a database backed observer. Then, I have AJAX poll that observer for real-time feedback. Sweet.
by Bishop Bettini
05-Jan-2008 @ 14:50 EST
I'm now working on my taxes, but I needed to take a screen shot in Windows earlier today, and I forgot how. (That should tell you how frequently I use Windows.) I had to dig through our support library to remember how, so I thought I would share our how to take screenshots documentation.
by Bishop Bettini
04-Dec-2007 @ 21:09 EST
If there's one thing I am, it's prepared, sometimes to the point of neurotic obsessiveness. Maybe it's the Type A personality. But let this be a lesson to the doubters: that preparation has saved us lots of time in the fire when our servers have trouble...
by Bishop Bettini
12-Nov-2007 @ 21:53 EST
We've been swamped the last 8 weeks, as we've all been working hard to put three, year-long projects to bed while also finishing several major new enhancements to our facilities management application, AERES and delivering a few custom software applications.
But that time's not been without lessons learned. We received a trouble ticket from a major client indicating that one of their records had been mysteriously wiped out. We were certain that the data wasn't lost, but we needed some forensics. So, armed with a backup and an audit log, we wrote a quick SQL statement to find holes in tables where rows had once been.
Turns out, being able to find sequence gaps is pretty useful and can be parameterized to work on any of your tables....
by Bishop Bettini
31-Aug-2007 @ 20:06 EDT
Want to log in without a password? While handy for users and required for many scripted tasks, it's very easy to do with OpenSSH. All you need do is to put your public key onto the server you want to access, in the .ssh/authorized_keys file.
You can generate your public key locally, create the authorized_keys file remotely, assign it the right permissions, and fill it with the right values all in one command from the local machine.
Here you go:
([ -f .ssh/id_rsa.pub ]||ssh-keygen -t rsa) && ssh user@host "([ -d .ssh ]||mkdir -m 700 .ssh) && cat>>.ssh/authorized_keys && chmod 600 .ssh/authorized_keys" < .ssh/id_rsa.pub
# NOTES:
# replace user@host with the remote user and host you want
# the first time run, this will require you authenticate as user@host.
# subsequent times will be password free.
# this requires that you use a Bourne-derivative shell and GNU mkdir
One command automates everything; what could be easier?!
If you're paranoid (we are), you should immediately edit authorized_keys to restrict the hosts from which this works and the allowed command. See the from= and command= syntax in the OpenSSH documentation. You might also be interested in the public key authentication specification.
Read more tricks in our UNIX Command Line Tricks journal.
by Bishop Bettini
24-Aug-2007 @ 01:38 EDT
I spend most of my life on the command line, which I liken to being on the front line: there is a constant barrage of incoming fire. Old gun bunnies like me know when to duck and when to dodge, but not everyone's been in the field so long.
The boys over at OpenSSH have decided they'd like to lob not one, not two, but three command line grenades, since a ruined day courtesy of a wayward space isn't enough (think rm -rf * .o):
ssh -p 20022 user@example.com
scp -P 20022 /tmp/whatever user@example.com:/tmp/whatever
sftp -oPort=20022 user@example.com
Did you catch that? That's right, there's three ways to specify the ssh connection port: a lower-case p, an upper-case P, and the option parameter. Lower-case p means something different to scp than it does to ssh, and isn't even a possible flag for sftp. And don't even think about -P for ssh because it's not a possible flag and means something completely different for sftp. Whew!
Before you purists mail me and say, "Uh, they all support -o", I know that. From a high quality team like OpenSSH, I would expect to use the same flag to mean the same thing across multiple programs in a software suite.
But my rant is about why we see -p and-P so frequently in examples when -o is sufficient. If you look at virtually every example found on the net, you'll see -p and -P used to specify alternate ports. Probably because that's the way it's always been done (the backwards compatibility defense pops up again). So the blowfish say "we'll leave -p and -P in there, everybody uses them."
Listen people: reduce the amount of thinking you have to do and start using -o always. Forget that -p and -P exist. Don't tell your friends and definitely don't tell the next generation. Both -p and -P are vestigial and need to evolve away. They're part of an inconsistent UI design that is just waiting to blow up in someone's face.
Here's the right way for you to specify alternate ports:
ssh -oPort=20022 user@example.com
scp -oPort=20022 /tmp/whatever user@example.com:/tmp/whatever
sftp -oPort=20022 user@example.com
Now isn't that nice?
by Bishop Bettini
23-Aug-2007 @ 18:56 EDT
Seven: my lucky number and also the number of years before we started posting technical notes on the Internet. Read all about us or go ahead and jump to the complete archive. Enjoy!