Intro
News
Humor
Sysadmin
Programming
Books
Screenshots
Firefox Extensions
Kitty
Links!
Employment

Here, I'll list some of the minor stuff all sysadmins and programmers tend to develop as a "quirk" after a while. These are ways of solving problems that are either not immediately obvious, or just plain interesting. I hope they prove useful to you.


Flushing giant sendmail queue backlogs.


When you have a large backlog of mail in the sendmail queue, the directory size gets really large. Scanning a directory under most UNIX filesystems takes a very long time. Since the usual method is an O(n) operation, where n is the maximum number of entries that ever existed in the directory (very few UNIX variants shrink directories), scanning it will take quite a long time.

In order to speed sendmail up again, here's a small trick you can use:

/etc/init.d/sendmail stop
cd /var/spool
mv mqueue mqueue.old
mkdir mqueue
/etc/init.d/sendmail start
for i in 0 1 2 3 4 5 6 7 8 9 ; do
	mkdir mqueue.old.$i
	mv mqueue.old/??$i* mqueue.old.$i
done
for i in 0 1 2 3 4 5 6 7 8 9 ; do
	sendmail -q -v -oQ/var/spool/mqueue.old.$i
done
rmdir mqueue.old

Trimming large pathname lists in environmental variables


When you have a single ~/.profile that can be used on many different systems (think NFS-mounted $HOME), there are times when trimming unnecessary components from one's PATH and LD_LIBRARY_PATH can be useful. This optimization comes in handy particularly on systems that mount many of the path components via NFS over a congested network. Adding this snippet to your ~/.profile will cause a performance hit only with the first shell of the session. All subsequent commands will run normally.

NPATH=""
for i in `echo $PATH|tr ':' ' '` ; do
	if [ -d "${i}" ] ; then
		if [ x"$NPATH" = x ] ; then
			NPATH="$i"
		else
			NPATH="$NPATH:$i"
		fi
	fi
done
PATH="${NPATH}" export PATH
unset NPATH

Telnet-based TCP port scanner.


Okay, this hack is a bit cheesy, but I end up doing it at almost every place I go to sooner or later, when I don't have access to nmap, any programming languages that supports BSD sockets, and so on. Please note, this scanner only does full TCP connect() scans. It's also incredibly slow. But it works.

Also, patch from martinjd was applied. Really stupid bug I didn't catch before. Oops.

#!/usr/bin/env bash
echo '^]close^M' > send # Note, that's control-], "close", control-m
			# You can generate this sequence via ^V^]close^V^M
			# in bash.
MAXPORTNUM=1024		# Highest port you want to scan.  Set to 0 to ignore.
PORTLIST="21 22 23 53 80 111"	# List of ports to scan if MAXPORTNUM==0
A=1
if [ $MAXPORTNUM != 0 ] ; then
	while [ $A -lt $MAXPORTNUM ] ; do
		echo "attempting [port $A]"
		telnet $1 $A < send
		# Address is the IP address or
		# hostname you want to scan.
		A=$((A+1))
	done
else
	for i in $PORTLIST ; do
		echo "attempting [port $i]"
		telnet $1 $i < send
	done
fi