The infamous advertising
to   (Translation provided by Digital Altavista)

Mike's Linux Scripts

I've always loved programming. Since moving to Linux however, I've found that writing scripts and the like is that much easier and more productive. Please consider some of my offerings below. All are licensed under the GPL version 2 or later.

Scripts I've done:

Let's start here ... these scripts are mine or my modifications to others'. They are under the GPL ... I don't mind if you use and modify them (please send me improved versions), just don't take my name out of them unless you make something extremely big out of it and my effort seems meaningless (in which case you must still notify me) :).

BIND root server updating

Everyone has a version of this one practically. If you know of a vastly superior one, tell me so I can use it instead. Basically it asks for an updated copy of the root servers list from rs.internic.net. You may have to change the file names or directories to reflect your configuration.

Copy the above to a file and then save it in your /etc/cron.monthly directory (or run it directly from crontab).

Average file size

I wanted a quick way to see what the average sized file was in a given directory so I wrote the following BASH script: (run it with one argument; the directory to check)

#!/bin/sh
echo -n "Determining average file size for "
echo $1
echo -n "Counting files ... "
COUNT=`ls -laR "$1" | awk '{print $5}' | wc -l`
echo -n $COUNT
echo " files"
echo -n "Sizing files ... "
SIZE=`(echo 0; \
       ls -laR "$1" | \
       awk '{printf("%d + \n", $5)}'; \
       echo $COUNT; \
       echo "/ p") | dc`
echo -n $SIZE
echo " bytes per file."
  

Temporary address blocking

Programs like portsentry (see Freshmeat listing) offer the option of blocking an address that has probed you for listening sockets, etc. via firewall blocks or route dropping. It is often desired that these actions not be permanent, especially in the case of a potential DoS attack or if the scanner was on a dynamic IP (which may end up being a legitimate user in a few minutes). For these cases, I created the following script (also available for download):

Note: Code released under LGPL version 2 or later.

#!/bin/sh

if [ "x$1" = "x" ]; then exit 0; fi
if [ "x$2" = "x" ]; then exit 0; fi

TARGET=$1	# IP address or name
TIMEOUT=$2	# In seconds

/sbin/ipchains -A input -s $TARGET -j DENY -l && \
sleep $TIMEOUT && \
/sbin/ipchains -D input -s $TARGET -j DENY -l

If you want to use this with portsentry, simply copy the script to /usr/sbin/tempblock.sh, comment out all KILL_ROUTE lines in your portsentry.conf and add the following in their place (to block the address for 30 minutes):

KILL_ROUTE=/usr/sbin/tempblock.sh $TARGET$ 1800

HylaFAX init.d entry

HylaFAX does a great job of sending both faxes and pages over a network, but the startup and shutdown via init.d left a bit to be desired for my RedHat blood. Here's my version:

Copy this into a file called /etc/rc.d/init.d/hylafax

#! /bin/sh
#
# fax server control

. /etc/rc.d/init.d/functions

FAXQ=/usr/sbin/faxq
HFAXD=/usr/sbin/hfaxd
FAXQUIT=/usr/sbin/faxquit
FAXPORT=hylafax			# designated port for new protocol
SNPPPORT=snpp			# official port for SNPP

FAXSTAT=/usr/bin/faxstat
FAXSTATOPTS="-i -r -s -l"	# additional info, receive and send queues, localtime

case $1 in
'start')
	echo -n "Starting HylaFAX queue handler (faxq)"
	daemon $FAXQ
	echo
	echo -n "Starting HylaFAX daemon (hfaxd)"
	daemon $HFAXD -i $FAXPORT -s $SNPPPORT
	echo
	;;
'stop')
	$FAXQUIT >/dev/null 2>&1
	echo -n "Stopping HylaFAX daemon (hfaxd)"
	killproc hfaxd
	echo
	;;
restart)
	$0 stop
	action "Waiting 5 seconds" sleep 5
	$0 start
	;;
status)
	$FAXSTAT $FAXSTATOPTS
	;;
*)
	echo "usage: /etc/init.d/hylafax {start|stop|restart|status}"
	;;
esac

EtermExec

I run certain programs in transparent Eterm windows and sometimes do so remotely. I like to know where I'm running an app from and so I wrote this quick script:


#!/bin/sh

if [ "$2" = "" ]; then
   TERMNAME="Terminal @ `hostname`"
else
   TERMNAME="$2 @ `hostname`"
fi

if [ "$1" = "" ]; then
   exit 0
fi

Eterm --scrollbar=0 --menubar=0 --watch-desktop=1 --title="$TERMNAME" --trans=1 --shade=0 --exec $1 & etermexec top \

Save this as, for example, /usr/bin/etermexec and use it to run programs like pine or tin. It accepts two arguments, the name of the application to run in the window, and the name to put in the titlebar. The name of the machine its executed from will be automatically appended. For example: (the last example is depicted to the right)

Note: To produce borderless windows on E, I simply have it "remember" that the window with a given title is borderless and then specify that title.

Colourising your system logs

This is a quick script I did after realising that other such tools didn't do what I wanted. The first time I saw colour-coded HTML or C in a good IDE, I never wanted to go back. I hope you have a similar experience after running something like:
cat /var/log/messages | /usr/local/bin/logcolorize.pl


I have my log constantly scrolling on my desktop which looks a lot like this: Logcolourise at work
(Click for JPG version)

Note: it seems that Redhat is including logcoloriser in their Power Tools (see here). Also, I now use my etermexec script, above, to produce the scrolling output.


It is currently at version 1.0.8. If you want to follow it, watch for announcements on Freshmeat. If you find it's useful or wonderful after using it, please add a comment to it's Freshmeat listing.

  • Download it!

    Parsing log from APCUPSD

    This script is for parsing APCUSPD's log output. It takes its input from STDIN. I run it daily in the postrotate section of logrotate.conf for /var/log/apcups.log (example below).

    zcat /var/log/apcupsd.log.1.gz | /bin/perl /usr/local/bin/apcloganalyser.pl > /home/httpd/html/apcups.html

    NB. I've had a lot of E-mails from persons using APC's now freely available PowerChute for Linux. APCUPSD is not the same program as PowerChute and they do not create the same log file format. I have some examples of PowerChute log outputs now and am going to try to make APCUPSD work with either format.


    I've added a cron.hourly entry to update the APC status on my intranets which looks a lot like this: APC log analyser at work
    (Click for actual HTML)


    If you find it's useful or wonderful after using it, please add a comment to it's Freshmeat listing.

    VI GLIB / GTK+ highlighting

    Ok, so its not really a script. But if you use GLIB or GTK+ at all, and program with VI, you'll find this helpful. Add the following lines to the beginning of your C syntax file, right after "syn clear": (/usr/share/vim/vim??/syntax/c.vim)

    syntax region cglibfunc start="g_" end="("me=s-1
    syntax region cgdkfunc start="gdk_" end="("me=s-1
    syntax region cgtkfunc start="gtk_" end="("me=s-1
    hi link cglibfunc glibfunc
    hi link cgdkfunc gdkfunc
    hi link cgtkfunc gtkfunc
    

    The problem with this is that it gives some false positives. I think the following regex version should work more precisely (replace the first three lines above with the three below):

    syn match cglibfunc "\<g_[a-z]*\ *("me=s-1
    syn match cgdkfunc "\<gdk_[a-z]*\ *("me=s-1
    syn match cgtkfunc "\<gtk_[a-z]*\ *("me=s-1
    

    Now edit your ~/.vimrc file and add:

    hi glibfunc guifg=cyan
    hi gdkfunc guifg=cyan
    hi gtkfunc guifg=cyan
    

    You should be all set for the next time you open your C source files. Note: The guifg=x only affects GVIM. Feel free to change "cyan" to whatever colour you prefer for your hilighted function names.

    Downloads

    If there's any demand for it, I'll make an RPM out of it, including the log rotating bit.

    What's Up imitator

    On NT servers, I've used a product known as What's Up? I'm slowly working at a GTK+ interface for some scripts I use to check what's up or down and E-mail people and/or run a script when a service goes up/down. I'll refine and post these scripts at some point. If you have your own that do something similar, please send them my way and we can incorporate each others' thoughts. Let's prove Linux can be more admin-friendly then NT.

    Click for complete statistics This page Copyright © 2016 Michael T. Babcock. <mikebabcock@pobox.com>

    It was last updated on the 28th of April, 2000.