'#================================================================================ '#【機能】PING 結果に時間を追加して表示 '# '#【補足】 '# ・環境依存ですが、pingの標準出力結果の文字列に時間を追加して表示します '# '# ・起動引数(パラメータ)に次のものが利用可能です。 '#   /H:{ホスト名 または IPアドレス} '# '# ・cscript.exeで単独実行すると、ホスト名の入力画面から実行が可能です。 '#  但し、起動方法として[%windir%\system32\cmd.exe /C "…"]としなければ、 '#  ウィンドウタイトルが設定できないようです。(titleコマンドの影響範囲の為です) '# '#【制約】 '# ・ '# '#【改訂履歴】 '# 2010/05/15 : 新規 : '#================================================================================ Option Explicit ' 変数宣言 Dim host ' ホスト名 Dim objWShell ' Object(シェル) Dim objFileSys ' Object(ファイル) Dim objExec ' Object(シェル実行) Dim strLine '(作業変数)1行分の文字列 Dim ip '(作業変数)IPアドレス ' 初期設定 Set objWShell = CreateObject("WScript.Shell") Set objFileSys = CreateObject("Scripting.FileSystemObject") ' ホスト名を取得 host = WScript.Arguments.Named("H") '(未設定時の場合) If Trim(host) = "" Then ' Echo-画面表示フラグを設定(起動スクリプトより自動判別) IF InStr(LCase(WScript.FullName), "cscript.exe") > 0 Then '(cscript.exeで起動時) ' ホスト名を入力 host = InputBox("PING先(ホスト名 or IPアドレス)" & vbCrLf & "を設定してください", "PING監視") '(未設定時の場合) If Trim(host) = "" Then WScript.Quit(0) ' 終了 End If '#stop# '([.domain.test.xx]付与チェック …※高速化のため) host = Trim(host) '#stop# If InStr(host, ".") = 0 Then '#stop# host = host & ".domain.test.xx" '#stop# End If ' ホスト名からIPアドレスを取得 ip = getAddressByHost(host) If ip <> "" Then '(通常時) ' ウィンドウのタイトルを設定 objWShell.Exec("cmd.exe /K title 疎通確認 [" & ip & "] " & host) Else '(エラー時) objWShell.Exec("cmd.exe /K title 疎通確認 " & host & " …※IP-NG") End If End If End If '(未設定時の場合) If Trim(host) = "" Then MsgBox "cscript.exe で実行してください。" WScript.Quit(0) ' 終了 End If ' pingを実行 Wscript.Echo "===== " & Now & " =====" Set objExec = objWShell.Exec("ping -t " + host) Do Until objExec.StdOut.AtEndOfStream '(Loop) 標準出力が終了するまで strLine = objExec.StdOut.ReadLine ' 1行読み込み If strLine <> "" Then Wscript.Echo FormatDateTime(Now, 3) & " - " & strLine ' 時刻+そのまま表示 End If Loop objExec.Terminate '(pingを強制終了) Set objExec = Nothing ' 後始末 Set objWShell = Nothing ' 終了 WScript.Quit(0) '#================================================================================ '#【機能】ホスト名からIPアドレスを取得 '# '#【引数】 '#  host ホスト名(IPアドレスでもOK) '# '#【戻値】 '#  =IPアドレス(例:"192.168.0.111") '#  ="" (取得失敗時) '# '#【補足】 '# ・ホスト名は例えば[www123]ではなく[www123.domain.test.xx]とする方が早く動作します '# '#【改訂履歴】 '# 2012/02/22 : 新規 : '#================================================================================ Function getAddressByHost(host) ' 変数宣言 Dim objWMIService ' サービス情報 Dim colItems ' (作業変数) 項目 Dim objItem ' (作業変数) 項目 ' ホスト名からIPアドレスを取得 getAddressByHost = "" '(初期値:未存在) Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus Where Address = '" & host & "'") For Each objItem In colItems '#WScript.Echo "Address: " & objItem.Address '#WScript.Echo "ProtocolAddress: " & objItem.ProtocolAddress getAddressByHost = objItem.ProtocolAddress Exit For '(1件のみの筈) Next ' 後始末 Set objItem = Nothing Set colItems = Nothing Set objWMIService = Nothing End Function