Using Bro to load balance

If your hardware doesn’t support HLB, or if you for whatever reason don’t want to use that load balancing, I’ve had good success with a Bro configuration that Seth Hall wrote for me. As background, my NIC is (currently) sending a full copy of the input stream to each of a dozen output streams. I’m going to be implementing HLB on my NIC, so I wanted to keep Seth’s hard work around somewhere that it might also do somebody else some good.

This configuration allows for six workers. If you want a differing amount, change the total_lb_procs and the integers at the end of each restrict_filters statement appropriately.

event bro_init() &priority=-12
 {
local total_lb_procs = 6;
if ( Cluster::node == "worker-1" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 0) );
if ( Cluster::node == "worker-2" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 1) );
if ( Cluster::node == "worker-3" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 2) );
if ( Cluster::node == "worker-4" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 3) );
if ( Cluster::node == "worker-5" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 4) );
if ( Cluster::node == "worker-6" )
    restrict_filters = table(["lb_filter"] = fmt("(ip[14:2]+ip[18:2]) - (%d*((ip[14:2]+ip[18:2])/%d)) == %d", total_lb_procs, total_lb_procs, 5) );
PacketFilter::install();
}

Update 16 September – Seth tells me that this is a terrible way to balance in Bro – he had some problems with this at another high-volume institution. Well, it worked for me. :)

Automated rsync backups with ssh key restrictions

For the first time ever I wanted to make an rsync script to back up a couple of remote servers, restricting the commands by the use of a key. I wanted to restrict the commands that could be run with that key in case of compromise, since there needs to be no passphrase on the key. I’m not going to explain the theory or most of the commands, since you (I) already know.

Doing some googling, I found this which was pretty close, but I wanted it here (so I could find it again) and with fewer words. I ripped off the validatersync.sh script wholesale:

#!/bin/sh
case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
*\|*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esac

There’s probably some holes in it, but it’s close enough for government work. Then, add to authorized_keys:

from="hostname",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,command="/path/tovalidatersync.sh" thebackupkey

And a sample backup script:

#!/bin/bash
LOGF=/path/to/LogFile
MYD=`date`
echo "Starting rsync at ${MYD}" >> ${LOGF}
/usr/bin/rsync -q -a --delete -e "ssh -i /the/.ssh/backup_key" userid@remote:/home/asdf/ asdf/
MYD=`date`
echo "Finished at ${MYD}" >> ${LOGF}

Call that in cron and you (I) should be good to go.

ETA: you might get “protocol mismatch” errors from rsync. TFM will tell you it’s because there’s output from your shell. TFM might be wrong. I’m still getting this error from one host I’m doing this with, but not the other. Since both are FreeBSD 8.4 machines, I’m somewhat mystified. Anyway, this might be enough to get started.

Hello, goodbye

You liked F1 racing and made me look kind of reactionary. You hated Zen and people misusing or misspelling Latin phrases. It was fun to tease you by writing koan-style poetry ending in “per say.”

It was good knowing you, Andy.

Double click for new terminal

I’m completely dumb at the Apple-way-of-scripting. I wanted something I could throw on my desktop that I could double click and it would open a terminal window and ssh somewhere. I’m not sure how useful this will actually *be* to me, but given it took me non-zero time with google to figure it out… thanks to various stackoverflow posts.

Open Applescript Editor. Put in:

tell application Terminal
do script "ssh hostname"
set bounds of front window to {63, 640, 1212, 1022}
end tell

File | Export … and save it as an application. Put it on your desktop, giving it a reasonable name. Done. If you want to later edit the script (say, to set boundaries :) ), right click, Show Package Contents, then go into Contents\Resources\Scripts and edit the main.scpt file you’ll find. The “set bounds” statement places the window at the bottom-left-ish and makes it 160×25 at my current resolution and font size. I can’t figure out how to tell Terminal to just set itself to 160×25 without also moving it, and I expect that the actual characters displayed depends on font, size, etc.