Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6883397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:22:43+00:00 2026-05-27T05:22:43+00:00

I have a perl script that reads our firewall access logs to see who

  • 0

I have a perl script that reads our firewall access logs to see who has used a specific vpn account. This works fine when run from the command line, but fails when run from crontab. The code is here

#!/usr/bin/perl -w
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my $basedir = "/var/log";
my @verdir = qw(fw_d);
my $fulldir;
my $configs; 
my $combidir;
my @results;
my @files1;
foreach $combidir (@verdir) {
    $fulldir = "$basedir/$combidir";
    opendir (DIR, $fulldir);
    my @files = grep { $_ ne '.' && $_ ne '..' && $_ ne 'CVS' } readdir DIR;
    closedir (DIR);
    @files1 = sort {expand($a)cmp expand($b)}(@files);
    foreach my $configs (@files1) {
        my $now = time;
        my @stat = stat("$fulldir/$configs");
        if ($stat[9] > ($now - 2592000)) {
            system("/usr/bin/less -f $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log"); }
        }
    }
    results();
    sendmailnew(\@results);

    sub results{
        my $input = "/usr/local/CCS/ravpn/output.log";
        open my $fh, "<", "$input" or die "could not open '$input': $!";
        while (<$fh>){
            if ($_ =~ /(................)fw.*(Group = NETOPS_TUNNEL). (Username = .(authenticated.)/){
                push (@results, "$1 $2 $3 $4<br>")
            }
        }
        return @results
    }

    sub expand  { 
        my $file=shift; 
        $file=~s{(\d+)}{sprintf "%04d", $1}eg; # expand all numbers to 4 digits
        return $file;
    }

    sub sendmailnew {
        my %params; 
        my @results = @{$_[0]};
        $params{sorted} = "@results";
        my %options; 
        $options{INCLUDE_PATH} = '/usr/local/CCS/ravpn/templates/'; 
        my $msg = MIME::Lite::TT::HTML->new( 
            From        =>  "",
            #To          =>  "", 
            BCC                 =>  "",
            Subject     =>  "", 
            Encoding    => 'quoted-printable',
            Template    =>  {
                text    =>  'test.txt.tt',
                html    =>  'sort.html.tt',
            },
            TmplOptions =>  \%options, 
            TmplParams  =>  \%params, 
        ); 

        $msg->send;
        system("rm -rf /usr/local/CCS/ravpn/output.log");
    }

When this is run from the command line, it goes to the directory /var/log/fw_d, gets all entries that are less than 30 days old, and passes them to have less and grep run against them, the results are outputted to output.log. (this file is created when it is run via crontab, but nothing is ever outputted). When run from the command line, entries are added to the output.log

Once the output.log is created, that is scanned for a specific set of items, and the results are pushed into an array called results, which is then emailed out.

This script works great if run manually, but if run from crontab, either the user we use to create the logs, or the root user, it fails to output anything to the output.log file.

I have a feeling its going to be something simple, but im not sure.

this is the crontab -e entry

  40 03 * * * perl /usr/local/CCS/ravpn/ravpn.pl    

The files that are being read are either plain text, or zipped text files, .gz, so i needed a way of reading them without unzipping hence the use of less.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:22:43+00:00Added an answer on May 27, 2026 at 5:22 am

    Try to replace

    system("/usr/bin/less -f $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log");
    

    with

    system("/bin/cat $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log");
    

    Less is not meant to run without terminal. There is no need to do that in your case. Even better if you do this operation inside perl without system call using perl own functions:

    if ($configs =~ /\.gz$/) {
        open (F1, "gunzip -c $fulldir/$configs |");
    } else {
        open (F1, "$fulldir/$configs");
    }
    open (F2, ">> /usr/local/CCS/ravpn/output.log");
    while (<F1>) {
        print F2 $_ if (/NETOPS_TUNNEL/);
    }
    close F2;
    close F1;
    

    PS: I see that you use /usr/local/CCS/ravpn/output.log as a temporary file. There is no need for that file based on what this script does. It makes your script even simpler.

    Edit: works with .gz files too. You can use IO::Uncompress::Gunzip also but this is more complicated already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Perl script that reads some data and generates an OpenOffice
I have a Perl script that reads a command file and restarts itself if
I have a perl script that is used to monitor databases and I'm trying
I have a Perl script that reads data from an Excel ( xls )
I have a Perl script that reads a XML file that doesn't have content,
I have a Perl script that uses WWW::Mechanize to read from a file and
I have a Perl script that I'm attempting to set up using Perl Threads
I have a Perl script that sets up variables near the top for directories
I have a Perl script that I'd like to run on Windows, using either
I have a Perl script that pops up a message box when its work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.