Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Combination Ping, NSLOOKUP and echo from txt file script  (Read 15882 times)

0 Members and 1 Guest are viewing this topic.

MadFly

    Topic Starter


    Rookie
    • Experience: Familiar
    • OS: Windows 7
    Combination Ping, NSLOOKUP and echo from txt file script
    « on: July 28, 2014, 01:33:08 AM »
    I am trying to ping a list of machines, do an nslookup on the ip addresses, then if hostname is same as original hostname, read from another txt file to output possible location of hostname.

    this is my hh.bat script:
    Code: [Select]
    @echo Off
    @cls
    if '%1'=='' GOTO Syntax
    echo Running Script and Saving Results to Results.CSV
    echo Script Run %date% %time% >> Results.csv
    for /F %%i in (%1) do Call :StartPing %%i
    goto :EOF

    :StartPing
    PING %1 -n 1| FIND /i "TTL" > nul && goto Success
    PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
    PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg

    :Success
    for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
    for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

    set IPAddress=%Address::=%
    nslookup %IPAddress% | find /i "Name" do set nsNAME=


    echo %1, %IPAddress%,%Hostname%
    echo %1, %IPAddress%,%Hostname% >> Results.csv
    goto :EOF

    :Timedout
    Echo %1, Request timed out.
    Echo %1, Request timed out. >> Results.csv

    :ErrorMsg
    Echo %1, Ping request could not find host.
    Echo %1, Ping request could not find host. >> Results.csv
    goto :EOF

    :Syntax
    echo . . .
    goto :EOF

    :EOF
    echo this is the END OF FILE
    pause

    and this is my host.txt file
    Code: [Select]
    Computer1

    and this is the list of locations - home.txt
    Code: [Select]
    ipaddress - 2G
    either ip or hostname... i don't know
    hostname - 2D

    i am struggeling to do the NSLookup part, and have no idea how to read from the home.txt once nslookup is successfull and echo on the location line.

    any suggestions or ideas?

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Combination Ping, NSLOOKUP and echo from txt file script
    « Reply #1 on: July 28, 2014, 08:04:29 AM »
    These snippets may help you (untested):

    Code: [Select]
    for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set "nsNAME=%%a"
    echo "%nsname%"
    pause

    Code: [Select]
    for /f "usebackq delims=" %%a in ("home.txt") do (
       echo "%%a"
    )
    pause

    MadFly

      Topic Starter


      Rookie
      • Experience: Familiar
      • OS: Windows 7
      Re: Combination Ping, NSLOOKUP and echo from txt file script
      « Reply #2 on: July 28, 2014, 11:23:46 PM »
      thank you for the reply.  Looks perfect.
      however, upon testing... the second the script hits the NSLOOKUP part, it creates 1700+ processes, doing the
      Code: [Select]
      nslookup %IPAddress% ^| find /i "Name: " part.
      I am running this script hh.bat host.bat

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Combination Ping, NSLOOKUP and echo from txt file script
      « Reply #3 on: July 29, 2014, 05:36:20 AM »
      thank you for the reply.  Looks perfect.
      however, upon testing... the second the script hits the NSLOOKUP part, it creates 1700+ processes, doing the
      Code: [Select]
      nslookup %IPAddress% ^| find /i "Name: " part.
      I am running this script hh.bat host.bat

      If you are getting 1700 processes running then you have a loop that is neverending - quite possibly because you called your batch file nslookup or you have another problem in your code.  Post it here as you have changed it for people to look at.




      MadFly

        Topic Starter


        Rookie
        • Experience: Familiar
        • OS: Windows 7
        Re: Combination Ping, NSLOOKUP and echo from txt file script
        « Reply #4 on: July 29, 2014, 10:48:39 PM »
        File is called hh.bat
        and i run it in cmd like hh.bat host.bat.
        everything works fine without the nslookup part. once nslookup lines are active and uncommented and the script hits it, processes goes up from 390, 1700 or even 3500 sometimes.

        Code: [Select]
        @echo off

        if '%1'=='' GOTO Syntax
        echo Running Script and Saving Results to Results.CSV
        echo.
        echo Script Run %date% %time% >> Results.csv
        for /F %%i in (%1) do Call :StartPing %%i
        goto :EOF

        :StartPing
        PING %1 -n 1| FIND /i "TTL" > nul && goto Success
        PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
        PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg

        :Success
        for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
        for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

        set IPAddress=%Address::=%
        echo %1, %IPAddress%,%Hostname%
        pause
        for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set "nsNAME=%%a"
        echo "%nsname%"
        pause

        :: Get Location of machine
        cls
        :: replace tokens=3 with delims= to get the whole line
        for /f "usebackq tokens=3" %%a in ("home.txt") do (
           echo "%%a"
        )
        pause
        echo %1, %IPAddress%,%Hostname% >> Results.csv
        goto :EOF

        :Timedout
        Echo %1, Request timed out.
        Echo %1, Request timed out. >> Results.csv

        :ErrorMsg
        Echo %1, Ping request could not find host.
        Echo %1, Ping request could not find host. >> Results.csv
        goto :EOF

        :Syntax
        echo . . .
        goto :EOF

        :EOF
        echo this is the END OF FILE



        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Combination Ping, NSLOOKUP and echo from txt file script
        « Reply #5 on: July 30, 2014, 02:10:27 AM »
        There is a system command called hh so pick another name, but that isn't the issue.

        Do you have a set of computer names in host.bat or IP addresses? 
        When using IP addresses it works, but using computer names it fails because the ping command doesn't include "TTL=" even though it is successful - which is a new behaviour on me.  Tested with the localhost in Windows 8.1 and it uses IPV6 in the ping screen display.

        Your code is missing a goto :EOF here and there and FWIW the :EOF label is internal to CMD and isn't needed in the batch script.


        Try this code - using a simple home.txt file with

        Code: [Select]
        a b reading
        c d file

        and in your hosts.bat just include a few computer names, and try it with a few IP addresses as a test too.
        Show us what it displays on the console if it still misbehaves.

        Make sure there are no ping.bat or nslookup.bat in the current directory or on the path.  I didn't notice a problem with extra processes.

        Code: [Select]
        @echo off

        if "%~1"=="" GOTO Syntax
        echo Running Script and Saving Results to Results.CSV
        echo.
        echo Script Run %date% %time% >> Results.csv
        for /F %%i in (%1) do Call :StartPing %%i
        goto :EOF

        :StartPing
        PING %1 -n 1| FIND /i "TTL" > nul && goto Success
        PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
        PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg
        goto :EOF

        :Success
        for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
        for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

        set IPAddress=%Address::=%
        echo %1, %IPAddress%,%Hostname%

        for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set "nsNAME=%%a"
        echo "%nsname%"


        :: Get Location of machine
        :: cls
        :: replace tokens=3 with delims= to get the whole line
        for /f "usebackq tokens=3" %%a in ("home.txt") do (
           echo "%%a"
        )

        echo %1, %IPAddress%,%Hostname% >> Results.csv
        goto :EOF

        :Timedout
        Echo %1, Request timed out.
        Echo %1, Request timed out. >> Results.csv
        goto :EOF

        :ErrorMsg
        Echo %1, Ping request could not find host.
        Echo %1, Ping request could not find host. >> Results.csv
        goto :EOF

        :Syntax
        echo . . .
        goto :EOF

        MadFly

          Topic Starter


          Rookie
          • Experience: Familiar
          • OS: Windows 7
          Re: Combination Ping, NSLOOKUP and echo from txt file script
          « Reply #6 on: July 30, 2014, 09:04:49 AM »
          Interesting. Thank you for the feedback and updates.
          will test in the morning once i am back at the office again.

          i'll rename the main hh script to something very random.
          and apologies, there isn't any host.bat, it should have been host.txt...

          hh.bat -> to be renamed to qwe.bat = contains main script
          host.txt -> just a list of computer names
          home.txt -> ip addresses - office location (eg. 172.16.1.55 - Reception)


          Try this code - using a simple home.txt file with

          Code: [Select]
          a b reading
          c d file

          MadFly

            Topic Starter


            Rookie
            • Experience: Familiar
            • OS: Windows 7
            Re: Combination Ping, NSLOOKUP and echo from txt file script
            « Reply #7 on: July 31, 2014, 07:34:57 AM »
            Excellent!  8)

            script is working like a charm.
            can now even put a if state to tell me if hostname is bad or good, as well as location.

            Just excellent stuff!

            Thanks a million!

            MadFly

              Topic Starter


              Rookie
              • Experience: Familiar
              • OS: Windows 7
              Re: Combination Ping, NSLOOKUP and echo from txt file script
              « Reply #8 on: August 01, 2014, 09:00:10 AM »
              one other issue I have picked up is when the script reaches the get location part...

              Code: [Select]
              for /f "usebackq tokens=3" %%a in ("home.txt") do (
                 echo Your machine is located at "%%a"
              )
              cls
              echo "%%a"

              it echos out every line in the home.txt, instead of only looking for the specified IP address in home.txt and echo only the location

              home.txt looks like this...

              Code: [Select]
              172.16.4.15 - Reception
              172.16.5.155 - Server Room

              It should only echo either the entire line where it finds the IP or only the Reception or Server Room part.
              any suggestions on this?

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: Combination Ping, NSLOOKUP and echo from txt file script
              « Reply #9 on: August 01, 2014, 09:05:10 AM »
              Instead of this:

              Code: [Select]
              :: Get Location of machine
              :: cls
              :: replace tokens=3 with delims= to get the whole line
              for /f "usebackq tokens=3" %%a in ("home.txt") do (
                 echo "%%a"
              )


              Try this: (untested)

              Code: [Select]
              :: Get Location of machine
              :: cls
              findstr /b/e  "%~1" "home.txt"


              MadFly

                Topic Starter


                Rookie
                • Experience: Familiar
                • OS: Windows 7
                Re: Combination Ping, NSLOOKUP and echo from txt file script
                « Reply #10 on: August 04, 2014, 03:16:15 AM »
                Thank you for the reply.
                i have added your suggested code as suggested, but how do i echo from that findstr command?
                or how can i put the result of findstr in some variable?

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: Combination Ping, NSLOOKUP and echo from txt file script
                « Reply #11 on: August 04, 2014, 09:27:51 AM »
                It already echos it to the console, and you can get the information into a log file like this, if that is what you want to do.

                Code: [Select]
                findstr /b/e  "%~1" "home.txt" >>"file.log"

                MadFly

                  Topic Starter


                  Rookie
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: Combination Ping, NSLOOKUP and echo from txt file script
                  « Reply #12 on: August 05, 2014, 10:01:34 AM »
                  when i put it just as is then my command line tells me
                  FINDSTR: // ignored

                  that could be because i have 2 hostnames in host.txt.
                  however, when removing 1 hostname from host.txt it does not echo anything out.

                  foxidrive



                    Specialist
                  • Thanked: 268
                  • Experience: Experienced
                  • OS: Windows 8
                  Re: Combination Ping, NSLOOKUP and echo from txt file script
                  « Reply #13 on: August 05, 2014, 09:24:45 PM »
                  Test it with the extra lines below and tell me what was echoed when the error appears on the screen.

                  It seems like the %~1 term has slashes in it.

                  Code: [Select]
                  echo "%~1"
                  findstr /b/e  "%~1" "home.txt" >>"file.log"
                  pause

                  MadFly

                    Topic Starter


                    Rookie
                    • Experience: Familiar
                    • OS: Windows 7
                    Re: Combination Ping, NSLOOKUP and echo from txt file script
                    « Reply #14 on: August 06, 2014, 01:40:03 AM »
                    it only echos out the hostname found in host.txt

                    Code: [Select]
                    Computer1, 172.16.4.115,computer1.localdomain.net
                    HOSTNAME is GOOD
                    "Computer1"
                    FINDSTR: // ignored
                    Press any key to continue . . .

                    foxidrive



                      Specialist
                    • Thanked: 268
                    • Experience: Experienced
                    • OS: Windows 8
                    Re: Combination Ping, NSLOOKUP and echo from txt file script
                    « Reply #15 on: August 06, 2014, 06:16:58 AM »
                    Use this instead.

                    Code: [Select]
                    findstr /i /c:"%~1" "home.txt" >>"file.log"

                    MadFly

                      Topic Starter


                      Rookie
                      • Experience: Familiar
                      • OS: Windows 7
                      Re: Combination Ping, NSLOOKUP and echo from txt file script
                      « Reply #16 on: August 06, 2014, 11:40:35 PM »
                      for some reason that still returns nothing and file.log is empty.
                      the closest to working that i had so far was...

                      Code: [Select]
                      :: Get Location of machine
                      :: replace tokens=3 with delims= to get the whole line
                      for /f "usebackq tokens=3" %%a in ("home.txt") do (
                         echo "%%a"
                      )

                      however, this echos out everything in home.txt. and not only the location of specified IP or ONLY the line where the IP address is found.

                      foxidrive



                        Specialist
                      • Thanked: 268
                      • Experience: Experienced
                      • OS: Windows 8
                      Re: Combination Ping, NSLOOKUP and echo from txt file script
                      « Reply #17 on: August 07, 2014, 07:51:39 AM »
                      Only you know what is in the home.txt file and in the %~1 term. 

                      Run this yourself to prove that it works.

                      Code: [Select]
                      @echo off
                      echo Computer1 - basement>home.txt
                      echo Computer2 - treehouse>>home.txt
                      findstr /i /c:"Computer1" "home.txt" >>"file.log"

                      MadFly

                        Topic Starter


                        Rookie
                        • Experience: Familiar
                        • OS: Windows 7
                        Re: Combination Ping, NSLOOKUP and echo from txt file script
                        « Reply #18 on: August 07, 2014, 10:22:30 AM »
                        ok, don't know what i did wrong or where it went wrong...
                        commented every step, echo'd what is happening.
                        cls the screen after each step and explaining the next step...

                        now it works like a charm again, and tells me only the IP and location of the hostname i originally searched for.

                        is there and way to only show the treehouse part after the Computer1 -

                        i have removed the >> file.log and now it echos out onscreen :)

                        foxidrive



                          Specialist
                        • Thanked: 268
                        • Experience: Experienced
                        • OS: Windows 8
                        Re: Combination Ping, NSLOOKUP and echo from txt file script
                        « Reply #19 on: August 07, 2014, 10:42:36 AM »
                        Given this home.txt

                        Code: [Select]
                        172.16.4.15 - Reception
                        172.16.5.155 - Server Room

                        then this should work.

                        Code: [Select]
                        for /f "tokens=1,* delims=- " %%a in ('findstr /i /c:"%~1" "home.txt" ') do echo %%b

                        As mentioned in an earlier post, if your network is IPV6 capable then the ping test can fail and skip servers.
                        « Last Edit: August 07, 2014, 11:02:23 AM by foxidrive »

                        MadFly

                          Topic Starter


                          Rookie
                          • Experience: Familiar
                          • OS: Windows 7
                          Re: Combination Ping, NSLOOKUP and echo from txt file script
                          « Reply #20 on: August 07, 2014, 10:56:55 AM »
                          nah its fine. its really fine if it echos out the ip as well as the - server room part from home.txt
                          when changed to your latest suggestion it again echos out nothing.

                          i believe very strongly that the network is not ipv6 enabled. however, ipv6 dhcp seems to be working.
                          but everything and all systems still work on the ipv4 addresses.

                          thanks again.

                          i am happy with
                          Code: [Select]
                          findstr /i /c:"%IPAddress%" "home.txt"

                          foxidrive



                            Specialist
                          • Thanked: 268
                          • Experience: Experienced
                          • OS: Windows 8
                          Re: Combination Ping, NSLOOKUP and echo from txt file script
                          « Reply #21 on: August 07, 2014, 11:04:28 AM »
                          Use this as home.txt which is what you said the format of the file was:

                          Code: [Select]
                          172.16.4.15 - Reception
                          172.16.5.155 - Server Room

                          and run this as: myfile.bat 172.16.4.15 to prove to yourself that it works.  I did change the last %%a to %%b

                          Code: [Select]
                          @echo off
                          for /f "tokens=1,* delims=- " %%a in ('findstr /i /c:"%~1" "home.txt" ') do echo %%b
                          pause


                          MadFly

                            Topic Starter


                            Rookie
                            • Experience: Familiar
                            • OS: Windows 7
                            Re: Combination Ping, NSLOOKUP and echo from txt file script
                            « Reply #22 on: August 07, 2014, 12:09:06 PM »
                            if i could...
                            i would give you a medal  8)
                            Working as hoped it should work.

                            just had to add the
                            Code: [Select]
                            %IPAddress% variable in the findstr line to allow it to follow the rest of the script.
                            Code: [Select]
                            for /f "tokens=1,* delims=- " %%a in ('findstr /i /c:"%IPAddress%" "home.txt" ') do echo %%bthank you.