From: Robert Klemme Date: 2004-02-04T18:00:12+09:00 Subject: Re: String.hex and valid mac addresses : a cleaner way ? "Theodore Knab" schrieb im Newsbeitrag news:20040203205800.GA14769@annapolislinux.org... > Is there a cleaner way to doing this ? > > Here is my code it finds bad mac addresses. > #start of source code > > #!/usr/bin/ruby -w > #find_bad_mac.rb > #description takes input of mac addresses and finds the bad ones > # > #usage pipe a file of mac addresses to this program > # > #cat mac_addresses | find_bad_mac.rb > > linecounter = 0 > > def check_zero(string) > #checks zeros > #valid base 16 numbers are '00' but not '0' > > if string.to_s == "00" > return string=0 > else > return string > end > > end > > while gets > > #a will hold an array of base 16 numbers > a = [] > > #line counter > linecounter = linecounter + 1 > > mac_address = $_.chomp! > > #mac address is transformed into an array called 'a' > a = $_.split(/:/) > > l = linecounter.to_s > > > #each element in array is checked > a.each { |element| > > print "debug " + l + " element:" + element + "\n" > > #tranforms the element into a number > b = element.to_s.hex > > #zeros are sometimes bad but not always > if b == 0 > > #if number is 0 check to see if it is '00' which is a valid base 16 number > #or '0' which is not a valid base 16 number. > > zero = check_zero(element.to_s) > > #if the number is not base 16 > if zero != 0 > > #create a string with all messed up mac entry > s = a.join(":") > > print "error with mac address entry " + s.to_s + "\n" > end > > end > > } > print "-----------------------------------------------\n" > end > ########################################## > #end of source code > ~ > > -- > ------------------------------------------ > Ted Knab > Chester, MD 21619 > ------------------------------------------ > 35570707f6274702478656021626f6c6964796f6e602f66602478656 > 02e6164796f6e60237471647560216e6460276c6f62616c60257e696 > 4797e2a0 > > #!/usr/bin/ruby -w #find_bad_mac.rb #description takes input of mac addresses and finds the bad ones # #usage pipe a file of mac addresses to this program # #cat mac_addresses | find_bad_mac.rb def check_mac(mac_address) #each element in array is checked mac_address.split(/:/).each do |element| $stderr.print "debug #{$.} element: #{element}\n" #if the number is not base 16 return false if element != "00" && element.hex == 0 end true end while ( mac_address = gets ) mac_address.chomp! print "error with mac address entry #{mac_address}\n" unless check_mac mac_address print "-----------------------------------------------\n" end robert