-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightCons_cron.sh
executable file
·98 lines (77 loc) · 2.76 KB
/
lightCons_cron.sh
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
log_file="logs/lightCons_cron_log_$(date +%Y-%m-%d).log" # Set the log file name
maxIterations=20000 # Set the number of iterations
allThreads=(
"https://www.asite.com"
"https://icanhazip.com"
"https://cats.com"
"https://cat.com"
"https://fish.com"
"https://www.dog.com"
"https://api.ipify.org"
)
SUC=0;
FAL=0;
TOL=0;
ArrCnt=0;
totalDownloaded=0
currentIteration=0
start_time=$(date +%s)
total=$(expr ${#allThreads[@]} - 1)
# Create a log file and write the header information
{
echo "Log File Created: $(date)"
echo "Operating System: $(uname -s)"
echo "Hostname: $(hostname)"
echo "Local IP: $(hostname -I | awk '{print $1}')"
echo "Public IP: $(curl -s https://icanhazip.com)"
echo "----------------------------------------"
} > "$log_file" # Only write header to log file
# Temporary file to capture stdout
temp_output=$(mktemp)
# Function to handle cleanup
cleanup() {
# Calculate and log the total execution time
end_time=$(date +%s)
execution_time=$((end_time - start_time)) # Calculate elapsed time in seconds
echo "Total Execution Time: $execution_time seconds"
echo "Total Execution Time: $execution_time seconds" >> "$log_file" # Log the execution time
# Append the last 20 lines of the output to the log file
{
echo "----------------------------------------"
echo "Last 20 lines of output:"
tail -n 20 "$temp_output"
} >> "$log_file"
# Clean up the temporary output file
rm "$temp_output"
}
# Set trap to call cleanup on EXIT
trap cleanup EXIT
while (( currentIteration < maxIterations ));
do
# Use a temporary file to store the downloaded content
tempFile=$(mktemp)
if curl -x "127.0.0.1:443" --max-time 5 -s -w 'Request Code: %{http_code}\n' -o "$tempFile" ${allThreads[$ArrCnt]};
then
((SUC=SUC+1))
# Calculate the size of the downloaded file in bytes and convert to MB
downloadedSize=$(du -b "$tempFile" | cut -f1)
totalDownloaded=$((totalDownloaded + downloadedSize))
else
((FAL=FAL+1))
fi
# Convert total downloaded size to MB
totalDownloadedMB=$(echo "scale=2; $totalDownloaded/1024/1024" | bc)
((TOL=TOL+1))
# Log the output to the temporary file and display it in the terminal
{
echo "Total: $TOL"
echo "Success: $SUC"
echo "Fault: $FAL"
echo "Total Downloaded: ${totalDownloadedMB} MB" # Display total downloaded in MB
} | tee -a "$temp_output" # Append output to the temporary file and display in terminal
# Clean up the temporary file
rm "$tempFile"
((currentIteration++)) # Increment the current iteration count
done
cleanup()