BashFu to show count of visitors today - M0YNG.uk
Created 2021-06-10
Modified 2021-07-13
- Linux
'cos I can't document this in just one toot...
#!/bin/bash # Tell the server hostname and today's date echo "Visitors to $(hostname) on $(date +%d/%b/%Y)" # Search nginx log file for today's date and slice it up to get just IP addresses, then sort them, then get just the unique ones, then count them HTTPV=$(grep "\[`date +%d/%b/%Y`" /var/log/nginx/access.log | cut -d" " -f1 | sort | uniq | wc -l) # same for gopher GOPHERV=$(grep "\[`date +%d/%b/%Y`" /var/log/gopher | cut -d" " -f1 | sort | uniq | wc -l) # same for gemini GEMINIV=$(grep "`date +'%b %d'`" /var/log/gemini.log | cut -d" " -f11 | sort | uniq | wc -l) # Tell the counts echo "HTTP Gopher Gemini" echo "$HTTPV $GOPHERV $GEMINIV"
Gives an output like this
Visitors to server.m0yng.uk on 10/Jun/2021 HTTP Gopher Gemini 43 2 6
A slightly more advanced version, this tries to get the data for yesterday too (although consistently fails for me with nginx probably due to logrotate), looks for stuff that says it is a bot, and tabulates the output for consistent output.
#!/bin/bash # Tell the server hostname and today's date echo "Visitors to $(hostname) on $(date +%d/%b/%Y)" # Search nginx log file for today's date and slice it up to get just IP addresses, then sort them, then get just the unique ones, then count them HTTPV=$(grep "\[`date +%d/%b/%Y`" /var/log/nginx/access.log | cut -d" " -f1 | sort | uniq | wc -l) # What about yesterday? HTTPY=$(grep "\[`date --date='yesterday' +%d/%b/%Y`" /var/log/nginx/access.log | cut -d" " -f1 | sort | uniq | wc -l) # Count bots and crawlers HTTPVBOT=$(grep "\[`date +%d/%b/%Y`" /var/log/nginx/access.log | grep -iE 'bot|crawler' | cut -d" " -f1 | sort | uniq | wc -l) HTTPYBOT=$(grep "\[`date --date='yesterday' +%d/%b/%Y`" /var/log/nginx/access.log | grep -iE 'bot|crawler' | cut -d" " -f1 | sort | uniq | wc -l) # same for gopher (but not the bots, are there bots on gopher?) GOPHERV=$(grep "\[`date +%d/%b/%Y`" /var/log/gopher | cut -d" " -f1 | sort | uniq | wc -l) GOPHERY=$(grep "\[`date --date='yesterday' +%d/%b/%Y`" /var/log/gopher | cut -d" " -f1 | sort | uniq | wc -l) # same for gemini GEMINIV=$(grep "`date +'%b %d'`" /var/log/gemini.log | cut -d" " -f11 | sort | uniq | wc -l) GEMINIY=$(grep "`date --date='yesterday' +'%b %d'`" /var/log/gemini.log | cut -d" " -f11 | sort | uniq | wc -l) GEMINIVBOT=$(grep "`date +'%b %d'`" /var/log/gemini.log | grep -iE 'bot|crawler' | cut -d" " -f11 | sort | uniq | wc -l) GEMINIYBOT=$(grep "`date --date='yesterday' +'%b %d'`" /var/log/gemini.log | grep -iE 'bot|crawler' | cut -d" " -f11 | sort | uniq | wc -l) # Tell the counts echo -e "\e[1;33m" # make it yellow printf "%-7s%-7s%-7s%-7s%-7s\n" "HTTP" "Bot?" "Gopher" "Gemini" "Bot?" # %-7s = left aligned 7 characters wide string. echo -e "\e[0m-----------------------------------" # stop being yellow printf "%-7s%-7s%-7s%-7s%-7s\n" $HTTPV $HTTPVBOT $GOPHERV $GEMINIV $GEMINIVBOT printf "%-7s%-7s%-7s%-7s%-7s%-9s\n" $HTTPY $HTTPYBOT $GOPHERY $GEMINIY $GEMINIYBOT "Yesterday" exit
Gives an output like this
Visitors to server.m0yng.uk on 13/Jul/2021 HTTP Bot? Gopher Gemini Bot? ----------------------------------- 585 498 0 9 0 0 0 0 18 0 Yesterday
-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-
🖤 Black Lives Matter
💙🤍💜 Trans Rights are Human Rights
❤️🧡💛💚💙💜 Love is Love
Copyright © 2025 Christopher M0YNG - It is forbidden to use any part of this site for crypto/NFT/AI related projects.
Page generated 2025-08-07 by Complex 19