2016-07-15 12 views
1

forループを使用してserver.txtファイルに記述されているすべてのサーバでcommand.txtで定義されたコマンドを読み込んで実行するバッチスクリプトがあります。バッチスクリプトで出力ファイル名として出力されるコマンドの取得方法

問題は、実行された各コマンドの出力/結果ファイルを "server_command.log"として取得しようとしています。

私は簡単にサーバーの詳細を取得することができますが、コマンドのために、私は、各コマンドは、特殊文字、すなわち、/

command.txtコンテンツを持っているのでcommand.txtファイルから最後の単語を取得する必要があります:

show /system1/firmware1 
show /system1/fan1 

SERVER.TXTは、ターゲットサーバーのIPアドレスを持っています:

10.1.1.101 
10.1.1.103 
10.1.1.105 

出力ファイルをする必要があります取得しようとしています:

101.1.1.101_firmware1.log (file will contain fireware1 command output) 
101.1.1.101_fan1.log (file will contain fan1 command output) 
101.1.1.103_firmware1.log (file will contain fireware1 command output) 
101.1.1.103_fan1.log (file will contain fan1 command output) 
101.1.1.105_firmware1.log (file will contain fireware1 command output) 
101.1.1.105_fan1.log (file will contain fan1 command output) 

私のバッチファイル:

for /f "tokens=1" %%a in (.\config\serverlist.txt) do (
    echo _ 
    echo ----- Opening connection to: %%a at %TIME% ----- >> %LOGFILE% 
    for /f "tokens=*" %%c in (.\config\commands.txt) do (
    set MyCmd=%%c 
    set Server=%%a 
    echo --------- %%c and %%a ------------------ 
    set MyLog=.\Logs\!Server!_%MyDate%.!MyCmd!.log 
     .\plink -ssh %username%@!Server! -pw %password% "!MyCmd!" >> !MyLog! 
) 
    echo. >> %LOGFILE% 
    echo ----- Closing connection to: %%a at !TIME! ----- >> %LOGFILE% 
) 

を感謝

答えて

0

はスペースであなたの特別な記号(/)を交換し、最後の要素を取得するには、単純なforを使用します。

... 
set MyCmd=%%c 
for %%X in (!MyCmd:/^= !) do set MyCmd=%%X 
echo !MyCmd! 
... 
関連する問題