Skip to main content

Command Palette

Search for a command to run...

Sadservers.com Solution for: "Saskatoon": counting IPs.

There's a web server access log file at /home/admin/access.log. The file consists of one line per HTTP request, with the requester's IP address at...

Published
2 min read
Sadservers.com Solution for: "Saskatoon": counting IPs.
S

I am a CS graduate from IIEST Shibpur (2023). I love to go deep into computers and the Internet and find out how both of them work from first principles. I want to be capable of understanding both of them with full depth to make them more secure and truly understand and harness their capabilities.

I also love to build scalable systems in the backend and test them to their limits. I aim to build products people want, at scale.

Question details: https://sadservers.com/scenario/saskatoon

# inspect logs
less /home/admin/access.log

# print first word of each line (space is separator)
awk '{print $1}' /home/admin/access.log

# we need count of all IPs which we'll get with uniq -c but for that first we need to sort
# because uniq -c only counts strings as similar if they are adjacent to each other
# sort would help us group all similar IPs together
awk '{print $1}' /home/admin/access.log | sort

# get count of each IP
awk '{print $1}' /home/admin/access.log | sort | uniq -c

# the output of uniq -c is not sorted by frequency of each IP
# it just contains <frequency> <ip> when it saw that group, so we need to sort it by frequency in descending order
# sort by default sorts lexicographically, -n is to sort numerically, -r is for reverse
awk '{print $1}' /home/admin/access.log | sort | uniq -c | sort -nr

# print first line
awk '{print $1}' /home/admin/access.log | sort | uniq -c | sort -nr | head -n 1

# print second word of each line (only 1 line here)
awk '{print $1}' /home/admin/access.log | sort | uniq -c | sort -nr | head -n 1 | awk '{print $2}' > /home/admin/highestip.txt

# verify answer
sha1sum /home/admin/highestip.txt
# 6ef426c40652babc0d081d438b9f353709008e93

Go ahead and try it yourself. Explore different commands to do the same thing. → sadservers.com

Share how you would approach the problem in the comments below.