11 Replies Latest reply on Oct 11, 2016 12:11 PM by Yanick Girouard

    While read loop using nexec

      I'm having a bit of a problem with nexec.

       

      Lets say you have a file that contains a list of systems defined in BL named hosts.txt.

       

      The following works, using a NSH Here on one of the systems.

       

      while read host

      do

        echo $host

      done < hosts.txt

       

      but the following only works for the first iteration.

       

      while read host

      do

        nexec $host uname -a

      done < hosts.txt

       

      nexec seems to break out of the while loop.

       

      Any ideas?  Thanks in advance for the assistance.

        • 1. While read loop using nexec
          Jim Campbell

          Do you use an nsh proxy?  We had problems with running out of nsh proxy contexts and it would stop working after the max was reached.  What worked for us was to add an explicit disconnect for the targetted host inside the loop.

          • 2. While read loop using nexec
            Bill Robinson

            i had the same issue.  you should open a ticket on this.

             

            you can change it to work like:

             

            for server in `cat hosts.txt`

            do

            nexec ${server} uname -a

            done

            1 of 1 people found this helpful
            • 3. While read loop using nexec
              Bill Robinson

              cat hosts.txt | while read line

              do

              ...

               

              should also work.  not sure why reading it directly in to the while causes an issue.

              • 4. Re: While read loop using nexec
                Patrick O'Callaghan

                I'm running into this same issue.  I have a file that has hostnames, and a separate file named by the hostname which has the IP address.  I am trying to read from the hostnames file, and use the returned value to read from the IP address file.

                 

                The big issue is, the IP address file has to be converted using "iconv" which needs nexec to run within an nsh job.

                 

                #!/bin/bash

                cat /tmp/templates.txt | while read FILENAMES

                do

                     IPADDR=$(cmd /c cat "/tmp/$FILENAMES.txt")

                     echo "IP from the Cat $IPADDR"  ------> Outputs: IP from the Cat ▒▒1 0 . 1 5 5 . 1 8 1 . 2 5 1 (4 entries)

                     IPADDR=$(cmd /c cat /tmp/$FILENAMES.txt | cmd /c iconv -f UTF-16 -t UTF-8 | tr -d '\n\r')

                     echo "IP from the iconv $IPADDR" -------> Outputs: IP from the iconv 10.155.181.251 (4 correct entries)

                done

                 

                Once I introduce nexec (tried in many formats) it will either hang indefinitely or if I remove the cat at the beginning and just use:

                $(nexec -i localhost iconv -f UTF-16 -t UTF-8 /tmp/$FILENAMES | tr -d '\n\r')

                echo "IP from the iconv $IPADDR" -----> Outputs:

                IP from the iconv FILENAME1FILENAME2FILENAME310.155.145.251 ( 3 incorrect, 1 correct)

                 

                I am on BSA 8.6.01.71

                • 5. Re: While read loop using nexec
                  Bill Robinson

                  this: cat /tmp/templates.txt | while read FILENAMES isn't the best actually.  you can do:

                   

                  while read FILENAMES

                  do

                  ...

                  done < /tmp/tmplates.txt

                   

                  why not

                   

                  tr -d '\n\r' <<< "$(cmd /c iconv -f UTF-16 -t UTF-8 /tmp/${FILENAMES}.txt)"

                  • 6. Re: While read loop using nexec
                    Patrick O'Callaghan

                    I have tried the above as well.  Both the while read and the cat | while read work with iconv if I run the nsh script from the command line.  As soon as I run it in an nsh script job, it says command not found: iconv.

                     

                    As soon as I introduce nexec to the while read or the cat | while read it messes everything up, as mentioned, either only getting one correct value out of 4, hanging indefinitely, or even returning with "?????????????????" depending on where in the line i put nexec.

                    • 7. Re: While read loop using nexec
                      Bill Robinson

                      is iconv installed on the appserver running the job ?  and is it in the system path ?

                      • 8. Re: While read loop using nexec
                        Wenchi Liao

                        When inside a while loop, nexec seems to read and discard stdin. When that happens, the nexec inside runs once, and the loop ends (nothing else to loop through).

                         

                        Try the -n flag to nexec:

                         

                        % echo $#targets

                        4

                        % print -l $targets | while read h

                        pipe while> do

                        pipe while> nexec $h hostname

                        pipe while> done | wc -l

                          1

                        % print -l $targets | while read h

                        do

                        nexec -n $h hostname

                        done | wc -l

                          4

                        % nexec

                        Usage: nexec [-i] hostname cmd ...

                          nexec [-i] -e cmd ...

                         

                        Execute remote command

                          -i Remote command is interactive (no stderr returned)

                          -l Behave like a login session

                          -r Do not transcode input/output

                          -u Output as UTF-8

                          -n Do not read in any stdin

                        2 of 2 people found this helpful
                        • 9. Re: While read loop using nexec
                          Patrick O'Callaghan

                          Perfect! nexec -n did it.  Thank you!

                          1 of 1 people found this helpful
                          • 10. Re: While read loop using nexec
                            Olivier Renault

                            in my case nexec -l solved the problem

                            • 11. Re: While read loop using nexec
                              Yanick Girouard

                              Just a thought, while read will use the internal field separator variable (IFS) to separate its items. You may want to explicitly set it to $'\r'', and then make sure you remove all \r from the file before you pass it, like so:

                               

                              cat filename | tr -d "\r" | while read h

                              do

                                   # do something with $h

                              done

                               

                              EDIT: Just saw the note about nexec -n, that's happened to me too before, so if that works, good