Home » Uncategorized » JunOS: haikus to CSV

#!/bin/bash
COMMAND_STRING="show version and haiku"

# Fetch 60 iterations of "show version and haiku"
# Sort and Uniq to generate a masterlist of them

for ((i = 0; i < 60; i++)) do ssh -q -o StrictHostKeyChecking=no "user@router" "${COMMAND_STRING}" > haiku${i}
        count=$(cat haiku${i} | wc -l)
        ((count--))
        ((count--))
        line1=$(cat haiku${i} | sed -n ${count}p | sed -e 's/^[ \t]*//')
        ((count++))
        line2=$(cat haiku${i} | sed -n ${count}p | sed -e 's/^[ \t]*//')
        ((count++))
        line3=$(cat haiku${i} | sed -n ${count}p | sed -e 's/^[ \t]*//')
        count=0

        echo "${line1},${line2},${line3}" >> haiku_masterlist
        echo -e "${line1}\n${line2}\n${line3}\n"
        rm haiku${i}
        sleep 1
done

temp=$(mktemp)
cat haiku_masterlist | sort | uniq > ${temp}
cp ${temp} haiku_masterlist