I’m trying to read a copy of my servers firewall deny list into an array but only one array element is returned. Here is a part of the list:
46.229.168.0/24 # {do not delete} - Thu Nov 14 01:23:31 2019
43.128.88.156 # lfd: Tue Dec 12 11:11:01 2023
150.158.44.71 # lfd: t 3600 secs - Tue Dec 12 11:18:27 2023
I have tried these two methods of reading the file into an array:
$ips = file('log.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
print_r($ips);
$file = fopen('log.txt',"r");
$ips = [];
while ($line = fgets($file)) {
$ips[] = $line;
}
fclose($file);
print_r($ips);
In both cases, the following is returned:
Array
(
[0] => 46.229.168.0/24 # {do not delete} - Thu Nov 14 01:23:31 2019
43.128.88.156 # lfd: (sshd) Failed SSH login from 43.128.88.156 (SG/Singapore/-): 5 in the last 3600 secs - Tue Dec 12 11:11:01 2023
150.158.44.71 # lfd: (sshd) Failed SSH login from 150.158.44.71 (CN/China/-): 5 in the last 3600 secs - Tue Dec 12 11:18:27 2023
)
I’ve used the file command many times so I assume the problem is in how the input file is formatted. It is just a straight copy of the servers firewall list. Am I doing something incorrectly or need to use different code?