Unable to get batch script to display IP address, Ping status, and hostname into a single 3 column .csv -
i'm trying ping against list of websites , following output in .csv, hostname, success of ping, ip address. output might this:
google.com, successful (or failure), 123.456.789.012 (if failure n/a)
i have 2 scripts can these things seperatley, can't seem them work together.
gets ip addresses:
@echo off set computerlist=c:\scripts\pinglist.txt echo computername,ip address>>pingresult.csv setlocal enabledelayedexpansion /f "usebackq tokens=*" %%a in ("%computerlist%") ( /f "tokens=3" %%b in ('ping -n 1 -l 1 %%a ^|findstr reply') ( set ipadd=%%b echo %%a,!ipadd:~0,-1!>>pingresult.csv ))* gets hostname , ping status:
rem set "pinglist" variable location of text file want ping against set pinglist=c:\scripts\pinglist.txt rem set "results" variable location want pipe output set results=c:\scripts\pingresult.csv echo computername, status >> %results% rem failed pings appened file /f "delims=" %%a in (%pinglist%) ping -n 1 %%a >nul && (echo %%a ok) || (echo %%a, failure ) >> %results% rem successful pings appened file /f "delims=" %%a in (%pinglist%) ping -n 1 %%a >nul && (echo %%a, success ) >> %results% || (echo %%a failed respond)
reformulated another answer; not optimized:
@echo off >nul @setlocal enableextensions echo computername, status, ip /f "usebackq delims=" %%i in ("files\30852528.txt") ( set "_found=" /f "delims=" %%g in (' ping -4 -n 1 %%i^|findstr /i /c:"ping request not find host"') ( echo %%i, "could not find host", "" set "_found=%%g" ) if not defined _found ( /f "tokens=3 delims=: " %%g in (' ping -4 -n 1 %%i^|findstr /i "ttl="') ( echo %%i, "ok", %%g set "_found=%%g" ) ) if not defined _found ( /f "tokens=2 delims=[]" %%g in (' ping -4 -n 1 %%i^|findstr /ib "pinging"') ( echo %%i, "request timed out", %%g set "_found=%%g" ) ) if not defined _found ( echo unknown ? %%i ) ) output
==>d:\bat\so\31525308.bat computername, status, ip foo.bar, "could not find host", "" google.com, "ok", 173.194.112.100 yahoo.com, "ok", 98.139.183.24 toyota.com, "ok", 95.101.1.91 bmw.com, "request timed out", 160.46.244.131 ==>
Comments
Post a Comment