bashparsing

Getting rid of spaces in a returned string in bash


This is a follow-up from another thread here. https://shorturl.at/kyAu3

I'm getting SMMPv3 enigineID's from several thousand devices that may get replaced with new ones and any time due to break/fix. I have an input list of the devices. I need to get this result written to /etc/snmp/snmptrapd.conf when all is said and done.

createUser -e 800092C404534E2D353332303435383232 UserName SHA Authphrase AES passphrase

Here's what I'm doing

while IFS=, read -r myIP UserIn _
do
    UserName=''
   
    case $UserIn in
      EverNeXT)
          UserName="DeviceType_1"
          ;;
      UPS)
          UserName="DeviceType_2"
          ;;
      *)
          echo "$(date '+%Y-%m-%d %H:%M:%S') Exception! Missing or incorrect information in $DeviceList, look for $myIP or $UserIn" >> $myLog
          UserName="foobar"
    esac

Then I do an snmpget to the FQDN's from the above to get the engineID which is a hex string, with a space between every two values. I need to strip the spaces and write it to snmptrapd.conf

if test $snmpget_RC -eq 0
then
    set -- $(snmpget -v3 -l authPriv -u $UserName -a SHA -A Authphrase -x AES -X Passphrase $myIP SNMP-FRAMEWORK-MIB::snmpEngineID.0)
    shift 3
    IFS=""
    echo "Hex value for $myIP is $*"
    echo 'createUser -e' "$*" $UserName' SHA Authphrase AES Passphrase ' >>$snmpPath/$MyFile
else
    echo "$(date '+%Y-%m-%d %H:%M:%S') $myIP is not reachable" >> $myLog
    RC=0
fi

I got some greatly appreciated help in the other thread, but comments doesn't let me post this followup information and I'm not really answering my own question.

I do get one instance of the engineID written correctly, but the rest is garbage. Here's what the relevant part of my snmptrapd.conf looks like when done

createUser -e 800092C404534E2D353332303435383232 UserName SHA Authphrase AES passphrase
createUser -e SNMP-FRAMEWORK-MIB::snmpEngineID.0 = Hex-STRING: 80 00 92 C4 04 53 4E 2D 35 33 32 30 36 30 35 37
39  UserName SHA Authphrase AES Passphrase

The first line is perfect, the rest isn't usable. Is it because I'm using IFS twice? or something else? More importantly, how to I get it so all lines in snmptrapd.conf are like the first one?

Regards, Tom


Solution

  • You didn't give this context in your previous question. Yes, looping over this sequence causes trouble, because on the second iteration the IFS="" setting is in effect for the set -- $(snmpget ...) making it not split as you want.

    However, you also didn't specify bash on your previous question. With bash the first solution I gave before -- read -d '' x x x hex < <(snmpget ...) then ${hex//[:space:]} -- is probably better. Or you can use this modified approach which doesn't change IFS:

       set -- $(snmpget ...) # unquoted, splits on default IFS including newline
       shift 3; hex=$* # rebuilt with spaces (but not newline)
       echo createuser -e ${hex// } ... # removes spaces in some shells including bash