从另一个变量中提取第一个变量

从另一个变量中提取第一个变量

我有一个 .bat 脚本,它对主机名列表执行一些 ping、nslookup 和其他命令。在某个地方,我得到了一个变量 %FQDN%,它回显了远程机器的 fqdn,但我只需要 %FQDN% 变量中的主机名来将其与使用的原始主机名进行比较。

我将主机名列表放在一个名为 list.txt 的文件中。(pc1、pc2、pc3)全部放在新行上。该主机名为 %1,我需要检查它是否等于 %FQDN% 变量中的主机名?

关于如何实现这一目标,有什么建议吗?

我依稀记得有一些关于 qback 的命令或者其他可以做到这一点的东西……但我不知道应该如何使用它。

执行 ping、dnslookup 和其他操作的主要脚本如下所示:一旦我满意一切按预期进行,许多回声将在稍后阶段被删除。

    @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

然后 list.txt 只包含这样的主机名:

DT048035
DT040676

%FQDN% 示例

DT048035.za.lacer.net

我只想检查第一个 . 之前的第一部分是否与任何其他主机名变量 %1 或 %strSvr% 相同(仅为 DT048035)

答案1

这应该有效

**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

相关内容