The config file format is simple -- IP address followed by MAC address,
separated by whitespace. Pound at the beginning of a line indicates
comment.
This has only been tested on Linux -- people on other platforms may need
to adjust the parameters to arp in the system call.
It is a quick 'n' dirty program, but works -- maybe it will be useful to
somebody out there, too.
Note: you want to make sure that it is run after your network interface is
brought up but before any servers or clients are started; otherwise,
somebody may be able to sneak in a connection before the ARP tables are
"locked".
Here's the script:
#!/usr/bin/perl
# by John Goerzen <jgoerzen@cs.twsu.edu>
# Program: forcehwaddr
# Program to run ARP to force certain tables.
# Specify filenames to read from on command line, or read from stdin.
foreach (<>) { # For each input line....
chomp; # Strip if CR/LF
if (/^#/) { next; } # If it's a comment, skip it.
if (((($host, $hw) = /\s*(.+?)\s+(\S+)\s*/) == 2) &&
!(/^#/)) {
# The text between the slashes parses the input line as follows:
# Ignore leading whitespace. (\s*)
# Then, start matching and put it into $host ($host, (.+?))
# Skip over the whitespace after that (\s+)
# Start matching. Continue matching until end of line or optional
# trailing whitespace.
# Then, the if checks to see that both a
# host and a hardware address were matched.
# (2 matches). If not, we skip the
# line (assuming it is blank or invalid or something).
# The second part of the if checks to see if the line starts with
# a pound sign; if so, ignore it (as a comment).
# Otherwise, run the appropriate command:
printf("Setting IP %-15s to hardware address %s\n", $host, $hw);
system "/usr/sbin/arp -s $host $hw\n";
}
}
-- John Goerzen Southwind Internet Access, Inc. Technical Support Business e-mail: jgoerzen@southwind.netPersonal e-mail: jgoerzen@complete.org Wichita State University e-mail: jgoerzen@cs.twsu.edu Developer, Debian GNU/Linux <http://www.debian.org>