| 1. |
Solve : Combination Ping, NSLOOKUP and echo from txt file script? |
|
Answer» 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. thank you for the reply. Looks perfect. 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. 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 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 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) Quote from: foxidrive on July 30, 2014, 02:10:27 AM Excellent! 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!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?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" 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?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"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.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 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 . . . |
|