3 Replies Latest reply on May 29, 2020 7:37 AM by Bill Robinson

    When executing a NSH/Nexec inside a while loop that read a file it breaks loop

    Alexander Foldats
      Share This:

      Hello,

       

      I have been developing a NSH script that basically runs though a file and with in each line of the file I extract certain information to then do a call to a script that launches a nexec to another machine to recuperate passwords. What I have noticed is that when I do this the loop is broken and the variable I get back from the script is including the rest of the loop in the variable.

       

      Here is the code I'm using:

       

      loop=0
      while read i;do
      host=`echo $i|awk '{print $8}'|awk -F "://" '{print $2}'|awk -F ":" '{print $1}'`
      replicaset=`echo $i|awk '{print $8}'|awk -F "=" '{print $2}'`
      echo "host = $host"
      echo "replicaset = $replicaset"
      nsh "//fileserver/opt/bmc/bladelogic/NSH/storage/scripts/2792280.1_Spufis Get Cyberark Password.nsh.nsh" "$host" "$replicaset"
      (( loop++ ))
      echo "loop run = $loop"
      done<execute.bat
      

       

      The file in question has 4 lines so in theory it should loop 4 times, if I run it without the nsh call:

       

      \u@\h:\w# loop=0
      \u@\h:\w# while read i;do
      while> host=`echo $i|awk '{print $8}'|awk -F "://" '{print $2}'|awk -F ":" '{print $1}'`
      while> replicaset=`echo $i|awk '{print $8}'|awk -F "=" '{print $2}'`
      while> echo "host = $host"
      while> echo "replicaset = $replicaset"
      while> (( loop++ ))
      while> echo "loop run = $loop"
      while> done<execute.bat
      
      host = lep1osb1
      replicaset = replODpro
      loop run = 1
      host = lep1osb1
      replicaset = replODpro
      loop run = 2
      host = lep1osb1
      replicaset = replODpro
      loop run = 3
      host = lep1osb1
      replicaset = replODpro
      loop run = 4
      

       

      works fine, but if I include the NSH call:

       

      \u@\h:\w# loop=0
      \u@\h:\w# while read i;do
      while> host=`echo $i|awk '{print $8}'|awk -F "://" '{print $2}'|awk -F ":" '{print $1}'`
      while> replicaset=`echo $i|awk '{print $8}'|awk -F "=" '{print $2}'`
      while> echo "host = $host"
      while> echo "replicaset = $replicaset"
      while> nsh "//fileserver/opt/bmc/bladelogic/NSH/storage/scripts/2792280.1_Spufis Get Cyberark Password.nsh.nsh" "$host" "$replicaset"
      while> (( loop++ ))
      while> echo "loop run = $loop"
      while> done<execute.bat
      
      host = lep1osb1
      replicaset = replODpro
      F:\SPUFIS_MONGO\BIN\mongo\mongo.exe -u user_spufi -p password --authenticationDatabase admin mongodb://lep1osb1:27017,lep1osb2:27017,lep2osb1:27017,lep2osb2:27017,lep1osr1:27017/order-flow?replicaSet=replODpro F:\SPUFIS_MONGO\LOGS\EN_EJECUCION\CRQ000000295341\Borrado_pedido_orderExecutions1.js
      F:\SPUFIS_MONGO\BIN\mongo\mongo.exe -u user_spufi -p password --authenticationDatabase admin mongodb://lep1osb1:27017,lep1osb2:27017,lep2osb1:27017,lep2osb2:27017,lep1osr1:27017/order-flow?replicaSet=replODpro F:\SPUFIS_MONGO\LOGS\EN_EJECUCION\CRQ000000295341\Borrado_pedido_orderExecutions2.js
      F:\SPUFIS_MONGO\BIN\mongo\mongo.exe -u user_spufi -p password --authenticationDatabase admin mongodb://lep1osb1:27017,lep1osb2:27017,lep2osb1:27017,lep2osb2:27017,lep1osr1:27017/order-flow?replicaSet=replODpro F:\SPUFIS_MONGO\LOGS\EN_EJECUCION\CRQ000000295341\Borrado_pedido_orderExecutions3.js
      user_spufi,password
      loop run = 1
      

       

      the retun of the script should only be : "user_spufi,password" but as you can see, it is returning that but before the rest of the loop!

       

      if I run this call alone it work as expected:

       

      \u@\h:\w# nsh "//fileserver/opt/bmc/bladelogic/NSH/storage/scripts/2792280.1_Spufis Get Cyberark Password.nsh.nsh" "lep1osb1:27017,lep1osb2:27017,lep2osb1:27017,lep2osb2:27017,lep1osr1:27017/order-flow?replicaSet=replODpro" "replODpro"
      user_spufi,password
      

       

      this is the nsh script:

       

      #!/bin/nsh
      
      get_password()
      {
          ###CYBERARK PARAMETERS###
          SafeName_sys=LPL_SPUFI_MONGO_LADM
          #ObjectName_sys=$TARGET-$BBDD-user_spufi
          ApplicationID=app_W0908PIN0944
          FolderName=Root
          CYBHOST="W0908PIN0944"
          hostsconn=$1
          replicaset=$2
          
          ###GET PASSWORD###
          host=`echo $hostsconn|awk -F ":" '{print $1}'`
          ObjectName_sys=$host-$replicaset-user_spufi
          WINBAT="\"C:\Program Files (x86)\CyberArk\ApplicationPasswordSdk\CLIPasswordSDK64.exe\" GetPassword /p AppDescs.AppID^=$ApplicationID /p Query^=Safe^=$SafeName_sys^;Folder^=$FolderName^;Object^=$ObjectName_sys /p RequiredProps^=UserName^,Address /o PassProps.UserName^,Password"
          CYBPWDT=`/opt/bmc/bladelogic/NSH/bin/nexec -ncq $CYBHOST cmd /c "$WINBAT"`
          #echo "CYBPWDT = $CYBPWDT"
          #CYBPWD=`echo -n $CYBPWDT |awk -F',' '{print $2}'|tr '\r' ' '| sed 's/ //g'`
          #CYBPWD=`echo $CYBPWDT|awk -F',' '{print $2}'|tr '\r' ' '| sed 's/ //g'`
          echo "$CYBPWDT"
          ###GET PASSWORD###
          
      }
      
      get_password $1 $2
      

       

      any idea as to why when this is called in the loop it breaks it and feeds the variable with the rest of the file??? same thing happens if I include the call inside the loop, thats why I tried creating a seperate script to see if it would help, but does not.