-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathping_checker.sh
More file actions
49 lines (39 loc) · 1.13 KB
/
ping_checker.sh
File metadata and controls
49 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Check if input file is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <input_file> [output_file]"
echo "Example: $0 ips.txt ping_results.csv"
exit 1
fi
INPUT_FILE="$1"
OUTPUT_FILE="${2:-ping_results.csv}"
# Check if input file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: Input file '$INPUT_FILE' not found!"
exit 1
fi
# Create CSV header
echo "IP address,Pingable" > "$OUTPUT_FILE"
# Read each IP from the file
while IFS= read -r ip; do
# Skip empty lines and comments
if [[ -z "$ip" || "$ip" =~ ^# ]]; then
continue
fi
# Remove any whitespace
ip=$(echo "$ip" | xargs)
# Ping the IP with 1 packet (adjust for different OS)
# For Linux (uses -c and -W)
if ping -c 1 -W 2 "$ip" > /dev/null 2>&1; then
echo "$ip,Yes" >> "$OUTPUT_FILE"
else
echo "$ip,No" >> "$OUTPUT_FILE"
fi
# Alternative for macOS (uses -c and -t)
# if ping -c 1 -t 2 "$ip" > /dev/null 2>&1; then
# echo "$ip,Yes" >> "$OUTPUT_FILE"
# else
# echo "$ip,No" >> "$OUTPUT_FILE"
# fi
done < "$INPUT_FILE"
echo "Ping results saved to: $OUTPUT_FILE"