#!/usr/bin/perl -w use Getopt::Std; use strict; my %opts; getopts('rd', \%opts); if ($#ARGV != 0) { print STDERR "Usage: $0 mac-address < profile > config\n"; exit -1; } my $target = $ARGV[0]; if ($target !~ /([[:xdigit:]]{2})[-:]*([[:xdigit:]]{2})[-:]*([[:xdigit:]]{2})[-:]*([[:xdigit:]]{2})[-:]*([[:xdigit:]]{2})[-:]*([[:xdigit:]]{2})/) { print STDERR "Not a valid MAC address: $target\n"; exit -1; } my @mac = unpack('A12', $1.$2.$3.$4.$5.$6); # Note that nibbles are swapped in below print statement! #print STDERR "Target is ";print STDERR @mac;print STDERR "\n"; my %PList; my $cfgParameters = ""; if (defined($opts{'r'})) { # Bit of logic to parse an existing, valid, config file: # my $cfg; while () { $cfg .= $_; } # my ($b1, $b2, $cfgLength, $cfgChecksum, @mac) = unpack('CCnnH12', $cfg); $cfgLength *= 2; my $lastByte = chop $cfg; $cfg .= $lastByte if (ord($lastByte) != 0); # # $cfgParameters = substr($cfg, 16, -10); #printf STDERR ("Checksum was %x\n", $cfgChecksum); } else { while () { next if (/^[\s]*$/); next if (/^[\s]*#/); chop; if (!(/^[\s]*(P[\d]+)[\s]*=[\s]*([\S]*.*)/)) { print STDERR "Badly formed input: \"$_\"\n"; exit -1; } $PList{$1} = $2; } foreach my $p (sort (keys %PList)) { $cfgParameters .= $p."=".$PList{$p}."&"; } } # Now build the full configuration string and compute the checksum my $parameters = $cfgParameters."gnkey=0b82"; my $cfgFile = $parameters; $cfgFile .= chr(0) if ((length($cfgFile) % 2) == 1); my $header = pack('CCnnH12CCCC', 0, 0, length($cfgFile)/2+8, 0, @mac, 0xd, 0xa, 0xd, 0xa ); my $checksum = 0x10000 - unpack('%16n*', $header.$cfgFile); #printf STDERR ("New Checksum is %x\n", $checksum); $header = pack('CCnnH12CCCC', 0, 0, length($cfgFile)/2+8, $checksum, @mac, 0xd, 0xa, 0xd, 0xa ); print $header.$cfgFile;