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 15881 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 . . .