Why do I get three user outputs instead of 2 [closed] - perl

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am having a problem with the output of this code:
#!/usr/bin/perl
# Opening the file timelog.txt, the "<" indicates that the file is being opened in read mode
open(DATA, "<timelog.txt") or die "Couldn't open file timelog.txt, $!";
# Looping through each line in this file, this each line can be accesed with $_
while(<DATA>) {
# In the line ($_), we search for patters
# We first search the pattern " pts/"
$_ =~ m/ pts\//;
# We store the text before this pattern in the variable userId
$userId = "$`";
# We now search for the patter "Word Number Number:Number - Number:Number"
# This pattern is found at the end of each string
$_ =~ m/(\w+) (\d+) (\d+):(\d+) - (\d+):(\d+)/;
# The required variables, i.e. the month start hour, start minutes, end hour and end minutes are extracted
($month, $hours1, $minutes1, $hours2, $minutes2) = ($1, $3, $4, $5, $6);
# $userId." ".$month is used to concatenate the userId along with month with a space between them
# the . is used to concatenate
# If the key already exists, we increase the value of time
if(exists($store{$userId." ".$month})){
$store{$userId." ".$month} += (($hours2 - $hours1)*60) + $minutes2 - $minutes1;
} else {
# If the key does not exist we create a new key value pair
$store{$userId." ".$month} = (($hours2 - $hours1)*60) + $minutes2 - $minutes1;
}
# Note that the key is concatenation of userId and the month
}
# We now loop through the elements and print them
foreach $key (keys %store){
print "$key $store{$key} minutes\n";
}
timelog.txt:
maclawty796 pts/1 10.1.28.122 Mon Oct 24 09:18 - 09:20
maclawty796 pts/1 ip-64-134-238-2. Fri Oct 21 14:23 - 14:25
maclawty796 pts/2 10.1.28.122 Fri Oct 21 09:42 - 09:55
ehowe pts/3 10.1.28.204 Fri Oct 21 09:28 - 09:29
kho398 pts/2 10.1.28.233 Fri Oct 21 09:22 - 09:34
maclawty796 pts/1 10.1.28.122 Fri Oct 21 09:20 - 09:55
maclawty796 pts/1 75.27.188.106 Thu Oct 20 21:09 - 21:23
maclawty796 pts/1 10.1.28.120 Fri Oct 14 09:46 - 09:52
kho398 pts/2 10.1.28.124 Thu Oct 13 09:29 - 09:30
maclawty796 pts/1 10.1.28.128 Thu Oct 13 09:28 - 09:51
maclawty796 pts/2 adsl-75-30-120-1 Wed Oct 12 20:35 - 20:35
maclawty796 pts/1 75-30-120-13.lig Wed Oct 12 19:27 - 19:33
hturner pts/1 tom-nilsons-macb Wed Oct 12 13:30 - 13:32
nnt pts/2 99-59-5-115.ligh Tue Oct 11 15:51 - 15:54
nnt pts/1 hitami yamas Tue Oct 11 15:31 - 17:42
maclawty796 pts/1 leda.local Tue Oct 11 14:10 - 14:11
ehowe pts/2 10.1.28.126 Tue Oct 11 09:21 - 09:24
maclawty796 pts/2 10.1.28.123 Tue Oct 11 09:01 - 09:08
nnt pts/1 10.1.28.210 Tue Oct 11 08:53 - 12:02
nnt pts/1 hitami yamas Tue Oct 11 07:53 - 07:54
nnt pts/1 hitami yamas Tue Oct 11 07:48 - 07:52
maclawty796 pts/1 adsl-75-16-61-10 Mon Oct 10 23:18 - 23:20
nnt pts/1 99-59-5-115.ligh Mon Oct 10 22:16 - 23:11
nnt pts/1 99-59-5-115.ligh Mon Oct 10 22:09 - 22:14
nnt pts/1 99-59-5-115.ligh Mon Oct 10 21:27 - 21:31
maclawty796 pts/2 adsl-75-16-61-10 Mon Oct 10 20:46 - 20:47
nnt pts/1 99-59-5-115.ligh Mon Oct 10 20:19 - 21:22
kho398 pts/2 108-93-76-161.li Mon Oct 10 19:15 - 19:19
kho398 pts/2 108-93-76-161.li Mon Oct 10 19:09 - 19:12
jimquinn409 pts/2 10.1.8.148 Mon Oct 10 17:40 - 17:46
jimquinn409 pts/2 10.1.8.148 Mon Oct 10 17:38 - 17:39
nnt pts/1 99-59-5-115.ligh Mon Oct 10 17:19 - 20:01
jimquinn409 pts/2 10.1.8.148 Mon Oct 10 16:29 - 16:33
jimquinn409 pts/2 10.1.8.148 Mon Oct 10 16:27 - 16:28
nnt pts/1 99-59-5-115.ligh Mon Oct 10 16:01 - 17:18
nnt pts/1 99-59-5-115.ligh Mon Oct 10 15:11 - 15:59
maclawty796 pts/1 10.1.31.27 Mon Oct 10 14:16 - 14:21
johnhouston pts/2 10.1.31.220 Mon Oct 10 12:01 - 12:17
jimquinn409 pts/1 ubuntu.local Mon Oct 10 11:58 - 12:04
kho398 pts/1 10.1.28.244 Mon Oct 10 09:07 - 09:17
maclawty796 pts/1 10.1.31.27 Mon Oct 10 08:45 - 08:49
honeill pts/1 99-59-7-95.light Mon Oct 10 05:42 - 07:14
honeill pts/1 99-59-7-95.light Mon Oct 10 04:26 - 05:26
kho398 pts/1 108-93-76-161.li Sun Oct 9 23:12 - 23:13
kho398 pts/1 108-93-76-161.li Sun Oct 9 20:56 - 21:15
kho398 pts/1 108-93-76-161.li Sun Oct 9 20:54 - 20:56
kho398 pts/1 108-93-76-161.li Sun Oct 9 20:51 - 20:52
kho398 pts/2 108-93-76-161.li Sun Oct 9 20:40 - 20:48
maclawty796 pts/1 adsl-75-27-233-7 Sun Oct 9 20:30 - 20:42
kho398 pts/1 108-93-76-161.li Sun Oct 9 20:15 - 20:19
johnhouston pts/2 99-176-56-113.li Sun Oct 9 19:58 - 20:04
kho398 pts/1 108-93-76-161.li Sun Oct 9 19:48 - 20:00
kho398 pts/3 108-93-76-161.li Sun Oct 9 19:36 - 19:48
kho398 pts/3 108-93-76-161.li Sun Oct 9 19:35 - 19:36
kho398 pts/3 108-93-76-161.li Sun Oct 9 19:32 - 19:34
johnhouston pts/2 99-176-56-113.li Sun Oct 9 19:29 - 19:55
nnt pts/1 99-59-5-115.ligh Sun Oct 9 18:29 - 19:45
kho398 pts/4 108-93-76-161.li Sun Oct 9 18:27 - 19:32
johnhouston pts/3 99-176-56-113.li Sun Oct 9 17:12 - 19:28
nnt pts/2 99-59-5-115.ligh Sun Oct 9 15:38 - 18:31
nnt pts/1 99-59-5-115.ligh Sun Oct 9 15:36 - 18:29
maclawty796 pts/1 10.1.31.27 Fri Oct 7 13:55 - 14:09
maclawty796 pts/1 10.1.31.27 Fri Oct 7 13:39 - 13:41
johnhouston pts/1 99-176-56-113.li Fri Oct 7 12:07 - 13:29
edrodri5722 pts/1 10.1.28.171 Fri Oct 7 09:49 - 09:52
kho398 pts/1 10.1.28.116 Fri Oct 7 09:46 - 09:48
jimquinn409 pts/1 10.1.28.200 Fri Oct 7 09:39 - 09:40
jimquinn409 pts/3 10.1.28.200 Fri Oct 7 08:56 - 09:42
nnt pts/3 hitami yamas Fri Oct 7 07:52 - 07:59
nnt pts/2 99-59-5-115.ligh Fri Oct 7 07:15 - 09:18
nnt pts/1 99-59-5-115.ligh Fri Oct 7 07:13 - 09:30
kho398 pts/1 108-93-76-161.li Fri Oct 7 01:15 - 01:17
kho398 pts/1 108-93-76-161.li Fri Oct 7 01:14 - 01:15
kho398 pts/1 108-93-76-161.li Fri Oct 7 01:12 - 01:13
kho398 pts/2 108-93-76-161.li Fri Oct 7 01:06 - 01:12
kho398 pts/2 108-93-76-161.li Fri Oct 7 01:04 - 01:05
kho398 pts/2 108-93-76-161.li Fri Oct 7 00:53 - 01:04
johnhouston pts/1 99-176-56-113.li Fri Oct 7 00:25 - 01:08
edrodri5722 pts/2 10.1.31.151 Thu Oct 6 22:27 - 22:48
honeill pts/1 jst-pc.local Thu Oct 6 22:19 - 22:53
nnt pts/1 99-59-5-115.ligh Thu Oct 6 19:44 - 19:54
honeill pts/6 jst-pc.local Thu Oct 6 19:43 - 22:19
edrodri5722 pts/5 10.1.31.151 Thu Oct 6 19:40 - 22:40
ehowe pts/2 ubuntu.local Thu Oct 6 19:40 - 20:36
hturner pts/2 97-90-193-108.dh Thu Oct 6 19:12 - 19:22
jimquinn409 pts/3 10.1.8.129 Thu Oct 6 18:21 - 22:54
jimquinn409 pts/5 10.1.8.129 Thu Oct 6 17:59 - 18:08
jimquinn409 pts/5 10.1.8.129 Thu Oct 6 17:55 - 17:58
johnhouston pts/4 99-176-56-113.li Thu Oct 6 17:13 - 22:15
johnhouston pts/4 99-176-56-113.li Thu Oct 6 17:09 - 17:12
johnhouston pts/4 99-176-56-113.li Thu Oct 6 17:07 - 17:08
jimquinn409 pts/3 ubuntu.local Thu Oct 6 16:56 - 18:14
hturner pts/2 97-90-193-108.dh Thu Oct 6 16:43 - 18:43
nnt pts/1 99-59-5-115.ligh Thu Oct 6 16:42 - 19:43
jimquinn409 pts/1 ubuntu.local Thu Oct 6 16:16 - 16:25
hturner pts/4 108-67-52-153.li Thu Oct 6 15:18 - 16:10
hturner pts/3 108-67-52-153.li Thu Oct 6 15:17 - 16:10
honeill pts/3 10.0.18.162 Thu Oct 6 14:00 - 14:15
hturner pts/2 10.1.13.227 Thu Oct 6 13:01 - 15:42
hturner pts/1 10.1.13.227 Thu Oct 6 13:01 - 15:41
maclawty796 pts/6 10.1.31.27 Thu Oct 6 10:02 - 10:11
hturner pts/5 tom-nilsons-macb Thu Oct 6 10:00 - 12:17
hturner pts/4 tom-nilsons-macb Thu Oct 6 09:58 - 12:17
edrodri5722 pts/4 10.1.28.135 Thu Oct 6 09:51 - 09:52
jimquinn409 pts/1 10.1.28.126 Thu Oct 6 09:49 - 12:06
jimquinn409 pts/4 10.1.28.126 Thu Oct 6 09:41 - 09:41
jimquinn409 pts/4 10.1.28.126 Thu Oct 6 09:29 - 09:35
hturner pts/2 10.1.28.167 Thu Oct 6 09:28 - 12:05
mvan682 pts/8 10.1.28.119 Thu Oct 6 09:17 - 09:18
honeill pts/7 10.1.28.163 Thu Oct 6 09:15 - 09:57
jimquinn409 pts/2 10.1.28.126 Thu Oct 6 09:15 - 09:21
jimquinn409 pts/7 10.1.28.126 Thu Oct 6 09:13 - 09:14
jimquinn409 pts/7 10.1.28.126 Thu Oct 6 09:08 - 09:11
ehowe pts/6 10.1.28.155 Thu Oct 6 09:07 - 09:28
maclawty796 pts/5 10.1.28.123 Thu Oct 6 09:05 - 09:28
maclawty796 pts/4 10.1.28.123 Thu Oct 6 09:04 - 09:28
hturner pts/3 10.1.28.167 Thu Oct 6 09:01 - 12:06
honeill pts/2 10.1.28.163 Thu Oct 6 08:59 - 09:14
edrodri5722 pts/1 10.1.28.135 Thu Oct 6 08:56 - 09:47
ehowe pts/2 10.1.28.155 Thu Oct 6 08:49 - 08:54
maclawty796 pts/1 10.1.31.27 Thu Oct 6 08:24 - 08:53
edrodri5722 pts/1 76-202-53-118.li Thu Oct 6 05:11 - 05:23
kho398 pts/1 108-93-76-161.li Thu Oct 6 02:18 - 02:23
kho398 pts/2 108-93-76-161.li Thu Oct 6 00:36 - 02:04
ehowe pts/1 10.0.17.151 Thu Oct 6 00:29 - 00:41
jimquinn409 pts/3 76-202-52-236.li Thu Oct 6 00:21 - 02:28
ehowe pts/1 10.0.17.151 Thu Oct 6 00:11 - 00:22
ehowe pts/1 10.0.17.180 Thu Oct 6 00:05 - 00:11
ehowe pts/1 10.0.17.151 Wed Oct 5 23:17 - 23:26
ehowe pts/1 10.1.6.218 Wed Oct 5 22:06 - 22:20
ehowe pts/1 10.1.6.218 Wed Oct 5 21:38 - 21:48
maclawty796 pts/2 adsl-75-16-59-16 Wed Oct 5 21:04 - 21:07
ehowe pts/3 10.1.6.218 Wed Oct 5 20:53 - 21:28
ehowe pts/2 10.1.6.218 Wed Oct 5 20:46 - 21:02
maclawty796 pts/3 adsl-75-16-59-16 Wed Oct 5 19:20 - 20:04
nnt pts/2 99-59-5-115.ligh Wed Oct 5 19:09 - 20:22
kho398 pts/1 108-93-76-161.li Wed Oct 5 19:00 - 21:31
maclawty796 pts/1 10.1.31.27 Wed Oct 5 15:27 - 16:28
maclawty796 pts/1 :0.0 Wed Oct 5 15:10 - 15:25
maclawty796 pts/1 10.1.31.27 Wed Oct 5 13:34 - 13:42
maclawty796 pts/1 10.1.31.27 Wed Oct 5 13:29 - 13:30
nnt pts/2 99-59-5-115.ligh Wed Oct 5 10:51 - 13:15
nnt pts/1 166.205.141.16 Wed Oct 5 09:28 - 12:07
honeill pts/4 76-202-53-118.li Tue Oct 4 21:12 - 22:47
honeill pts/4 76-202-53-118.li Tue Oct 4 21:02 - 21:11
edrodri5722 pts/1 76-202-53-118.li Tue Oct 4 20:46 - 22:48
jimquinn409 pts/1 10.1.8.233 Tue Oct 4 20:37 - 20:43
edrodri5722 pts/4 76-202-53-118.li Tue Oct 4 20:29 - 20:32
edrodri5722 pts/4 76-202-53-118.li Tue Oct 4 20:02 - 20:09
edrodri5722 pts/3 76-202-53-118.li Tue Oct 4 19:43 - 21:51
nnt pts/2 166.205.141.16 Tue Oct 4 19:19 - 21:54
nnt pts/1 166.205.141.16 Tue Oct 4 18:29 - 20:33
nnt pts/3 108-195-113-206. Tue Oct 4 16:47 - 17:18
maclawty796 pts/2 10.1.31.27 Tue Oct 4 16:43 - 16:59
maclawty796 pts/2 10.1.31.27 Tue Oct 4 16:27 - 16:32
maclawty796 pts/3 10.1.31.27 Tue Oct 4 16:26 - 16:26
maclawty796 pts/2 10.1.31.27 Tue Oct 4 15:05 - 16:26
nnt pts/1 108-195-113-206. Tue Oct 4 14:40 - 17:32
kho398 pts/1 108-93-76-161.li Tue Oct 4 02:22 - 03:08
edrodri5722 pts/1 76-202-53-118.li Mon Oct 3 22:32 - 22:36
edrodri5722 pts/1 76-202-53-118.li Mon Oct 3 20:45 - 22:31
edrodri5722 pts/1 76-202-53-118.li Mon Oct 3 20:44 - 20:44
nnt pts/1 108-195-113-206. Mon Oct 3 16:07 - 19:54
jimquinn409 pts/1 10.1.28.142 Mon Oct 3 09:04 - 09:47
kho398 pts/1 10.1.28.199 Mon Oct 3 09:01 - 09:02
kho398 pts/1 108-93-76-161.li Sun Oct 2 22:27 - 22:29
kho398 pts/1 108-93-76-161.li Sun Oct 2 19:18 - 19:23
kho398 pts/1 108-93-76-161.li Sun Oct 2 18:51 - 19:17
edrodri5722 pts/1 76-202-53-118.li Sun Oct 2 18:28 - 18:47
kho398 pts/1 108-93-76-161.li Sun Oct 2 17:04 - 18:16
maclawty796 pts/1 adsl-75-28-65-14 Sat Oct 1 21:15 - 21:32
maclawty796 pts/1 10.1.31.27 Fri Sep 30 15:19 - 15:20
hturner pts/1 108-67-52-153.li Fri Sep 30 11:06 - 11:13
mvan682 pts/4 10.1.28.119 Fri Sep 30 09:50 - 09:50
kho398 pts/3 10.1.28.199 Fri Sep 30 09:49 - 09:50
kho398 pts/3 10.1.28.199 Fri Sep 30 09:29 - 09:46
mvan682 pts/4 10.1.28.121 Fri Sep 30 09:04 - 09:12
kho398 pts/3 10.1.28.199 Fri Sep 30 09:02 - 09:21
jimquinn409 pts/2 10.1.28.142 Fri Sep 30 09:02 - 11:27
mvan682 pts/2 10.1.28.121 Fri Sep 30 09:02 - 09:02
kho398 pts/2 10.1.28.199 Fri Sep 30 09:00 - 09:01
maclawty796 pts/1 10.1.28.212 Fri Sep 30 08:57 - 09:51
maclawty796 pts/1 adsl-75-28-22-24 Thu Sep 29 20:16 - 20:35
nnt pts/1 108-195-113-206. Thu Sep 29 16:24 - 17:14
jimquinn409 pts/2 ubuntu.local Thu Sep 29 15:24 - 17:40
maclawty796 pts/2 enoch-tang-ima Thu Sep 29 14:21 - 14:22
maclawty796 pts/2 10.1.31.27 Thu Sep 29 14:08 - 14:09
johnhouston pts/2 187.34 -32-94.dhcp Thu Sep 29 13:25 - 13:40
johnhouston pts/1 187.34 -32-94.dhcp Thu Sep 29 13:10 - 15:25
mvan682 pts/3 10.1.28.119 Thu Sep 29 09:47 - 09:48
kho398 pts/2 10.1.28.103 Thu Sep 29 09:47 - 09:51
kho398 pts/2 10.1.28.103 Thu Sep 29 09:44 - 09:46
maclawty796 pts/1 10.1.28.105 Thu Sep 29 09:43 - 09:54
mvan682 pts/3 10.1.28.119 Thu Sep 29 09:23 - 09:24
edrodri5722 pts/2 10.1.28.199 Thu Sep 29 09:20 - 09:23
jimquinn409 pts/1 10.1.28.142 Thu Sep 29 09:20 - 09:28
kho398 pts/3 10.1.28.103 Thu Sep 29 09:08 - 09:15
ehowe pts/2 10.1.28.106 Thu Sep 29 09:07 - 09:10
maclawty796 pts/1 10.1.28.105 Thu Sep 29 09:05 - 09:15
ehowe pts/1 10.1.28.106 Thu Sep 29 08:51 - 08:52
ehowe pts/1 10.1.28.225 Thu Sep 29 08:47 - 08:48
maclawty796 pts/2 10.1.31.27 Thu Sep 29 08:41 - 08:56
ehowe pts/1 10.1.28.225 Thu Sep 29 08:40 - 08:46
honeill pts/1 99-59-7-95.light Thu Sep 29 03:50 - 03:56
edrodri5722 pts/1 76-202-53-118.li Thu Sep 29 01:03 - 01:04
edrodri5722 pts/1 76-202-53-118.li Thu Sep 29 00:59 - 01:03
johnhouston pts/1 187.34 -32-94.dhcp Thu Sep 29 00:49 - 00:55
johnhouston pts/1 187.34 -32-94.dhcp Wed Sep 28 23:40 - 23:43
ehowe pts/1 10.0.17.91 Wed Sep 28 21:57 - 22:12
ehowe pts/1 10.0.17.91 Wed Sep 28 21:23 - 21:53
kho398 pts/2 10.1.8.83 Wed Sep 28 20:16 - 20:18
kho398 pts/2 10.1.8.83 Wed Sep 28 20:13 - 20:13
kho398 pts/2 10.1.8.83 Wed Sep 28 20:10 - 20:11
kho398 pts/2 10.1.8.83 Wed Sep 28 20:06 - 20:07
kho398 pts/2 10.1.8.83 Wed Sep 28 19:53 - 20:00
ehowe pts/1 10.0.17.91 Wed Sep 28 19:17 - 21:11
jimquinn409 pts/1 10.1.8.85 Wed Sep 28 15:25 - 16:22
maclawty796 pts/1 10.1.31.27 Wed Sep 28 14:32 - 14:37
kho398 pts/1 108-93-76-161.li Wed Sep 28 00:28 - 01:05
kho398 pts/1 108-93-76-161.li Wed Sep 28 00:25 - 00:28
maclawty796 pts/1 75.16.62.214 Tue Sep 27 22:32 - 22:44
nnt pts/1 enoch-tang-ima Tue Sep 27 16:26 - 16:26
nnt pts/2 psc147-04-4.loca Tue Sep 27 15:42 - 16:22
johnhouston pts/1 psc147-04-3.loca Tue Sep 27 15:40 - 16:22
maclawty796 pts/1 psc147-04-3.loca Tue Sep 27 15:37 - 15:37
maclawty796 pts/1 psc147-04.local Tue Sep 27 15:26 - 15:26
hturner pts/1 10.1.13.49 Tue Sep 27 12:52 - 12:58
hturner pts/1 10.1.13.49 Tue Sep 27 12:50 - 12:51
jimquinn409 pts/2 10.1.31.27 Tue Sep 27 10:06 - 10:06
honeill pts/2 10.1.31.27 Tue Sep 27 10:05 - 10:06
maclawty796 pts/1 10.1.31.27 Tue Sep 27 10:04 - 10:07
maclawty796 pts/2 10.1.28.120 Tue Sep 27 09:42 - 09:52
edrodri5722 pts/1 10.1.28.207 Tue Sep 27 09:41 - 09:46
mvan682 pts/1 10.1.28.157 Tue Sep 27 09:39 - 09:39
mvan682 pts/1 10.1.28.157 Tue Sep 27 09:33 - 09:37
maclawty796 pts/1 10.1.31.27 Tue Sep 27 08:37 - 08:54
maclawty796 pts/1 adsl-75-30-121-1 Mon Sep 26 22:51 - 23:06
maclawty796 pts/1 75-30-121-143.li Mon Sep 26 22:44 - 22:49
edrodri5722 pts/2 10.1.31.27 Mon Sep 26 12:14 - 12:14
edrodri5722 pts/2 10.1.31.27 Mon Sep 26 12:13 - 12:13
maclawty796 pts/1 10.1.31.27 Mon Sep 26 12:11 - 12:14
maclawty796 pts/1 10.1.28.154 Mon Sep 26 09:55 - 09:55
I do not know why I am getting 3 outputs for each user (each should be just 2) being the third one an empty one.

Please make a small change to your code for debug purpose:
at very begin add
use strict;
use warnings;
and output your captured data
# The required variables, i.e. the month start hour, start minutes, end hour and end minutes are extracted
my ($month, $hours1, $minutes1, $hours2, $minutes2) = ($1, $3, $4, $5, $6);
my $format = "DEBUG: %-25s %3s %02d:%02d %02d:%02d\n";
printf $format,$userId, $month, $hours1, $minutes1, $hours2, $minutes2;
# $userId." ".$month is used to concatenate the userId along with month with a space between them
Debug information gives you a clue where the problem is originated from and you will figure out the direction how to correct it.
NOTE: your code is flowed as it does not take into an account when login crosses midnight hour -- computation will get negative time
The code after a slight modification could look like following (midnight issue still present)
use strict;
use warnings;
use feature 'say';
my $fname = shift || 'timelog.txt';
my %store;
my $re = qr|(\w+)\s+pts/\d\s+(.*?)\s+\w{3} (\w{3}) (\d+) (\d+):(\d+) - (\d+):(\d+)|;
# Opening the file timelog.txt, the "<" indicates that the file is being opened in read mode
open(DATA, '<', $fname)
or die "Couldn't open file timelog.txt, $!";
while(<DATA>) {
next unless /$re/;
my($userId,$host,$month,$hours1,$minutes1,$hours2,$minutes2) = ($1,$2,$3,$4,$5,$6,$7);
# $userId." ".$month is used to concatenate the userId along with month with a space between them
# the . is used to concatenate
# If the key already exists, we increase the value of time
if(exists($store{$userId." ".$month})){
$store{$userId." ".$month} += (($hours2 - $hours1)*60) + $minutes2 - $minutes1;
} else {
# If the key does not exist we create a new key value pair
$store{$userId." ".$month} = (($hours2 - $hours1)*60) + $minutes2 - $minutes1;
}
# Note that the key is concatenation of userId and the month
}
# We now loop through the elements and print them
foreach my $key (sort keys %store){
printf "%-25s - %6d minutes\n", $key, $store{$key};
}
Final output
edrodri5722 Sep - -959 minutes
ehowe Oct - 1020 minutes
ehowe Sep - 2523 minutes
honeill Oct - 2883 minutes
honeill Sep - -60 minutes
hturner Oct - 1080 minutes
hturner Sep - 1440 minutes
jimquinn409 Oct - 8521 minutes
jimquinn409 Sep - -3955 minutes
johnhouston Oct - -540 minutes
johnhouston Sep - 1323 minutes
kho398 Oct - 1080 minutes
kho398 Sep - -4378 minutes
maclawty796 Oct - 9840 minutes
maclawty796 Sep - 3182 minutes
mvan682 Sep - -240 minutes
nnt Oct - 12792 minutes
nnt Sep - 542 minutes
Reference:
open,
regex,
Perl regular expression quick start

Related

how to use logrotate for keeping compressed 5 days files by excluding current date log files

Log files mentioned as below in /var/log/
340 Mar 1 10:36 sql-20220301.log
350913 Mar 1 10:43 sql-20220301.csv
1298450 Mar 2 17:59 sql-20220302.log
1689208 Mar 2 17:59 sql-20220302.csv
3207594 Mar 3 17:59 sql-20220303.log
2250155 Mar 3 17:59 sql-20220303.csv
3200691 Mar 4 17:59 sql-20220304.log
2144001 Mar 4 17:59 sql-20220304.csv
3200691 Mar 5 17:59 sql-20220305.log
2139968 Mar 5 17:5 sql-20220305.csv
3200691 Mar 6 17:59 sql-20220306.log
2140712 Mar 6 17:59 sql-20220306.csv
3202992 Mar 7 17:59 sql-20220307.log
2158303 Mar 7 17:59 sql-20220307.csv
3212196 Mar 8 17:59 sql-20220308.log
2175547 Mar 8 17:59 sql-20220308.csv
2237950 Mar 9 12:31 sql-20220309.log
1661695 Mar 9 17:30 sql-20220309.csv
Assume that current date is mar 9 , i want to keep this as original by keeping 5 days (mar 4- mar8) both .log and .csv as .gz files.
created conf file in /etc/logroatate.d/sqlchck as
/var/log/.log /var/log/.csv {
daily
rotate 5
compress
delaycompress
dateext
nocopytruncate
notifempty
missingok
}
it doesn't works as expected.
i need below result as
3200691 Mar 4 17:59 sql-20220304.log.gz
2144001 Mar 4 17:59 sql-20220304.csv.gz
3200691 Mar 5 17:59 sql-20220305.log.gz
2139968 Mar 5 17:5 sql-20220305.csv.gz
3200691 Mar 6 17:59 sql-20220306.log.gz
2140712 Mar 6 17:59 sql-20220306.csv.gz
3202992 Mar 7 17:59 sql-20220307.log.gz
2158303 Mar 7 17:59 sql-20220307.csv.gz
3212196 Mar 8 17:59 sql-20220308.log.gz
2175547 Mar 8 17:59 sql-20220308.csv.gz
2237950 Mar 9 12:31 sql-20220309.log
1661695 Mar 9 17:30 sql-20220309.csv

Perl Script to determine how many logins were still on the system after 16:00

I have a data file that displays the following data:
cfs264su pts/6 x.x.x.x.x Tue May 26 16:46 - 19:21 (02:34)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:30 - 16:46 (00:15)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:19 - 16:30 (00:10)
cfs264su pts/6 x.x.x.x.x Tue May 26 14:59 - 15:30 (00:31)
cfs264su pts/5 x.x.x.x.x Tue May 26 14:40 - 17:13 (02:33)
cfs264su pts/1 x.x.x.x.x Tue May 26 14:02 - 19:06 (05:03)
cfs264su pts/6 x.x.x.x.x Tue May 26 10:36 - 13:18 (02:41)
cfs264su pts/5 x.x.x.x.x Tue May 26 10:22 - 12:45 (02:23)
cfs264su pts/1 x.x.x.x.x Tue May 26 08:45 - 12:12 (03:27)
cfs264su pts/5 x.x.x.x.x Tue May 26 00:34 - 01:28 (00:54)
I have created a perl script that is suppose to display how many logins were still signed in after 16:00 on May 26th. This is what I have so far and cant figure out how to display the correct number of logins which are 5.
#!/usr/bin/perl
open(FILE, $ARGV[0]) or die ("Error Found: $!");
while ( $line = <FILE> ) {
($login, $time) = split('\(', $line);
# print "Time: $time";
($hour,$m) = split('\:',$time);
# print "Hour: $hour\n";
if ( $hour <= 16 ) {
# print "Found: $hour\n";
$n++;
}
}
print "There were $n logins that were still on the system after 16:00 on May 26.\n";
close(FILE);
I finally figure it out. the split had to use / - /
#!/usr/bin/perl
open(FILE, $ARGV[0]) or die ("Error Found: $!");
while ( $line = <FILE> ) {
($login, $time) = split(/ - /, $line);
# print "Time: $time";
($hour,$m) = split('/ - /:',$time);
# print "Hour: $hour\n";
if ( $hour < 16 ) {
# print "Found: $hour\n";
$n++;
}
}
print "There were $n logins that were still on the system after 16:00 on May 26.\n";
close(FILE);
Thanks!
Following code sample demonstrates one of possible ways.
Algorithm:
convert date and time into epoch for $start and $end timeframe
read input data into hash $record->{#fields} with fields as keys
store read record into HoA %login with userid as a key
for user of interest go through all records
compare login time with $start and $end timeframe
if match increase $count
print $count on completion
Run as:
Linux script.pl user '[start]' '[end]'
Windows script.pl user "[start]" "[end]"
use strict;
use warnings;
use feature 'say';
use Date::Parse;
my $user = shift || 'cfs264su';
my $start = shift || 'May 26 10:00 2020';
my $end = shift || 'May 26 15:00 2020';
$start = date2epoch($start);
$end = date2epoch($end);
say 'USER: ' . $user;
say 'START: ' . scalar localtime $start;
say 'END: ' . scalar localtime $end;
say '-' x 45;
my %login;
my #fields = qw/id pts what wday month mday in out duration/;
my $count;
while( <DATA> ) {
my($id, $record);
$record->#{#fields} = split "[- ()]+";
$id = $record->{id};
delete $record->{id};
push #{$login{$id}}, $record;
}
for( #{$login{$user}} ) {
my $in = date2epoch(join ' ', $_->#{qw/month mday in/});
my $out = date2epoch(join ' ', $_->#{qw/month mday out/});
if( $in >= $start && $in <= $end) {
++$count;
say 'IN: ' . scalar localtime $in;
say 'OUT: ' . scalar localtime $out;
say '-' x 45;
}
}
say "Logged in: $count times";
exit;
sub date2epoch {
my $date = shift;
return str2time($date);
}
__DATA__
cfs264su pts/6 x.x.x.x.x Tue May 26 16:46 - 19:21 (02:34)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:30 - 16:46 (00:15)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:19 - 16:30 (00:10)
cfs264su pts/6 x.x.x.x.x Tue May 26 14:59 - 15:30 (00:31)
cfs264su pts/5 x.x.x.x.x Tue May 26 14:40 - 17:13 (02:33)
cfs264su pts/1 x.x.x.x.x Tue May 26 14:02 - 19:06 (05:03)
cfs264su pts/6 x.x.x.x.x Tue May 26 10:36 - 13:18 (02:41)
cfs264su pts/5 x.x.x.x.x Tue May 26 10:22 - 12:45 (02:23)
cfs264su pts/1 x.x.x.x.x Tue May 26 08:45 - 12:12 (03:27)
cfs264su pts/5 x.x.x.x.x Tue May 26 00:34 - 01:28 (00:54)
Output
USER: cfs264su
START: Tue May 26 10:00:00 2020
END: Tue May 26 15:00:00 2020
---------------------------------------------
IN: Tue May 26 14:59:00 2020
OUT: Tue May 26 15:30:00 2020
---------------------------------------------
IN: Tue May 26 14:40:00 2020
OUT: Tue May 26 17:13:00 2020
---------------------------------------------
IN: Tue May 26 14:02:00 2020
OUT: Tue May 26 19:06:00 2020
---------------------------------------------
IN: Tue May 26 10:36:00 2020
OUT: Tue May 26 13:18:00 2020
---------------------------------------------
IN: Tue May 26 10:22:00 2020
OUT: Tue May 26 12:45:00 2020
---------------------------------------------
Logged in: 5 times
A simple approach using Time::Piece.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
# The format of the dates and times that we're dealing with
my $fmt = '%b %d %H:%M';
# Store the count
my $count;
# Get a Time::Piece object that represents our cut-off time
my $cutoff = Time::Piece->strptime('May 26 16:00', $fmt);
# Note: I'm reading from DATA here. It's an easy way to
# prototype stuff without having to deal with opening files
while (<DATA>) {
# Split the data on whitespace
# Pull out columns 4, 5 and 8
# Join them with a space - which gives us the end date/time
my $end_str = join ' ', (split /\s+/)[4, 5, 8];
# Parse that string into a Time::Piece object
my $end = Time::Piece->strptime($end_str, $fmt);
# Increment the counter if this date/time is
# greater than the cut-off
$count++ if $end > $cutoff;
}
say $count;
__DATA__
cfs264su pts/6 x.x.x.x.x Tue May 26 16:46 - 19:21 (02:34)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:30 - 16:46 (00:15)
cfs264su pts/6 x.x.x.x.x Tue May 26 16:19 - 16:30 (00:10)
cfs264su pts/6 x.x.x.x.x Tue May 26 14:59 - 15:30 (00:31)
cfs264su pts/5 x.x.x.x.x Tue May 26 14:40 - 17:13 (02:33)
cfs264su pts/1 x.x.x.x.x Tue May 26 14:02 - 19:06 (05:03)
cfs264su pts/6 x.x.x.x.x Tue May 26 10:36 - 13:18 (02:41)
cfs264su pts/5 x.x.x.x.x Tue May 26 10:22 - 12:45 (02:23)
cfs264su pts/1 x.x.x.x.x Tue May 26 08:45 - 12:12 (03:27)
cfs264su pts/5 x.x.x.x.x Tue May 26 00:34 - 01:28 (00:54)

freeradius daloradius authentication failure

i followed this tutorial to install freeradius and dalo radius for the raspberry pi:
http://www.binaryheartbeat.net/2013/12/raspberry-pi-based-freeradius-server.html
i tested the file authentication and it worked fine but after installing daloradius and switching to MySQL authnetications fail for unknown reasons
here is freeradius output that occurs when trying to authenticate a user:
rad_recv: Access-Request packet from host 192.168.1.1 port 32779, id=216, length=172
User-Name = "ccc"
State = 0xf9775519ff7f4c9188c14494359a170f
EAP-Message = 0x0208005b190017030100500d2898ca35aa9fa9e4febd8816c9e6deda71960fe5692b7c3d0499f2b5bba6b531483e373e14f8aff517aa081e214edc98e2c8bb22d16a961ecff4f498d20d152535b4d11ace1484b985bd2501ade77b
Service-Type = Framed-User
Framed-MTU = 1420
NAS-IP-Address = 192.168.1.1
Message-Authenticator = 0x49fc781b8a152fbec467b2c1f275a1a1
Tue Dec 29 18:38:47 2015 : Info: # Executing section authorize from file /etc/freeradius/sites-enabled/default
Tue Dec 29 18:38:47 2015 : Info: +group authorize {
Tue Dec 29 18:38:47 2015 : Info: ++[preprocess] = ok
Tue Dec 29 18:38:47 2015 : Info: ++[chap] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[mschap] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[digest] = noop
Tue Dec 29 18:38:47 2015 : Info: [suffix] No '#' in User-Name = "ccc", looking up realm NULL
Tue Dec 29 18:38:47 2015 : Info: [suffix] No such realm "NULL"
Tue Dec 29 18:38:47 2015 : Info: ++[suffix] = noop
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP packet type response id 8 length 91
Tue Dec 29 18:38:47 2015 : Info: [eap] Continuing tunnel setup.
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = ok
Tue Dec 29 18:38:47 2015 : Info: +} # group authorize = ok
Tue Dec 29 18:38:47 2015 : Info: Found Auth-Type = EAP
Tue Dec 29 18:38:47 2015 : Info: # Executing group from file /etc/freeradius/sites-enabled/default
Tue Dec 29 18:38:47 2015 : Info: +group authenticate {
Tue Dec 29 18:38:47 2015 : Info: [eap] Request found, released from the list
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP/peap
Tue Dec 29 18:38:47 2015 : Info: [eap] processing type peap
Tue Dec 29 18:38:47 2015 : Info: [peap] processing EAP-TLS
Tue Dec 29 18:38:47 2015 : Info: [peap] eaptls_verify returned 7
Tue Dec 29 18:38:47 2015 : Info: [peap] Done initial handshake
Tue Dec 29 18:38:47 2015 : Info: [peap] eaptls_process returned 7
Tue Dec 29 18:38:47 2015 : Info: [peap] EAPTLS_OK
Tue Dec 29 18:38:47 2015 : Info: [peap] Session established. Decoding tunneled attributes.
Tue Dec 29 18:38:47 2015 : Info: [peap] Peap state phase2
Tue Dec 29 18:38:47 2015 : Info: [peap] EAP type mschapv2
Tue Dec 29 18:38:47 2015 : Info: [peap] Got tunneled request
EAP-Message = 0x0208003e1a0208003931461c2f1334a4b7bab38912e9d82dd97b000000000000000070fb7810a938a00d884f17dc01b62eaa7dde9fbb7ab2cf4200636363
server {
Tue Dec 29 18:38:47 2015 : Info: [peap] Setting User-Name to ccc
Sending tunneled request
EAP-Message = 0x0208003e1a0208003931461c2f1334a4b7bab38912e9d82dd97b000000000000000070fb7810a938a00d884f17dc01b62eaa7dde9fbb7ab2cf4200636363
FreeRADIUS-Proxied-To = 127.0.0.1
User-Name = "ccc"
State = 0x4bb6eef44bbef48a7072f4e023895561
server inner-tunnel {
Tue Dec 29 18:38:47 2015 : Info: # Executing section authorize from file /etc/freeradius/sites-enabled/inner-tunnel
Tue Dec 29 18:38:47 2015 : Info: +group authorize {
Tue Dec 29 18:38:47 2015 : Info: ++[chap] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[mschap] = noop
Tue Dec 29 18:38:47 2015 : Info: [suffix] No '#' in User-Name = "ccc", looking up realm NULL
Tue Dec 29 18:38:47 2015 : Info: [suffix] No such realm "NULL"
Tue Dec 29 18:38:47 2015 : Info: ++[suffix] = noop
Tue Dec 29 18:38:47 2015 : Info: ++update control {
Tue Dec 29 18:38:47 2015 : Info: ++} # update control = noop
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP packet type response id 8 length 62
Tue Dec 29 18:38:47 2015 : Info: [eap] No EAP Start, assuming it's an on-going EAP conversation
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = updated
Tue Dec 29 18:38:47 2015 : Info: ++[files] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[expiration] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[logintime] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[pap] = noop
Tue Dec 29 18:38:47 2015 : Info: +} # group authorize = updated
Tue Dec 29 18:38:47 2015 : Info: Found Auth-Type = EAP
Tue Dec 29 18:38:47 2015 : Info: # Executing group from file /etc/freeradius/sites-enabled/inner-tunnel
Tue Dec 29 18:38:47 2015 : Info: +group authenticate {
Tue Dec 29 18:38:47 2015 : Info: [eap] Request found, released from the list
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP/mschapv2
Tue Dec 29 18:38:47 2015 : Info: [eap] processing type mschapv2
Tue Dec 29 18:38:47 2015 : Info: [mschapv2] # Executing group from file /etc/freeradius/sites-enabled/inner-tunnel
Tue Dec 29 18:38:47 2015 : Info: [mschapv2] +group MS-CHAP {
Tue Dec 29 18:38:47 2015 : Info: [mschap] No Cleartext-Password configured. Cannot create LM-Password.
Tue Dec 29 18:38:47 2015 : Info: [mschap] No Cleartext-Password configured. Cannot create NT-Password.
Tue Dec 29 18:38:47 2015 : Info: [mschap] Creating challenge hash with username: ccc
Tue Dec 29 18:38:47 2015 : Info: [mschap] Client is using MS-CHAPv2 for ccc, we need NT-Password
Tue Dec 29 18:38:47 2015 : Info: [mschap] FAILED: No NT/LM-Password. Cannot perform authentication.
Tue Dec 29 18:38:47 2015 : Info: [mschap] FAILED: MS-CHAP2-Response is incorrect
Tue Dec 29 18:38:47 2015 : Info: ++[mschap] = reject
Tue Dec 29 18:38:47 2015 : Info: +} # group MS-CHAP = reject
Tue Dec 29 18:38:47 2015 : Info: [eap] Freeing handler
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = reject
Tue Dec 29 18:38:47 2015 : Info: +} # group authenticate = reject
Tue Dec 29 18:38:47 2015 : Info: Failed to authenticate the user.
Tue Dec 29 18:38:47 2015 : Info: Using Post-Auth-Type REJECT
Tue Dec 29 18:38:47 2015 : Info: # Executing group from file /etc/freeradius/sites-enabled/inner-tunnel
Tue Dec 29 18:38:47 2015 : Info: +group REJECT {
Tue Dec 29 18:38:47 2015 : Info: [attr_filter.access_reject] expand: %{User-Name} -> ccc
Tue Dec 29 18:38:47 2015 : Debug: attr_filter: Matched entry DEFAULT at line 11
Tue Dec 29 18:38:47 2015 : Info: ++[attr_filter.access_reject] = updated
Tue Dec 29 18:38:47 2015 : Info: +} # group REJECT = updated
} # server inner-tunnel
Tue Dec 29 18:38:47 2015 : Info: [peap] Got tunneled reply code 3
MS-CHAP-Error = "\010E=691 R=1"
EAP-Message = 0x04080004
Message-Authenticator = 0x00000000000000000000000000000000
Tue Dec 29 18:38:47 2015 : Info: [peap] Got tunneled reply RADIUS code 3
MS-CHAP-Error = "\010E=691 R=1"
EAP-Message = 0x04080004
Message-Authenticator = 0x00000000000000000000000000000000
Tue Dec 29 18:38:47 2015 : Info: [peap] Tunneled authentication was rejected.
Tue Dec 29 18:38:47 2015 : Info: [peap] FAILURE
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = handled
Tue Dec 29 18:38:47 2015 : Info: +} # group authenticate = handled
Sending Access-Challenge of id 216 to 192.168.1.1 port 32779
EAP-Message = 0x0109002b190017030100205991bfd8f9e7f70794477d653c848e8b443626b3b935a5b3f049ac7af1534d3e
Message-Authenticator = 0x00000000000000000000000000000000
State = 0xf9775519fe7e4c9188c14494359a170f
Tue Dec 29 18:38:47 2015 : Info: Finished request 7.
Tue Dec 29 18:38:47 2015 : Debug: Going to the next request
Tue Dec 29 18:38:47 2015 : Debug: Waking up in 0.4 seconds.
rad_recv: Access-Request packet from host 192.168.1.1 port 32779, id=217, length=124
User-Name = "ccc"
State = 0xf9775519fe7e4c9188c14494359a170f
EAP-Message = 0x0209002b190017030100202a7f1a72de2970b689e44c005661d1e1e444854af7499ebeb23eabc7bfad7b64
Service-Type = Framed-User
Framed-MTU = 1420
NAS-IP-Address = 192.168.1.1
Message-Authenticator = 0xc9b0d8e268df2d8e4b484725c3efa189
Tue Dec 29 18:38:47 2015 : Info: # Executing section authorize from file /etc/freeradius/sites-enabled/default
Tue Dec 29 18:38:47 2015 : Info: +group authorize {
Tue Dec 29 18:38:47 2015 : Info: ++[preprocess] = ok
Tue Dec 29 18:38:47 2015 : Info: ++[chap] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[mschap] = noop
Tue Dec 29 18:38:47 2015 : Info: ++[digest] = noop
Tue Dec 29 18:38:47 2015 : Info: [suffix] No '#' in User-Name = "ccc", looking up realm NULL
Tue Dec 29 18:38:47 2015 : Info: [suffix] No such realm "NULL"
Tue Dec 29 18:38:47 2015 : Info: ++[suffix] = noop
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP packet type response id 9 length 43
Tue Dec 29 18:38:47 2015 : Info: [eap] Continuing tunnel setup.
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = ok
Tue Dec 29 18:38:47 2015 : Info: +} # group authorize = ok
Tue Dec 29 18:38:47 2015 : Info: Found Auth-Type = EAP
Tue Dec 29 18:38:47 2015 : Info: # Executing group from file /etc/freeradius/sites-enabled/default
Tue Dec 29 18:38:47 2015 : Info: +group authenticate {
Tue Dec 29 18:38:47 2015 : Info: [eap] Request found, released from the list
Tue Dec 29 18:38:47 2015 : Info: [eap] EAP/peap
Tue Dec 29 18:38:47 2015 : Info: [eap] processing type peap
Tue Dec 29 18:38:47 2015 : Info: [peap] processing EAP-TLS
Tue Dec 29 18:38:47 2015 : Info: [peap] eaptls_verify returned 7
Tue Dec 29 18:38:47 2015 : Info: [peap] Done initial handshake
Tue Dec 29 18:38:47 2015 : Info: [peap] eaptls_process returned 7
Tue Dec 29 18:38:47 2015 : Info: [peap] EAPTLS_OK
Tue Dec 29 18:38:47 2015 : Info: [peap] Session established. Decoding tunneled attributes.
Tue Dec 29 18:38:47 2015 : Info: [peap] Peap state send tlv failure
Tue Dec 29 18:38:47 2015 : Info: [peap] Received EAP-TLV response.
Tue Dec 29 18:38:47 2015 : Info: [peap] The users session was previously rejected: returning reject (again.)
Tue Dec 29 18:38:47 2015 : Info: [peap] *** This means you need to read the PREVIOUS messages in the debug output
Tue Dec 29 18:38:47 2015 : Info: [peap] *** to find out the reason why the user was rejected.
Tue Dec 29 18:38:47 2015 : Info: [peap] *** Look for "reject" or "fail". Those earlier messages will tell you.
Tue Dec 29 18:38:47 2015 : Info: [peap] *** what went wrong, and how to fix the problem.
Tue Dec 29 18:38:47 2015 : Info: [eap] Handler failed in EAP/peap
Tue Dec 29 18:38:47 2015 : Info: [eap] Failed in EAP select
Tue Dec 29 18:38:47 2015 : Info: ++[eap] = invalid
Tue Dec 29 18:38:47 2015 : Info: +} # group authenticate = invalid
Tue Dec 29 18:38:47 2015 : Info: Failed to authenticate the user.
Tue Dec 29 18:38:47 2015 : Info: Using Post-Auth-Type REJECT
Tue Dec 29 18:38:47 2015 : Info: # Executing group from file /etc/freeradius/sites-enabled/default
Tue Dec 29 18:38:47 2015 : Info: +group REJECT {
Tue Dec 29 18:38:47 2015 : Info: [sql] expand: %{User-Name} -> ccc
Tue Dec 29 18:38:47 2015 : Info: [sql] sql_set_user escaped user --> 'ccc'
Tue Dec 29 18:38:47 2015 : Info: [sql] expand: %{User-Password} ->
Tue Dec 29 18:38:47 2015 : Info: [sql] ... expanding second conditional
Tue Dec 29 18:38:47 2015 : Info: [sql] expand: %{Chap-Password} ->
Tue Dec 29 18:38:47 2015 : Info: [sql] expand: INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( '%{User-Name}', '%{%{User-Password}:-%{Chap-Password}}', '%{reply:Packet-Type}', '%S') -> INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( 'ccc', '', 'Access-Reject', '2015-12-29 18:38:47')
Tue Dec 29 18:38:47 2015 : Debug: rlm_sql (sql) in sql_postauth: query is INSERT INTO radpostauth (username, pass, reply, authdate) VALUES ( 'ccc', '', 'Access-Reject', '2015-12-29 18:38:47')
Tue Dec 29 18:38:47 2015 : Debug: rlm_sql (sql): Reserving sql socket id: 29
Tue Dec 29 18:38:47 2015 : Debug: rlm_sql (sql): Released sql socket id: 29
Tue Dec 29 18:38:47 2015 : Info: ++[sql] = ok
Tue Dec 29 18:38:47 2015 : Info: [attr_filter.access_reject] expand: %{User-Name} -> ccc
Tue Dec 29 18:38:47 2015 : Debug: attr_filter: Matched entry DEFAULT at line 11
Tue Dec 29 18:38:47 2015 : Info: ++[attr_filter.access_reject] = updated
Tue Dec 29 18:38:47 2015 : Info: +} # group REJECT = updated
Tue Dec 29 18:38:47 2015 : Info: Delaying reject of request 8 for 1 seconds
Tue Dec 29 18:38:47 2015 : Debug: Going to the next request
Tue Dec 29 18:38:47 2015 : Debug: Waking up in 0.1 seconds.
Tue Dec 29 18:38:47 2015 : Info: Cleaning up request 0 ID 209 with timestamp +11
Tue Dec 29 18:38:47 2015 : Debug: Waking up in 0.3 seconds.
Tue Dec 29 18:38:47 2015 : Info: Cleaning up request 1 ID 210 with timestamp +11
Tue Dec 29 18:38:47 2015 : Debug: Waking up in 0.3 seconds.
Tue Dec 29 18:38:48 2015 : Info: Cleaning up request 2 ID 211 with timestamp +12
Tue Dec 29 18:38:48 2015 : Debug: Waking up in 0.1 seconds.
Tue Dec 29 18:38:48 2015 : Info: Sending delayed reject for request 8
Sending Access-Reject of id 217 to 192.168.1.1 port 32779
EAP-Message = 0x04090004
Message-Authenticator = 0x00000000000000000000000000000000
Found the solution,
the problem was that i didn't configure the /etc/raddb/sites-available/inner-tunnel file to use sql

First program in bash pipeline is blocking

I'm trying to pipe several perl programs together, and from everything I've read, piped programs are supposed to all open and run in parallel. That doesn't appear to be the case with whatever it is that I did. Here is the simplified version:
./video
#!/usr/bin/env perl
my $i = 0;
while($i<10){
my $time = localtime(time);
print "VID: $time $i\n";
sleep 1;
$i++;
}
./controller
#!/usr/bin/env perl
while(my $line = <STDIN>){
my $time = localtime(time);
print "CTRL: $time $line";
}
./pipes
#!/usr/bin/env perl
open(my $controller, "|./controller") || die "Can't fork: $!";
open(STDOUT, ">&", $controller);
open(my $video, "|./video") || die "Can't fork: $!";
print STDERR "All processes started\n";
I tried executing it two ways, but both resulted in the same output:
<16:34:21> rswhiting#Minas-Tirith:~/code/Pipes $ ./pipes
All processes started
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:22 2014 0
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:23 2014 1
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:24 2014 2
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:25 2014 3
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:26 2014 4
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:27 2014 5
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:28 2014 6
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:29 2014 7
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:30 2014 8
CTRL: Sun Apr 20 16:34:32 2014 VID: Sun Apr 20 16:34:31 2014 9
<16:34:49> rob#Minas-Tirith:~/code/Pipes $ ./video | ./controller
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:15 2014 0
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:16 2014 1
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:17 2014 2
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:18 2014 3
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:19 2014 4
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:20 2014 5
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:21 2014 6
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:22 2014 7
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:23 2014 8
CTRL: Sun Apr 20 16:35:25 2014 VID: Sun Apr 20 16:35:24 2014 9
<16:35:25> rob#Minas-Tirith:~/code/Pipes $
The timestamps from the video program increment as they should, but the time that the control program receives them is identical (after the video program has ended). How do I make the controller receive the data as it is produced instead of after the fact?
It's because of STDOUT buffering in the video program. print is line-buffered only if it goes to an interactive terminal. Otherwise it uses a much bigger buffer, and the accumulated output is emitted in one big chunk when video exits. Try flushing after each print or use autoflush.
$| = 1; # Enables autoflush
By the way I don't get why you open a pipe TO video, it does not read anything. It should be
system "./video";
If you have a program that you can't modify but does not autoflush, you are in trouble. Except, you're not. You can use a pseudoterminal instead of a pipe. I've never done it. But chapter 64 of the book "The UXIX Programming Interface" is about pseudoterminals.

How to get first day of last month with Perl

So I would like to use function localtime(), but I'm having problems with getting first and last day of last month properly. Right now I have working functionality, but I bet there is a better way to solve this.
use Time::Piece;
use Time::Seconds;
$start_of_month = localtime();
while($start_of_month->mday < 10) {
$start_of_month += ONE_DAY;
}
$start_of_month -= ONE_MONTH; # Subtract one month to get previous month. "ONE_MONTH" is defined by Time::Seconds
$end_of_month = $start_of_month; # Copy start_of_month to end_of_month as they both have same year and month.
# Subtract day from $start_of_month until mday is the first day of the month.
while($start_of_month->mday != 1) {
$start_of_month -= ONE_DAY;
}
# Silly workaround to bring $end_of_month to last day of the month as Time::Piece object does not have good way to change mday.
while($end_of_month->mday != $start_of_month->month_last_day) {
$end_of_month += ONE_DAY;
}
$period_start = $start_of_month->dmy("."); # End result has to be same!
Can anyone give me a better way of handling this?
Don't rely on Time::Piece and constants in Time::Seconds for date manipulation
The following was my suggested simplification of the OP's date manipulation using Time::Piece and Time::Seconds. IF this worked on overloaded operators, it might actually work as intended, but as ikegami pointed out, this isn't guaranteed. So I tested below.
If you want to go the math route, at least you could simplify things a little:
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $date_start = localtime();
$date_start -= ONE_MONTH;
$date_start -= ($date_start->mday - 1) * ONE_DAY;
my $date_end = $date_start + ($date_start->month_last_day - 1) * ONE_DAY;
print $date_start , "\n"; # Prints Mar 1st (at least today it does)
print $date_end , "\n"; # Prints Mar 31st
I'd consider using a different module for this, like Date::Calc or DateTime, but this might work for your purposes.
Testing the above solution - many failures
I created a script the below script that loops through every date for a year starting at January 15th, showing every range where the above code does not work as expected.
use strict;
use warnings;
use Time::Piece;
use Time::Seconds;
my $t = 1389812400; # Wed 2014-Jan-15 7pm GMT. 11am PST
my $t_max = 1421348400; # Thu 2015-Jan-15 7pm GMT. 11am PST
my %prev_month = map {$_ => ($_ - 1) || 12} (1..12);
my $fail = '';
while ($t <= $t_max) {
$t += 60; # Increment by 1 minute
# Testing potentially overloaded math of Time::Piece & Time::Seconds
my $start = my $src = localtime($t);
$start -= ONE_MONTH;
$start -= ($start->mday - 1) * ONE_DAY;
if ($start->mon != $prev_month{$src->mon}) {
print "From ($t) $src -> $start\n" if !$fail;
$fail = " To ($t) $src -> $start\n\n";
} elsif ($fail) {
print $fail;
$fail = '';
}
}
The below is the output of this script with comments inserted to explain why each range fails:
# ONE_MONTH is exactly 2_629_744 seconds, or 30.437 days.
# ONE_MONTH is too short for January
From (1391193000) Fri Jan 31 10:30:00 2014 -> Wed Jan 1 00:00:56 2014
To (1391241540) Fri Jan 31 23:59:00 2014 -> Wed Jan 1 13:29:56 2014
# ONE_MONTH is too long for February
From (1393660800) Sat Mar 1 00:00:00 2014 -> Wed Jan 1 13:30:56 2014
To (1393871340) Mon Mar 3 10:29:00 2014 -> Wed Jan 1 23:59:56 2014
# ONE_MONTH is too short for March
From (1396290600) Mon Mar 31 11:30:00 2014 -> Sat Mar 1 00:00:56 2014
To (1396335540) Mon Mar 31 23:59:00 2014 -> Sat Mar 1 12:29:56 2014
# ONE_DAY is 86_400 seconds, or 24 hours.
# March 9th is only 23 hours long due to DST, ONE_DAY goes to far.
From (1397064600) Wed Apr 9 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397068140) Wed Apr 9 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397151000) Thu Apr 10 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397154540) Thu Apr 10 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397237400) Fri Apr 11 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397240940) Fri Apr 11 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397323800) Sat Apr 12 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397327340) Sat Apr 12 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397410200) Sun Apr 13 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397413740) Sun Apr 13 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397496600) Mon Apr 14 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397500140) Mon Apr 14 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397583000) Tue Apr 15 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397586540) Tue Apr 15 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397669400) Wed Apr 16 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397672940) Wed Apr 16 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397755800) Thu Apr 17 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397759340) Thu Apr 17 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397842200) Fri Apr 18 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397845740) Fri Apr 18 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1397928600) Sat Apr 19 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1397932140) Sat Apr 19 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398015000) Sun Apr 20 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398018540) Sun Apr 20 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398101400) Mon Apr 21 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398104940) Mon Apr 21 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398187800) Tue Apr 22 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398191340) Tue Apr 22 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398274200) Wed Apr 23 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398277740) Wed Apr 23 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398360600) Thu Apr 24 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398364140) Thu Apr 24 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398447000) Fri Apr 25 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398450540) Fri Apr 25 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398533400) Sat Apr 26 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398536940) Sat Apr 26 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398619800) Sun Apr 27 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398623340) Sun Apr 27 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398706200) Mon Apr 28 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398709740) Mon Apr 28 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398792600) Tue Apr 29 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398796140) Tue Apr 29 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
From (1398879000) Wed Apr 30 10:30:00 2014 -> Fri Feb 28 23:00:56 2014
To (1398882540) Wed Apr 30 11:29:00 2014 -> Fri Feb 28 23:59:56 2014
# ONE_MONTH is too long for April
From (1398927600) Thu May 1 00:00:00 2014 -> Sat Mar 1 12:30:56 2014
To (1398965340) Thu May 1 10:29:00 2014 -> Sat Mar 1 22:59:56 2014
# ONE_MONTH is too short for May
From (1401557400) Sat May 31 10:30:00 2014 -> Thu May 1 00:00:56 2014
To (1401605940) Sat May 31 23:59:00 2014 -> Thu May 1 13:29:56 2014
# ONE_MONTH is too long for June
From (1404198000) Tue Jul 1 00:00:00 2014 -> Thu May 1 13:30:56 2014
To (1404235740) Tue Jul 1 10:29:00 2014 -> Thu May 1 23:59:56 2014
# ONE_MONTH is too short for July
From (1406827800) Thu Jul 31 10:30:00 2014 -> Tue Jul 1 00:00:56 2014
To (1406876340) Thu Jul 31 23:59:00 2014 -> Tue Jul 1 13:29:56 2014
# ONE_MONTH is too short for August
From (1409506200) Sun Aug 31 10:30:00 2014 -> Fri Aug 1 00:00:56 2014
To (1409554740) Sun Aug 31 23:59:00 2014 -> Fri Aug 1 13:29:56 2014
# ONE_MONTH is too long for September
From (1412146800) Wed Oct 1 00:00:00 2014 -> Fri Aug 1 13:30:56 2014
To (1412184540) Wed Oct 1 10:29:00 2014 -> Fri Aug 1 23:59:56 2014
# ONE_MONTH is too short for October
From (1414776600) Fri Oct 31 10:30:00 2014 -> Wed Oct 1 00:00:56 2014
To (1414825140) Fri Oct 31 23:59:00 2014 -> Wed Oct 1 13:29:56 2014
# ONE_MONTH is too long for November
From (1417420800) Mon Dec 1 00:00:00 2014 -> Wed Oct 1 14:30:56 2014
To (1417454940) Mon Dec 1 09:29:00 2014 -> Wed Oct 1 23:59:56 2014
# ONE_MONTH is too short for December
From (1420050600) Wed Dec 31 10:30:00 2014 -> Mon Dec 1 00:00:56 2014
To (1420099140) Wed Dec 31 23:59:00 2014 -> Mon Dec 1 13:29:56 2014
Alternative using Time::Piece->add_months? Nope
Time::Piece has two functions, add_months and add_years intended for date calculations. Unfortunately, the documentation states:
Note that there is some "strange" behaviour when adding and subtracting months at the ends of months. Generally when the resulting month is shorter than the starting month then the number of overlap days is added. For example subtracting a month from 2008-03-31 will not result in 2008-02-31 as this is an impossible date. Instead you will get 2008-03-02. This appears to be consistent with other date manipulation tools.
The below code fully demonstrates this behavior for the timezone PST.
use strict;
use warnings;
use Time::Piece;
my $t = localtime(1420012800);
print $t->add_months($_),"\n" for (0..12)
Output:
Wed Dec 31 00:00:00 2014
Sat Jan 31 00:00:00 2015
Tue Mar 3 00:00:00 2015
Tue Mar 31 00:00:00 2015
Fri May 1 00:00:00 2015
Sun May 31 00:00:00 2015
Wed Jul 1 00:00:00 2015
Fri Jul 31 00:00:00 2015
Mon Aug 31 00:00:00 2015
Thu Oct 1 00:00:00 2015
Sat Oct 31 00:00:00 2015
Tue Dec 1 00:00:00 2015
Thu Dec 31 00:00:00 2015
Now, it is kind of nice how this function is able to accurately cycle back to the same date a year later, but there are lots of unfavorable months until then.
Conclusion
The constants in Time::Seconds are just that, constants. They are exact numbers of seconds meant to represent periods of time. There is no operator overloading to facilitate fancy date manipulation. Instead, these values are simply good for mathematical comparisons.
To manipulate specific dates, I'd advise using Date::Calc, DateTime, or any of the other modules suggested in this question.
A less OO, but alternate solution:
use POSIX qw(mktime);
my #now = localtime;
# mday = 1, month = month - 1
my $date_start = mktime(#now[0 .. 2], 1, $now[4] - 1, #now[5 .. 8]);
# mday = 0, the 0th day of this month == last day of prior month
my $date_end = mktime(#now[0 .. 2], 0, #now[4 .. 8]);
print scalar localtime $date_start, "\n";
print scalar localtime $date_end, "\n";
This hiccups with daylight saving time; if you don't care about time-of-day you could set $now[2] to 12 noon and hope no country declares night to be day and day to be night:
my #now = (0, 0, 12, (localtime)[3..8]);
I don't trust code that uses Time::Piece. It relies on overloading operators to make it look like you're doing it wrong ($time += ONE_DAY; looks like you're adding a constant) when you're actually doing it right. Maybe. It's hard to tell. You must have intimate knowledge of the guts of the module to know if you're doing it right.
DateTime solution:
my $dt = DateTime
->now( time_zone => 'local' )
->set_time_zone('floating') # Do this when you want to date arithmetic.
->truncate( to => 'day' );
$dt->set( day => 1 )->subtract( days => 1 );
my $last = $dt->ymd('-');
$dt->set( day => 1 );
my $first = $dt->ymd('-');
say "$first .. $last";
Using the Time::Seconds definition of ONE_MONTH gives you a solution that is wrong for several days a year. It is the average length of a month in seconds, and is equal to about 30.4 days. Subtracting that from 1 March, for instance, gives you a date in late January.
However the first day of the previous month can be calculated simply from the month and day fields returned by localtime. Converting this into a Time::Piece object lets us calculate the last day of the same month.
use strict;
use warnings;
use Time::Piece;
my ($m, $y) = (localtime)[4,5];
$y += 1900;
if ($m == 0) {
$m = 11;
$y -= 1;
}
my $period_start = sprintf '%02d.%02d.%04d', 1, $m, $y;
my $period_end = do {
my $tp = Time::Piece->strptime($period_start, '%d.%m.%Y');
sprintf '%02d.%02d.%04d', $tp->month_last_day, $m, $y;
};
print $period_start, "\n";
print $period_end, "\n";
output
01.03.2014
31.03.2014
Sorry if I did not understood property but: DaysInMonth (check coment)
my #monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 );
sub MonthDays {
my $month= shift(#_);
my $year= #_ ? shift(#_) : 1900+(localtime())[5];
if( $year <= 1752 ) {
# Note: Although September 1752 only had 19 days,
# they were numbered 1,2,14..30!
return 19 if 1752 == $year && 9 == $month;
return 29 if 2 == $month && 0 == $year % 4;
} else {
return 29 if 2 == $month and
0 == $year%4 && 0 != $year%100 || 0 == $year%400;
}
return $monthDays[$month-1];
}
If you wish easy data to seconds then take look Time::Local
$time = timelocal( $sec, $min, $hour, $mday, $mon, $year );