Filter ip in the list

defmodule PurgeOfIps do
# create the module started by application.ex each hour, starts a purge of Ips not seen last 7 days from the list
# get list of ips
def ip_not_listed_7_days do
# get list of ips
seconds_in_seven_days = 604_800
ips = File.read!(“hardcodedList.txt”)
# start the purge of ips not seen last 7 days every hour
ips
|> String.split(~r/\W/, trim: true)
end)
end
end
I have the ips = [
[“192.168.44.152”, “2021-12-16”, “12:44:11.822908”],
[“192.168.44.163”, “2021-12-16”, “12:44:11.822924”],
[“192.168.44.164”, “2021-12-16”, “12:44:11.822931”],
[“192.168.44.165”, “2021-12-16”, “12:44:11.822937”],
[“192.168.44.166”, “2021-12-16”, “12:44:11.822943”],
[“192.168.44.167”, “2021-12-16”, “12:44:11.822962”],
[“192.168.44.168”, “2021-12-16”, “12:44:11.822984”],
[“192.168.44.169”, “2021-12-16”, “12:44:11.822991”],
[“192.168.44.151”, “2021-12-16”, “12:44:11.823005”],
[“192.168.44.153”, “2021-12-16”, “12:44:11.823012”]
]
How to check the list every hour and filter out the ip not seen in last 7 days??

Can you please format your code with triple backticks – ``` – at the top and the bottom of the code block?

And can you please also provide your hardcodedList.txt file?

it is the same ips in that txt file.

Can you show us what have you tried?

I have written in the file with ip and timestamp. which i mentioned in previous comment with code.

Hi.

  1. use parts: 2 in your split function to only split into two items. first will be the IP address, second the date, which you should convert to DateTime or NaiveDateTime.

2 . write a function that will filter your list of entries, you can use the DateTime.compare or DateTime.diff and list the ones that are older than 7 days.
After that run Enum.uniq to return unique ip addresses, and that is your solution to your problem.

Let us know if you have any more questions.

1 Like

Thank you for the help. I will try to complete it.

1 Like

But there is a list of list. How do i do it?? For example: [
[“192.168.44.163”, “2021-12-16”, “12:44:11.822924”],
[“192.168.44.164”, “2021-12-16”, “12:44:11.822931”],
[“192.168.44.165”, “2021-12-16”, “12:44:11.822937”],
[“192.168.44.166”, “2021-12-16”, “12:44:11.822943”],
[“192.168.44.167”, “2021-12-16”, “12:44:11.822962”],
[“192.168.44.168”, “2021-12-16”, “12:44:11.822984”],
[“192.168.44.169”, “2021-12-16”, “12:44:11.822991”],
[“192.168.44.151”, “2021-12-16”, “12:44:11.823005”],
[“192.168.44.153”, “2021-12-16”, “12:44:11.823012”]
]
after i implement String.split function. Can you please elaborate it?? I am new to this language.

Let’s work over some real code.
Create a github repot with a Livebook or a sample project.

ok sure

Split is if your source is a string (that is, you are extracting this info from a plain text file),
If you already have that list, because let’s say you have retrieved that from a database, you can map with Enum.map over it, and convert the last two elements into a DateTime with DateTime.new/2

I have created the github file now. Can you please check it?

I am.
to begin with, your log file has no new lines, While it is not impossible, it add an unnecessary extra level of complexity. You should fix that.
I will create issues in your Github repo so I don’t bombard the forum with comments.

sure thank you.

1 Like