0

I've been struggling to come up with a solution for this, the goal is to organize the results into comma separated columns as we have to recable our entire datacenter from cat6 to cat6a.

$DBFILE output is access1a access1b ...

goal:

    switch,switch-port,server,server-port
    access1a,1,server6,eth0
    access1a,2,server4,eth0
    access1a,3,server1,eth0

how ever my current output is the following:

#!/bin/sh
DBFILE=$(cat /tmp/routers.all | awk -F: '{print $1}'| grep access)

for OUTPUT in $DBFILE
do
    /usr/bin/snmpwalk -Os -c pass -v 2c $OUTPUT iso.0.8802.1.1.2.1.4.1.1.8.0 | tr -d "\"" | sed -r 's/ /./g' |awk  -F. '{print "'"$OUTPUT"'"","$13","$22","}'
    /usr/bin/snmpwalk -Os -c pass -v 2c $OUTPUT iso.0.8802.1.1.2.1.4.1.1.9.0 | tr -d "\"" | sed -r 's/ /./g' |awk  -F. '{print "'"$OUTPUT"'"","$13","$17","}'
done

access1a,1,server6,
access1a,2,server4,
access1a,3,server1,
access1a,1,eth0,
access1a,2,eth0,
access1a,3,eth0,

I've tried many different variations of arrays and for loops and either i only get the very last query or no success at all, i figured i'd ask as i can't seem to find methods to accomplish the following.

0

A simple route forward is to put the output of each invocation of snmpwalk into a variable and then concatenate them with a little awk filtering, something like:

#!/bin/sh
DBFILE=$(cat /tmp/routers.all | awk -F: '{print $1}'| grep access)

for OUTPUT in $DBFILE do
    FIRST_HALF=`/usr/bin/snmpwalk -Os -c pass -v 2c $OUTPUT iso.0.8802.1.1.2.1.4.1.1.8.0 | tr -d "\"" | sed -r 's/ /./g' |awk  -F. '{print "'"$OUTPUT"'"","$13","$22","}'`
    SECOND_HALF=`/usr/bin/snmpwalk -Os -c pass -v 2c $OUTPUT iso.0.8802.1.1.2.1.4.1.1.9.0 | tr -d "\"" | sed -r 's/ /./g' |awk  -F. '{print "'"$OUTPUT"'"","$13","$17","}' |awk 'BEGIN{FS=","}{print($3)}'`
    echo ${FIRST_HALF}${SECOND_HALF}
done

which I think results in

access1a,1,server6,eth0
access1a,2,server4,eth0
access1a,3,server1,eth0

Does this help?

Disclosure: I am not familiar with snmpwalk, so there may be a better option.

  • echo ${FIRST_HALF}${SECOND_HALF} should be echo "${FIRST_HALF}""${SECOND_HALF}" or it outputs as one long string No luck tho it outputs is same: access1a,50,SERVER1a, access1a,52,SERVER3A,eth0 eth0 eth0 Only the very last port of the first variable result shows correct else is separate. – Matheus May 20 '15 at 14:25
  • I think I misunderstood the question. Is the output that you show the result of one or three iterations of the for loop? – sergut May 20 '15 at 14:37
  • I have tried with a different approach in case it is more useful. If it is does, I will delete this answer. – sergut May 20 '15 at 15:06
0

I am not familiar with snmpwalk but it seems from the question (and from my former invalid answer) that it produces several lines of output. It also seems to me like most of the result of your script is repetitive/redundant, i.e. there are always two lines with the format:

A,B,C
...
A,B,D

and you want another ouput with half the lines and looking like

A,B,C,D

In this case, it may be easier to leave your script as it is and pass the output through a pipe to the following AWK script:

BEGIN{
  FS=",";
}
{
  if ( ($1,$2) in A ) {
     print A[$1,$2] "," $3;
  } else {
     A[$1,$2]=$1 "," $2 "," $3;
  }
}

which looks like this for a one-liner that you can cut-and-paste.

| awk 'BEGIN{FS=","}{if ( ($1,$2) in A ) {print A[$1,$2] "," $3;} else {A[$1,$2]=$1 "," $2 "," $3;}}'

Does this help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.