String.hex and valid mac addresses : a cleaner way ?

T

Theodore Knab

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
~

--
 
R

Robert Klemme

Theodore Knab said:
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
~

--

#!/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top