Skip to content

Instantly share code, notes, and snippets.

@Opa-
Last active February 27, 2024 15:26
Show Gist options
  • Save Opa-/619d26afafd85484e23d7e6594028efe to your computer and use it in GitHub Desktop.
Save Opa-/619d26afafd85484e23d7e6594028efe to your computer and use it in GitHub Desktop.
endlessh winner table
#!/bin/bash
TOP=$(journalctl -u endlessh.service | grep "time=" | awk '{split($11, time, "="); split($8, ip, ":"); print ip[4], time[2]}' | awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}' | sort -n -k2)
format_duration() {
local seconds=$(printf "%.3f" $1) # Convert seconds to 3 decimal places
local milliseconds=$(echo "scale=0; $seconds * 1000" | bc) # Convert seconds to milliseconds
local milliseconds=${milliseconds%.*} # Remove decimal part
# Calculate days, hours, minutes, and seconds
local days=$((milliseconds / 86400000))
local hours=$(( (milliseconds % 86400000) / 3600000 ))
local minutes=$(( (milliseconds % 3600000) / 60000 ))
local seconds=$(( (milliseconds % 60000) / 1000 ))
# Using printf to format the duration and capturing the output in a variable
local formatted_duration=$(printf "%d days, %02d:%02d:%02d\n" $days $hours $minutes $seconds)
echo $formatted_duration
}
TABLE=""
TOTAL="0"
while IFS= read -r WINNER; do
IP=$(echo $WINNER | cut -d' ' -f1)
TIME=$(echo $WINNER | cut -d' ' -f2)
LOC=$(geoiplookup $IP | cut -d':' -f2 | tr "\n" " ")
TIME_FMT=$(format_duration $TIME)
TABLE="$TABLE;$IP;$LOC;$TIME_FMT\n"
TOTAL=$(echo "$TOTAL + TIME" | bc)
done <<< "$TOP"
echo -e $TABLE | tail -n 30 | column --table --separator ';'
format_duration $TOTAL
@Opa-
Copy link
Author

Opa- commented Feb 22, 2024

Output

  140.246.72.37     CN, China                0 days, 00:46:30
  85.209.11.254     RU, Russian Federation   0 days, 00:46:41
  118.185.157.226   IN, India                0 days, 00:50:51
  125.66.243.1      CN, China                0 days, 00:54:31
  218.56.155.106    CN, China                0 days, 01:17:42
  222.134.32.74     CN, China                0 days, 01:34:11
  212.70.149.150    BG, Bulgaria             0 days, 01:39:42
  45.71.24.198      BR, Brazil               0 days, 01:40:52
  45.167.92.168     MX, Mexico               0 days, 01:44:52
  36.151.192.25     CN, China                0 days, 01:53:02
  111.42.35.138     CN, China                0 days, 01:55:03
  154.127.86.66     NE, Niger                0 days, 01:55:22
  119.91.51.41      CN, China                0 days, 02:27:20
  190.107.30.120    CO, Colombia             0 days, 02:49:14
  61.153.208.38     CN, China                0 days, 02:54:26
  101.126.24.19     CN, China                0 days, 03:30:13
  159.203.19.56     CA, Canada               0 days, 03:39:43
  165.154.133.74    TW, Taiwan               0 days, 04:04:30
  87.120.84.106     US, United States        0 days, 04:43:07
  101.89.165.88     CN, China                0 days, 05:41:37
  116.8.108.115     CN, China                0 days, 06:13:25
  66.85.185.123     US, United States        0 days, 06:41:10
  175.178.234.104   CN, China                0 days, 07:46:40
  46.101.144.51     DE, Germany              0 days, 09:26:00
  103.163.215.12    VN, Vietnam              0 days, 10:43:30
  49.235.237.222    CN, China                0 days, 15:11:01
  172.232.97.153    US, United States        0 days, 21:29:40
  124.230.124.250   CN, China                0 days, 21:50:10
  180.101.88.220    CN, China                0 days, 22:01:30
202 days, 23:44:06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment