Isso deve funcionar
**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
Eu tenho um script .bat que faz alguns ping, nslookup e outros comandos em uma lista de nomes de host. Em algum lugar abaixo da linha, eu obtenho uma variável% FQDN%, que ecoa o fqdn da máquina remota, no entanto eu só preciso do nome do host da variável% FQDN% para compará-la ao nome do host original usado.
Eu coloco uma lista de nomes de host em um arquivo chamado list.txt. (pc1, pc2, pc3) tudo em uma nova linha. Esse hostname é então% 1, que eu preciso verificar se é = para o nome do host na variável% FQDN%?
alguma sugestão de como eu faria isso?
Eu me lembro vagamente de que havia algum comando sobre o qback ou algo que poderia fazer isso ... mas não sei como ele deve ser usado.
O script principal que faz o ping, o dnslookup e o material é assim: Muitos dos ecos serão removidos em um estágio posterior, uma vez que estou feliz que tudo funcione como deveria.
@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
e, em seguida, list.txt apenas contém nomes de host como este:
DT048035
DT040676
Amostra de% FQDN%
DT048035.za.lacer.net
Eu só quero verificar se a primeira parte antes da primeira. é o mesmo que qualquer outra variável hostname% 1 ou% strSvr% (que é apenas DT048035)
Isso deve funcionar
**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
Tags command-line dns