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
~
where each h is 0-f, and there are any number of :hh pairs after the
first? Why not a regex?
while s = gets
if(s !~ /[\da-fA-F][\da-fA-F](:[\da-fA-F][\da-fA-F])*/)
print "#{s} is bad!\n"
end
end
Note: I just typed that code out, I think the regex is right, the
rest... no idea.
Don't you want to know there are exactly 6 hh pairs, by the way, at
least for an ethernet MAC addr?
Cheers,
Sam
Quoteing tjk@annapolislinux.org, on Wed, Feb 04, 2004 at 05:58:01AM +0900:
···
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
~
while gets
a = $_.split(/:/)
a.each{ |element|
puts "debug #{$.} element: #{element}"
if( element =~ /^[\da-fA-F][\da-fA-F]$/ ) then #if it’s valid
else #if the element is not valid
puts “bad element”; end } end
or for a shorter version:
while gets
puts "Reading in #{$.chomp}:"
a = $.split(/:/)
a.each{ |element|
puts “\tFound bad element #{element}” unless ( element =~
/^[\da-fA-F][\da-fA-F]$/ ) } end
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