別の変数から最初の変数を抽出する


0

ホスト名のリストに対していくつかのping、nslookup、およびその他のコマンドを実行する.batスクリプトがあります。行のどこかで、リモートマシンの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のみ)と同じです


この質問は「テキストファイルに文字列のリストがあり、%FQDN%がそれらのいずれかに一致するかどうかをテストする必要があります」に要約できますか?
ロブ14


FOR / F Loopコマンド:別のコマンドの結果に対して、「usebackq」の使用方法を示します。投稿を編集してバッチファイルを含める場合、これを行う方法を確認できます。list.txt(必要に応じて適切に検閲済み)のコピーも含めてください
DavidPostill

@robいいえ、私が探しているものではないので、あなたが提案したようにそれが煮詰められるとは思わない。実際のスクリプトとホスト名のリストを追加しました。
うさんくさ14

@duDEはFINDSTRを調べましたが、メインスクリプトにどのように実装するかわかりませんでした。
魚のような14

回答:


0

これは動作するはずです

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

あなたは、ユーザーからそれを求めるのではなく、他のいくつかの変数に名前の値を設定するためにスクリプトを変更することができます
Dhiwakar Ravikumarを
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.