im pretty sure this is a remedial task for many of you but im having an issue with arrays from a shell script being accessed in an awk command. im pretty good with shell scripting but i am embarrassingly unfamiliar with awk. so here's the meat of the script...
i am trying to take an input file of ip addresses and corresponding netmasks and put it into a format to be loaded onto a juniper switch. the result should look something like this.. x.x.x.x/netmask using the cidr notation. no matter what subnet is provided though, /32 always gets appended to the end of the ip even when it should be /16, /24, etc... also, the cisco part works fine so that doesnt need any attention.Code:masks=( "0.0.0.0" "0.0.0.1" "0.0.0.3" "0.0.0.7" "0.0.0.15" "0.0.0.31" "0.0.0.63" "0.0.0.127" \ "0.0.0.255" "0.0.1.255" "0.0.3.255" "0.0.7.255" "0.0.15.255" "0.0.31.255" "0.0.63.255" "0.0.127.255" \ "0.0.255.255" "0.1.255.255" "0.3.255.255" "0.7.255.255" "0.15.255.255" "0.31.255.255" "0.63.255.255" \ "0.127.255.255" "0.255.255.255" "1.255.255.255" "3.255.255.255" "7.255.255.255" "15.255.255.255" \ "31.255.255.255" "63.255.255.255" "127.255.255.255" "255.255.255.255" ) bits=( "32" "31" "30" "29" "28" "27" "26" "25" "24" "23" "22" "21" "20" "19" "18" "17" "16" "15" \ "14" "13" "12" "11" "10" "9" "8" "7" "6" "5" "4" "3" "2" "1" "0" ) if [ $# -lt 3 ] then echo "Usage: $0 <Input File (Crump)> <Output File (Blacklist)> <Format (cisco|juniper)>" fi awk=`which awk` input=$1 output=$2 format=$3 if [ $format = "cisco" ] then $awk -v null="" 'BEGIN { IFS=" "; IRS="\n"; OFS=" "; ORS="\n"; } { line=0; print "deny ip " $2 " " $3 " " "any"; }' < $input > $output elif [ $format = "juniper" ] then $awk -v masks="$masks" -v bits="$bits" 'BEGIN { IFS=" "; IRS="\n"; OFS=" "; ORS="\n"; } { line=0; for (i=0; i<=31; i++ ) { if($3 == "${masks[$i]}") { break; } } print "\t" $2 "/" ${bits[$x]}; } ' < $input > $output fi echo "DONE!"
any ideas on what i'm doing wrong?




Reply With Quote
