Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Spent about a day reverse engineering the Bixolon ethernet interface setup protocol, so I could have a utility available on *nix systems.

Written with help from dylan and another co-worker.

Comments/critiques welcome.

#! /usr/bin/perl use strict; use warnings; use IO::Socket 'inet_aton'; use Getopt::Std 'getopts'; $Getopt::Std::STANDARD_HELP_VERSION++; use constant CMD => 'SETT'; my %speeds = ( 1200 => 160, 2400 => 208, 4800 => 232, 9600 => 244, 19200 => 250, 57600 => 254, 115200 => 255, 230400 => 187, ); my $speed_re = join( '|', map( quotemeta, keys %speeds ) ); my @checks = ( { opt => 'm', name => 'mac', req => 1, re => qr/00:15:94:[0-9a +-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}/i }, { opt => 'a', name => 'ip', req => 1, re => qr/\d{1,3}\.\d{1, +3}\.\d{1,3}\.\d{1,3}/ }, { opt => 'g', name => 'gateway', req => 1, re => qr/\d{1,3}\.\d{1, +3}\.\d{1,3}\.\d{1,3}/ }, { opt => 'n', name => 'netmask', req => 0, re => qr/\d{1,3}\.\d{1, +3}\.\d{1,3}\.\d{1,3}/, d +efault => '255.255.255.0' }, { opt => 'p', name => 'port', req => 0, re => qr/\d+/, d +efault => 9100 }, { opt => 's', name => 'speed', req => 0, re => qr/$speed_re/, d +efault => 9600 }, { opt => 'd', name => 'dhcp', d +efault => 0 }, ); my %opts; my $opt_str = join( '', map { defined $_->{re} ? "$_->{opt}:" : $_->{o +pt} } @checks ); getopts( $opt_str, \%opts ); foreach my $check (@checks) { if ( not $check->{re} ) { $opts{ $check->{opt} } ||= 0; } else { $opts{ $check->{opt} } = $check->{default} if ( not $check->{req} and not defined $opts{ $check->{opt +} } ); HELP_MESSAGE( "$check->{name} (-$check->{opt}) is required" ) if ( not defined $opts{ $check->{opt} } ); HELP_MESSAGE( "$opts{ $check->{opt} } is invalid input for $ch +eck->{name}" ) if ( not $opts{ $check->{opt} } =~ /^$check->{re}$/ ); } } my $sock = IO::Socket::INET->new( PeerAddr => '255.255.255.255', PeerPort => 48781, Proto => 'udp', Broadcast => 1, LocalPort => 48780, ) or die "Can't bind : $@\n"; my $cmd = pack( 'aaaa', split( //, CMD ) ); my $mac = pack( 'H2H2H2H2H2H2', split( /:/, $opts{m} ) ); my $port = pack( 'n', $opts{p} ); my $ip = inet_aton( $opts{a} ); my $nm = inet_aton( $opts{n} ); my $gw = inet_aton( $opts{g} ); my $baud = pack( 'C', $speeds{ $opts{s} } ); my $dhcp = pack( 'C', $opts{d} ); my $payload = join('', $cmd, $mac, $ip, $nm, $gw, $port, $baud, "\x01\ +x01", $dhcp, "\x00\x00" ); $sock->send($payload); printf "%10s: %-25s\n", uc( $_->{name} ), $opts{ $_->{opt} } foreach +(@checks); sub HELP_MESSAGE { my $die_message = shift; print "bix.pl - Setup a Bixolon Ethernet Interface\n"; print "Usage:\n"; print " bix.pl -m <mac> -a <ip> -n <netmask> -g <gateway> -p <p +ort> [ -d ] [-s <speed> ]\n\n"; print "\t\t-m\tMAC Address ex. ff:ff:ff:ff:ff:ff\n"; print "\t\t-a\tIP Address ex. 192.168.1.200\n"; print "\t\t-g\tGateway ex. 192.168.1.1\n"; print "\t\t-n\tNetmask (default:255.255.255.0)\n"; print "\t\t-p\tPort 1-65535 (default:9100)\n"; print "\t\t-d\tDHCP Enable (default:off)\n"; print "\t\t-s\tBaud Rate 1200,2400,4800,9600,19200,57600,115200 +,230400 (default:9600)\n"; print "\n\t\t--help\tThis Message\n"; die "$die_message\n" if (defined $die_message); }
grep
One dead unjugged rabbit fish later...

In reply to Bixolon Ethernet Interface Setup Utility by grep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-10-26 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?