Certainly! Below is a comprehensive Windows Batch script that performs the following tasks:
- Reads IP addresses from a file named
ip.txt (one IP per line).
- Pings each IP to check if it's online.
- If the IP is online, executes a
sqlcmd command with a specified timeout.
- Logs all results to a
results.log file.
Prerequisites:
-
SQLCMD Utility: Ensure that the sqlcmd utility is installed on your system and added to your system's PATH. It's typically included with SQL Server installations or can be installed separately via the Microsoft Command Line Utilities for SQL Server.
-
ip.txt File: Create a file named ip.txt in the same directory as the script, containing one IP address per line. For example:
192.168.1.10
192.168.1.20
192.168.1.30
-
Customize SQLCMD Parameters: Update the SQL Server connection details (SERVER, USER, PASSWORD, DATABASE, and QUERY) in the script as per your environment.
The Batch Script: check_ips.bat
@echo off
setlocal enabledelayedexpansion
:: ============================
:: Configuration Section
:: ============================
:: Path to the file containing IP addresses
set "IPFILE=ip.txt"
:: Path to the log file
set "LOGFILE=results.log"
:: SQLCMD Configuration
set "SQLCMD_SERVER=YOUR_SQL_SERVER" :: Replace with your SQL Server name or IP
set "SQLCMD_USER=YOUR_USERNAME" :: Replace with your SQL username
set "SQLCMD_PASSWORD=YOUR_PASSWORD" :: Replace with your SQL password
set "SQLCMD_DATABASE=YOUR_DATABASE" :: Replace with your target database
set "SQLCMD_QUERY=YOUR_QUERY_HERE" :: Replace with your SQL query, e.g., "SELECT 1"
:: Timeout settings
set "PING_TIMEOUT=1000" :: Ping timeout in milliseconds
set "SQLCMD_TIMEOUT=30" :: SQLCMD query timeout in seconds
:: ============================
:: End of Configuration
:: ============================
:: Initialize the log file
echo =============================== >> "%LOGFILE%"
echo Log started at %date% %time% >> "%LOGFILE%"
echo =============================== >> "%LOGFILE%"
:: Check if IP file exists
if not exist "%IPFILE%" (
echo [%date% %time%] ERROR: IP file "%IPFILE%" not found.
echo [%date% %time%] ERROR: IP file "%IPFILE%" not found. >> "%LOGFILE%"
exit /b 1
)
:: Loop through each IP in the IP file
for /f "usebackq tokens=*" %%A in ("%IPFILE%") do (
set "IP=%%A"
echo [%%A] Pinging...
echo [%%A] Pinging... >> "%LOGFILE%"
:: Ping the IP address
ping -n 1 -w %PING_TIMEOUT% %%A >nul
if !errorlevel! equ 0 (
echo [%%A] Online. Executing SQLCMD...
echo [%%A] Online. Executing SQLCMD... >> "%LOGFILE%"
:: Execute SQLCMD with timeout
(
echo [%%A] [%date% %time%] SQLCMD started.
sqlcmd -S %SQLCMD_SERVER% -U %SQLCMD_USER% -P %SQLCMD_PASSWORD% -d %SQLCMD_DATABASE% -Q "%SQLCMD_QUERY%" -t %SQLCMD_TIMEOUT%
if !errorlevel! neq 0 (
echo [%%A] [%date% %time%] SQLCMD failed or timed out.
) else (
echo [%%A] [%date% %time%] SQLCMD executed successfully.
)
echo [%%A] [%date% %time%] SQLCMD ended.
) >> "%LOGFILE%" 2>&1
) else (
echo [%%A] Offline.
echo [%%A] [%date% %time%] Offline. >> "%LOGFILE%"
)
)
:: Final log entry
echo =============================== >> "%LOGFILE%"
echo Log ended at %date% %time% >> "%LOGFILE%"
echo =============================== >> "%LOGFILE%"
echo All IPs processed. Check "%LOGFILE%" for details.
endlocal
exit /b 0
Explanation of the Script:
-
Configuration Section:
- IPFILE: Path to your
ip.txt file containing IP addresses.
- LOGFILE: Path to your log file where results will be stored.
- SQLCMD Configuration: Set your SQL Server connection details and the query you want to execute.
- Timeout Settings:
PING_TIMEOUT: Maximum wait time for each ping in milliseconds.
SQLCMD_TIMEOUT: Maximum time to wait for the SQL query to execute in seconds.
-
Log Initialization:
- The script starts by appending a header with the current date and time to the log file.
-
IP File Check:
- Before proceeding, the script checks if the
ip.txt file exists. If not, it logs an error and exits.
-
Processing Each IP Address:
- The
for /f loop reads each IP address from ip.txt.
- Ping Check:
- Pings the IP address once with the specified timeout.
- If the ping is successful (
errorlevel is 0), it proceeds to execute sqlcmd.
- If the ping fails, it logs that the IP is offline.
- Executing SQLCMD:
- Logs the start of the
sqlcmd execution with a timestamp.
- Runs the
sqlcmd command with the specified parameters.
- Checks the
errorlevel to determine if sqlcmd executed successfully or failed/timed out.
- Logs the result accordingly.
- All output and errors from
sqlcmd are appended to the log file.
-
Final Log Entry:
- After processing all IPs, the script appends a footer with the end time to the log file.
- Notifies the user that all IPs have been processed and directs them to check the log file for details.
Customization:
-
SQL Server Details: Replace the placeholder values (YOUR_SQL_SERVER, YOUR_USERNAME, YOUR_PASSWORD, YOUR_DATABASE, YOUR_QUERY_HERE) with your actual SQL Server information and the query you wish to run.
set "SQLCMD_SERVER=192.168.1.100"
set "SQLCMD_USER=sa"
set "SQLCMD_PASSWORD=YourPassword"
set "SQLCMD_DATABASE=master"
set "SQLCMD_QUERY=SELECT GETDATE();"
-
Adding More SQLCMD Options: You can customize the sqlcmd command further by adding more options as needed. Refer to the sqlcmd documentation for additional parameters.
-
Enhancing Logging: If you require more detailed logging (e.g., capturing specific output from sqlcmd), you can modify the script to include additional information.
Running the Script:
-
Save the Script:
- Save the above script to a file named
check_ips.bat.
-
Prepare ip.txt:
- Ensure that
ip.txt is in the same directory as check_ips.bat and contains the list of IP addresses you want to check.
-
Execute the Script:
-
Review the Log:
- After execution, open
results.log to review the outcomes of the ping and sqlcmd operations for each IP address.
Sample results.log Output:
===============================
Log started at 04/27/2024 10:00:00.00 AM
===============================
[192.168.1.10] Pinging...
[192.168.1.10] Online. Executing SQLCMD...
[192.168.1.10] [04/27/2024 10:00:01.23 AM] SQLCMD started.
[192.168.1.10] SELECT GETDATE();
[192.168.1.10] -----------
[192.168.1.10] 2024-04-27 10:00:01.23
[192.168.1.10] [%date% %time%] SQLCMD executed successfully.
[192.168.1.10] [04/27/2024 10:00:01.23 AM] SQLCMD ended.
[192.168.1.20] Pinging...
[192.168.1.20] Offline.
[192.168.1.20] [04/27/2024 10:00:02.34 AM] Offline.
===============================
Log ended at 04/27/2024 10:00:03.45 AM
===============================
All IPs processed. Check "results.log" for details.
Additional Enhancements:
-
Parallel Execution: For large lists of IPs, consider enhancing the script to run checks in parallel to save time. This can be achieved using PowerShell or other scripting languages, as batch scripting has limited support for parallelism.
-
Error Handling: Implement more robust error handling to capture and respond to different types of failures (e.g., network issues, authentication problems).
-
Notifications: Integrate email or other notification mechanisms to alert you of specific events (e.g., all IPs down, sqlcmd failures).
Conclusion
This batch script provides a straightforward way to automate the process of checking the availability of multiple IP addresses, executing SQL commands on reachable hosts, and logging the outcomes for review. Customize the script as needed to fit your specific requirements and environment.
If you have any further questions or need additional features, feel free to ask!