Sorting IP Addresses
By · CommentsThe following will sort an array of IP addresses in @in and the sorted IP addresses will be in @out.
@out = sort { pack('C4' => $a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) cmp pack('C4' => $b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) } @in;
What this does is it forms a string of four bytes out the IP address octet using the pack() function then sorts it lexicographically.
See also Sorting Section Numbers
Determining the External IP Address
By · CommentsHere’s how to find out your external IP address courtesy of this hint:
http://www.macosxhints.com/article.php?story=20060602180942480
curl --silent http://checkip.dyndns.org | awk '{print $6}' | cut -f 1 -d "<"
If you are using Apple's Airport Extreme Basestation (mine is particularly the Time Capsule and this is where I have tested this), and you have the SNMP interface enabled, you can run the following command
prompt$ snmpwalk -Os -c public -v 1 192.168.63.1 ipAdEntAddr IpAddress \
| grep -E -v '(127.0.0|169.254|192.168.63.1)' \
| cut -d : -f 2 | sed 's/ //g'
Write a Daemon in Perl
By · CommentsThe code below is a template for a daemon written in Perl. Use the code below as a starting point when you have to write a program that has to persist in the background to do its things and without a gui.
use POSIX qw(setsid); chdir '/' or die "Can't chdir to /: $!"; umask 0; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; #open STDOUT, '>/dev/null' # or die "Can't write to /dev/null: $!"; open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; while(1) { sleep(5); print "Hello...n"; }
Note that one of the lines above is commented out to let the output print to the screen. Uncomment this in the final code to silence your program. For more on this code, see this tutorial: http://www.webreference.com/perl/tutorial/9/.
Line Terminations
By · CommentsLine terminations for different operating systems:
| unix | 0×0a | LF |
| Classic Mac | 0×0d | CR |
| Windows | 0×0d 0×0a | CR LF |
To convert a text file with DOS line termination to UNIX line termination:
tr -d '\015' < winfile.txt > unixfile.txt
or
sed s/.$// winfile.txt > unixfile.txt
To convert a unix file to a DOS file:
sed s/$/\x0d/ unixfile.txt > winfile.txt
Run Periodic Maintenance Scripts Manually
By · CommentsMac OS X has to run some maintenance scripts in the middle of the night to do some important housekeeping tasks. If you have a Mac that sleeps in the middle of the night, as you do, you need to run these manually by running this from the shell:
sudo periodic daily weekly monthly
Screen Size vs. Viewing Distance
By · CommentsThe following is a rule of thumb on the minimum viewing distance for a certain screen size:
| Display Type | Minimum Viewing Distance |
|---|---|
| 16:9 HDTV | 1.5x Diagonal |
| 4:3 HDTV | 1.8x Diagonal |
| Analog | 3x Diagonal |
Run as root only
By · CommentsTo make sure that a script will be run by root only:
#!/bin/sh if [ `id -u` != 0 ]; then echo "Permission denied, must be root" exit fi # Do the thing...
Authenticate Users
By · CommentsThis snippet could authenticate users using their /etc/passwd or /etc/shadow entry. May have to run this with higher than normal privilege:
#!/usr/bin/env perl print "Username: "; chomp($uname = <stdin>); $pwd = (getpwnam($uname))[1]; # get the user's pwd die "invalid user\n" unless defined $pwd and length $pwd; $salt = substr($pwd, 0, 2); system "stty -echo"; print "Password: "; chomp($word = <stdin>); print "\n"; system "stty echo"; if (crypt($word, $salt) ne $pwd) { die "Sorry...\n"; } else { print "ok\n"; }
How to Burn CDs
By · CommentsFor accomplishing other tasks such as copying CDs, etc., try carefully reading the cdrecord manual page and, if necessary, consulting the canonical CD recording FAQ at http://www.cdrfaq.org/.
——————————————————
Creating a data CD:
Step 1: Make a directory to contain the files you want to place on the the CD. “mkdir /image/userdir/” is the standard method.
Step 2: Copy the files from wherever they are to the directory you just created.
Step 3: Create the ISO9660 image that will be burned on the CD. You do this using the mkisofs command. An example command is:
csh> cd /image/userdir/ csh> mkisofs -o /image/burn-image/.iso -l -R -L -V "" -P "" -p "" -A "" . -o: the name of the file that will contain the image -l: use long file names -R: use Rock Ridge extensions. This means that long filenames will be used, file uid/gids and permissions will be preserved, symbolic links will be included, etc. In other words, the CD will try hard to emulate a Unix file system. [-r: This is like the -R option, but file uid/gid are set to 0, files will be readable by anyone and all write permissions will be removed. Use this switch if you anticipate needing to read the CD in an environment where your uid/gid do not exist. E.g., if you are sending to CD to another institution.] -L: Allow file names beginning with '.'. -V, -P, -p, -A: See the mkisofs man page. The final argument (in the example '.') is the name of the top-level directory containing file data. See the mkisofs manual page for further details.
Disable single user mode using lilo
By · CommentsIn your LILO configuration section, write something like:
image = /vmlinuz label = Linux root = /dev/hda1 password = wingedlizard restricted read-only
Then chmod this file 600 (so nobody but root can read it) and re-run /sbin/lilo.
The “restricted” keyword means that LILO will stop and ask for a password if you try to boot this kernel with _any_ keywords such as “1″ or “single” or “init=/bin/bash”. A password won’t be required during normal (no-added-keywords) boots.