0

I have a .bat script that does some ping, nslookup and other commands on a list of hostnames. Somewhere down the line, I get a variable %FQDN%, which echoes out the fqdn of the remote machine, however i only need the hostname from the %FQDN% variable to compare it to the original hostname used.

I put a list of hostnames in a file called list.txt. (pc1, pc2, pc3) all on a new line. That hostname is then %1, which i need to check if it is = to the hostname in the %FQDN% variable?

any suggestions on how i would accomplish this?

I vaguely remember there was some command something about qback or something that could do this... but i don't know how it should be used.

The main script that does the ping, dnslookup and stuff looks like so: Many of the echos will be removed at a later stage, once i am happy that everything works as supposed to.

    @echo off
@cls

:: **************************************************
::
:: Just grabs the machine names from a list and then
:: calls another subroutine, passing the name to the
:: routine.
::
:: **************************************************

:getName

  for /f %%a in (list.txt) do call :doIt %%a

  goto end

:: **************************************************
::
:: The %1 is the %%a from the previous routine. In this
:: case you get the machine name. It is being set
:: as a variable for ease of use in the rest of the
:: script.
::
:: So now you copy the file out to the system and
:: and verify it is there. The IF statement defines
:: a variable to be used for logging and to determine
:: whether or not to waste time running PSEXEC against
:: a machine where the file failed to copy.
::
:: So now we say if the var strFil = "ok", go ahead
:: and run PSEXEC. If not, then go log what you have
:: so far.
::
:: I would include some kind of error checking after
:: running REGSVR32 to verify the file was registered
:: and then log that as well.
::
:: **************************************************

:doIt

  set strSvr=%1

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
cls
echo Ping command was successful
echo Now we are setting the IP and HostName variable

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 FQDN=%%a

set IPAddress=%Address::=%
cls
echo.
echo %1
echo %IPAddress%
echo %FQDN%
echo.
echo above is just to confirm that hostname,IP and FQDN is set
echo.
pause
cls
echo now we do a NSLOOKUP on the IPAddress collected from PING.
for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set nsNAME=%%a
echo.
pause
cls
echo now we confirm that original hostname = FQDN
echo using NSLOOKUP details from previous commands
echo.
pause
cls
echo nsname
echo %nsNAME%
echo.
echo var 1
echo %1
echo.
echo strSvr
echo %strSvr%
echo.
echo FQDN
echo %FQDN%
pause
cls

if "%1"=="%FQDN%" (
set hnstatus="HOSTNAME is GOOD fix will be run"
) else (
set hnstatus="HOSTNAME is BAD we cannot do anything"
)
echo %hnstatus%
echo.
echo Hostname status above = GOOD or bad
echo if bad, then hostname resolves to different IP.
echo.
cls
pause

::exit


echo %strSvr%
echo just checking if we still have a machine name as a variable.
pause
cls
echo.
echo Now we need to start the copy process and run wmifix remotely.
echo.
pause

:: if "%nsname%"=="%Hostname%" (
:: echo f | xcopy /f /Y "wmifix.bat" "\\%strSvr%\c$\Temp\fallout\wmifix.bat"
:: psexec \\%strSvr% c:\Temp\fallout\wmifix.bat
:: ) else (
:: echo Hostname is bad cannot do anything
:: set hnstatusbad="Hostname is bad cannot do anything"
:: )

  goto logIt

:: **************************************************
:: 
:: LOGS ARE IMPORTANT!!
:: Get in the habit of logging the results of your
:: scripts. Verify the important pieces so you know
:: what has been completed and what you have to chase
:: down.
:: 
:: **************************************************

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

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



:logIt

  echo.%strSvr%,%hnstatus%,%hnstatusbad%>>fallouts_log.csv
pause
:end

and then list.txt just contains hostnames like this:

DT048035
DT040676

Sample of %FQDN%

DT048035.za.lacer.net

i only want to check if first part before first . is the same as any of the other hostname variables %1 or %strSvr% (which is only DT048035)

  • can this question be boiled down to "I have a list of strings in a text file and I need to test if %FQDN% matches any of them"? – rob Sep 23 '14 at 13:28
  • Take a look at FINDSTR (ss64.com/nt/findstr.html) – duDE Sep 23 '14 at 13:35
  • FOR /F Loop command: against the results of another command shows how to use "usebackq". If you edit your post to include your batch file I can take a look at how you can do this. Please also include a copy of list.txt (suitably censored if necessary) – DavidPostill Sep 23 '14 at 14:36
  • @rob no, i do not think that it can be boiled down as you suggested, as that is not what i am looking for. I have added the actual script and list of hostnames. – Fishy Sep 29 '14 at 5:37
  • @duDE i took a look at FINDSTR, but could not figure out how i would implement it into my main script. – Fishy Sep 29 '14 at 5:38
0

This should work

**names.txt**
host1
host2
host3

**check.bat**
@echo off
setlocal EnableDelayedExpansion
set /p name=Enter the host name 
FINDSTR /L /C:%name% names.txt
IF %ERRORLEVEL% EQU 0 echo "found" ELSE echo "not found"
pause
  • You can modify the script to set the value of name to some other variable instead of prompting it from the user – Dhiwakar Ravikumar Sep 23 '14 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.