Last active
September 19, 2024 04:24
-
-
Save jackbdu/75a5c5579fb02646496169fe0f8eb2a2 to your computer and use it in GitHub Desktop.
Initiating traceroute to a list of hosts specified in hosts.txt and logging the outputs to separate files for each host.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # ----------------------------------------------------------------------------- | |
| # Script: batch-traceroute.sh | |
| # Description: A script to run multiple traceroutes in the background, track their | |
| # completion status, and log the results with timestamps. | |
| # | |
| # Created by: Jack B. Du ([email protected]) | |
| # with help from OpenAI ChatGPT | |
| # Date: Sept 18, 2024 | |
| # ----------------------------------------------------------------------------- | |
| # Path to the file containing the list of hosts to traceroute | |
| hosts_file="hosts.txt" | |
| # Array to store the PIDs of the background traceroute processes | |
| pids=() | |
| # Initialize a counter for completed traceroutes | |
| completed=0 | |
| total=0 | |
| # Check if the file exists | |
| if [ ! -f "$hosts_file" ]; then | |
| echo "Error: File '$hosts_file' not found." | |
| exit 1 | |
| fi | |
| # Loop through each line in the file | |
| while IFS=, read -r host; do | |
| # Generate a timestamp in the format YYYY-MM-DD_HH-MM-SS | |
| timestamp=$(date +"%Y-%m-%d_%H-%M-%S") | |
| # Log file name with timestamp | |
| logfile="log_${host}_${timestamp}.txt" | |
| # Log the start of traceroute with a timestamp | |
| echo "[${timestamp}] Starting traceroute to $host" > "$logfile" | |
| # Start traceroute and log the output | |
| traceroute "$host" >> "$logfile" 2>&1 & | |
| # Capture the PID of the traceroute process | |
| pids+=($!) | |
| echo "Started traceroute to $host with PID ${pids[total]}" | |
| ((total++)) | |
| done < "$hosts_file" | |
| # Function to check the status of traceroutes | |
| check_status() { | |
| completed=0 # Reset the completed counter | |
| for pid in "${pids[@]}"; do | |
| if ! kill -0 "$pid" 2>/dev/null; then | |
| ((completed++)) | |
| fi | |
| done | |
| echo "Completed: $completed/$total traceroutes." | |
| } | |
| # Continuously check and show the status of traceroutes | |
| while [ "$completed" -lt "$total" ]; do | |
| check_status | |
| sleep 5 # Wait for 5 seconds before checking again | |
| done | |
| echo "All traceroutes are complete!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| github.com | |
| google.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🔥