Ping a list of IPs with strings of names listed in a txt file and must use batch












2














I want to ping a list of IPs with a string of text entered next to it. The text will be multiple words and have numbers. The IPs all start with 10.x.x.x.



This is a script that I was looking into, but it tries to resolve the IP of the IP that I put into it. I guess it would work if I put the hostnames in there. Maybe I should keep that in there just in case.



@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)


What I really want is to have a text string like "This is the Server XYZ" to be concatenated at the end of line for the result.txt file.



So my testservers.txt file will look like this:



10.0.0.1 This is the Server ABC.
10.0.0.2 This is the Server DEF.
hostname1 This is the Server LMN.
hostname2 This is the Server XYZ.


When I run it now, it spits out results like this into the result.txt file.



10.0.0.1 [10.0.0.1] is UP
10.0.0.1 [10.0.0.2] is UP
hostname1 [10.0.0.3] is UP
hostname2 [10.0.0.4] is UP


Then the result.txt file would look like this:



10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
10.0.0.2 [10.0.0.2] This is the Server DEF.
hostname1 [10.0.0.3] This is the Server LMN.
hostname2 [10.0.0.4] This is the Server XYZ.


Hope I provided enough information. Let me know if I didn't.










share|improve this question





























    2














    I want to ping a list of IPs with a string of text entered next to it. The text will be multiple words and have numbers. The IPs all start with 10.x.x.x.



    This is a script that I was looking into, but it tries to resolve the IP of the IP that I put into it. I guess it would work if I put the hostnames in there. Maybe I should keep that in there just in case.



    @echo off
    setlocal enabledelayedexpansion

    set OUTPUT_FILE=result.txt
    >nul copy nul %OUTPUT_FILE%
    for /f %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
    if %%x==Pinging set SERVER_ADDRESS=%%y
    if %%x==Reply set SERVER_ADDRESS=%%z
    if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
    )


    What I really want is to have a text string like "This is the Server XYZ" to be concatenated at the end of line for the result.txt file.



    So my testservers.txt file will look like this:



    10.0.0.1 This is the Server ABC.
    10.0.0.2 This is the Server DEF.
    hostname1 This is the Server LMN.
    hostname2 This is the Server XYZ.


    When I run it now, it spits out results like this into the result.txt file.



    10.0.0.1 [10.0.0.1] is UP
    10.0.0.1 [10.0.0.2] is UP
    hostname1 [10.0.0.3] is UP
    hostname2 [10.0.0.4] is UP


    Then the result.txt file would look like this:



    10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
    10.0.0.2 [10.0.0.2] This is the Server DEF.
    hostname1 [10.0.0.3] This is the Server LMN.
    hostname2 [10.0.0.4] This is the Server XYZ.


    Hope I provided enough information. Let me know if I didn't.










    share|improve this question



























      2












      2








      2


      1





      I want to ping a list of IPs with a string of text entered next to it. The text will be multiple words and have numbers. The IPs all start with 10.x.x.x.



      This is a script that I was looking into, but it tries to resolve the IP of the IP that I put into it. I guess it would work if I put the hostnames in there. Maybe I should keep that in there just in case.



      @echo off
      setlocal enabledelayedexpansion

      set OUTPUT_FILE=result.txt
      >nul copy nul %OUTPUT_FILE%
      for /f %%i in (testservers.txt) do (
      set SERVER_ADDRESS=ADDRESS N/A
      for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
      if %%x==Pinging set SERVER_ADDRESS=%%y
      if %%x==Reply set SERVER_ADDRESS=%%z
      if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
      )
      echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
      )


      What I really want is to have a text string like "This is the Server XYZ" to be concatenated at the end of line for the result.txt file.



      So my testservers.txt file will look like this:



      10.0.0.1 This is the Server ABC.
      10.0.0.2 This is the Server DEF.
      hostname1 This is the Server LMN.
      hostname2 This is the Server XYZ.


      When I run it now, it spits out results like this into the result.txt file.



      10.0.0.1 [10.0.0.1] is UP
      10.0.0.1 [10.0.0.2] is UP
      hostname1 [10.0.0.3] is UP
      hostname2 [10.0.0.4] is UP


      Then the result.txt file would look like this:



      10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
      10.0.0.2 [10.0.0.2] This is the Server DEF.
      hostname1 [10.0.0.3] This is the Server LMN.
      hostname2 [10.0.0.4] This is the Server XYZ.


      Hope I provided enough information. Let me know if I didn't.










      share|improve this question















      I want to ping a list of IPs with a string of text entered next to it. The text will be multiple words and have numbers. The IPs all start with 10.x.x.x.



      This is a script that I was looking into, but it tries to resolve the IP of the IP that I put into it. I guess it would work if I put the hostnames in there. Maybe I should keep that in there just in case.



      @echo off
      setlocal enabledelayedexpansion

      set OUTPUT_FILE=result.txt
      >nul copy nul %OUTPUT_FILE%
      for /f %%i in (testservers.txt) do (
      set SERVER_ADDRESS=ADDRESS N/A
      for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
      if %%x==Pinging set SERVER_ADDRESS=%%y
      if %%x==Reply set SERVER_ADDRESS=%%z
      if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
      )
      echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
      )


      What I really want is to have a text string like "This is the Server XYZ" to be concatenated at the end of line for the result.txt file.



      So my testservers.txt file will look like this:



      10.0.0.1 This is the Server ABC.
      10.0.0.2 This is the Server DEF.
      hostname1 This is the Server LMN.
      hostname2 This is the Server XYZ.


      When I run it now, it spits out results like this into the result.txt file.



      10.0.0.1 [10.0.0.1] is UP
      10.0.0.1 [10.0.0.2] is UP
      hostname1 [10.0.0.3] is UP
      hostname2 [10.0.0.4] is UP


      Then the result.txt file would look like this:



      10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
      10.0.0.2 [10.0.0.2] This is the Server DEF.
      hostname1 [10.0.0.3] This is the Server LMN.
      hostname2 [10.0.0.4] This is the Server XYZ.


      Hope I provided enough information. Let me know if I didn't.







      windows networking command-line batch-file ping






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 18:02









      Hennes

      58.8k792141




      58.8k792141










      asked Dec 28 '17 at 22:54









      rockower

      1157




      1157






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.



          This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.




          testservers.txt



          enter image description here






          Example Script



          @echo off
          setlocal enabledelayedexpansion

          set OUTPUT_FILE=result.txt
          >nul copy nul %OUTPUT_FILE%
          for /f "tokens=1,*" %%i in (testservers.txt) do (
          set SERVER_ADDRESS=ADDRESS N/A
          for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
          if %%x==Pinging set SERVER_ADDRESS=%%y
          if %%x==Reply set SERVER_ADDRESS=%%z
          if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
          )
          echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
          )




          Output Results



          10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
          10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
          hostname1 [<IP Address>] is UP This is the Server LMN.
          hostname2 [<IP Address>] is UP This is the Server XYZ.




          Further Resources




          • FOR /F



          • FOR /?




                tokens=x,y,m-n  - specifies which tokens from each line are to
            be passed to the for body for each iteration.
            This will cause additional variable names to
            be allocated. The m-n form is a range,
            specifying the mth through the nth tokens. If
            the last character in the tokens= string is an
            asterisk, then an additional variable is
            allocated and receives the remaining text on
            the line after the last token parsed.








          share|improve this answer



















          • 1




            Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
            – rockower
            Jan 3 at 18:37











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1280791%2fping-a-list-of-ips-with-strings-of-names-listed-in-a-txt-file-and-must-use-batch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.



          This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.




          testservers.txt



          enter image description here






          Example Script



          @echo off
          setlocal enabledelayedexpansion

          set OUTPUT_FILE=result.txt
          >nul copy nul %OUTPUT_FILE%
          for /f "tokens=1,*" %%i in (testservers.txt) do (
          set SERVER_ADDRESS=ADDRESS N/A
          for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
          if %%x==Pinging set SERVER_ADDRESS=%%y
          if %%x==Reply set SERVER_ADDRESS=%%z
          if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
          )
          echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
          )




          Output Results



          10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
          10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
          hostname1 [<IP Address>] is UP This is the Server LMN.
          hostname2 [<IP Address>] is UP This is the Server XYZ.




          Further Resources




          • FOR /F



          • FOR /?




                tokens=x,y,m-n  - specifies which tokens from each line are to
            be passed to the for body for each iteration.
            This will cause additional variable names to
            be allocated. The m-n form is a range,
            specifying the mth through the nth tokens. If
            the last character in the tokens= string is an
            asterisk, then an additional variable is
            allocated and receives the remaining text on
            the line after the last token parsed.








          share|improve this answer



















          • 1




            Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
            – rockower
            Jan 3 at 18:37
















          2














          Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.



          This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.




          testservers.txt



          enter image description here






          Example Script



          @echo off
          setlocal enabledelayedexpansion

          set OUTPUT_FILE=result.txt
          >nul copy nul %OUTPUT_FILE%
          for /f "tokens=1,*" %%i in (testservers.txt) do (
          set SERVER_ADDRESS=ADDRESS N/A
          for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
          if %%x==Pinging set SERVER_ADDRESS=%%y
          if %%x==Reply set SERVER_ADDRESS=%%z
          if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
          )
          echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
          )




          Output Results



          10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
          10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
          hostname1 [<IP Address>] is UP This is the Server LMN.
          hostname2 [<IP Address>] is UP This is the Server XYZ.




          Further Resources




          • FOR /F



          • FOR /?




                tokens=x,y,m-n  - specifies which tokens from each line are to
            be passed to the for body for each iteration.
            This will cause additional variable names to
            be allocated. The m-n form is a range,
            specifying the mth through the nth tokens. If
            the last character in the tokens= string is an
            asterisk, then an additional variable is
            allocated and receives the remaining text on
            the line after the last token parsed.








          share|improve this answer



















          • 1




            Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
            – rockower
            Jan 3 at 18:37














          2












          2








          2






          Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.



          This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.




          testservers.txt



          enter image description here






          Example Script



          @echo off
          setlocal enabledelayedexpansion

          set OUTPUT_FILE=result.txt
          >nul copy nul %OUTPUT_FILE%
          for /f "tokens=1,*" %%i in (testservers.txt) do (
          set SERVER_ADDRESS=ADDRESS N/A
          for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
          if %%x==Pinging set SERVER_ADDRESS=%%y
          if %%x==Reply set SERVER_ADDRESS=%%z
          if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
          )
          echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
          )




          Output Results



          10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
          10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
          hostname1 [<IP Address>] is UP This is the Server LMN.
          hostname2 [<IP Address>] is UP This is the Server XYZ.




          Further Resources




          • FOR /F



          • FOR /?




                tokens=x,y,m-n  - specifies which tokens from each line are to
            be passed to the for body for each iteration.
            This will cause additional variable names to
            be allocated. The m-n form is a range,
            specifying the mth through the nth tokens. If
            the last character in the tokens= string is an
            asterisk, then an additional variable is
            allocated and receives the remaining text on
            the line after the last token parsed.








          share|improve this answer














          Since you are using a FOR /F loop to read thru the text document content of testservers.txt then you can simply add "TOKENS=1,*" and the first token will be the the server name or IP address per each line and the next token will be the remaining portion of each line after that.



          This means you can then utilize the next token of the FOR /F loop to get the remaining portion of each line after the first token and append that to the ECHO line for the %OUTPUT_FILE%.




          testservers.txt



          enter image description here






          Example Script



          @echo off
          setlocal enabledelayedexpansion

          set OUTPUT_FILE=result.txt
          >nul copy nul %OUTPUT_FILE%
          for /f "tokens=1,*" %%i in (testservers.txt) do (
          set SERVER_ADDRESS=ADDRESS N/A
          for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
          if %%x==Pinging set SERVER_ADDRESS=%%y
          if %%x==Reply set SERVER_ADDRESS=%%z
          if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
          )
          echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
          )




          Output Results



          10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC. 
          10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
          hostname1 [<IP Address>] is UP This is the Server LMN.
          hostname2 [<IP Address>] is UP This is the Server XYZ.




          Further Resources




          • FOR /F



          • FOR /?




                tokens=x,y,m-n  - specifies which tokens from each line are to
            be passed to the for body for each iteration.
            This will cause additional variable names to
            be allocated. The m-n form is a range,
            specifying the mth through the nth tokens. If
            the last character in the tokens= string is an
            asterisk, then an additional variable is
            allocated and receives the remaining text on
            the line after the last token parsed.









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 16:37

























          answered Dec 31 '17 at 23:46









          Pimp Juice IT

          22.9k113869




          22.9k113869








          • 1




            Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
            – rockower
            Jan 3 at 18:37














          • 1




            Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
            – rockower
            Jan 3 at 18:37








          1




          1




          Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
          – rockower
          Jan 3 at 18:37




          Your accurate solution works with or without the message after the IP or Hostname.. Additionally, thank you for the reference to the FOR /F resource.
          – rockower
          Jan 3 at 18:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1280791%2fping-a-list-of-ips-with-strings-of-names-listed-in-a-txt-file-and-must-use-batch%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?