Skip to content

Instantly share code, notes, and snippets.

@ChatchaiJ
Created January 14, 2020 09:36
Show Gist options
  • Save ChatchaiJ/39c74726c1e909bd1da9868c38261c26 to your computer and use it in GitHub Desktop.
Save ChatchaiJ/39c74726c1e909bd1da9868c38261c26 to your computer and use it in GitHub Desktop.
Send alert message using telegram message when someone connect to anyconnect VPN
#!/usr/bin/perl -w-
use strict;
use warnings;
my $DEBUG = 0;
my $RUNFILE = "/var/tmp/ftd-logmon.run";
my $ALRT = "ALRT";
$ALRT = "CJ" if ($DEBUG);
sub init() {
if ( -f $RUNFILE ) {
open my $f, '<', "$RUNFILE" or die "Can't open $RUNFILE";
my $pid = <$f>;
chomp($pid);
close $f;
die "Another process [ $pid ] is running" if -d "/proc/$pid";
}
my $pid = fork();
if ($pid != 0) {
open my $f, '>', "$RUNFILE" or die "Can't open $RUNFILE : $!";
print $f "$pid";
close $f;
exit;
}
}
sub close_and_exit() {
print "Terminated\n";
exit(0);
}
sub send_message() {
my ($mesg) = @_;
print "send: $mesg\n" if $DEBUG;
system("/home/cj/bin/bot_sendmsg2 $ALRT '$mesg'");
}
$SIG{USR1} = sub(){ exit(0); };
### Start Here ###
&init();
my $logfile = "/var/log/cisco-ftd.log";
$logfile = $ARGV[0] if defined($ARGV[0]);
my $sz = (stat $logfile)[7];
# $sz = 0 if $DEBUG;
open my $f, '<', "$logfile";
while (1) {
my $newsz = (stat $logfile)[7];
if ($newsz == $sz) {
sleep(1);
} else {
if ($newsz < $sz) {
&send_message("Size reduced, log file reopen");
close($f);
open $f, '<', "$logfile";
} else {
while (<$f>) {
next unless /%FTD-auth-6-113039|%FTD-auth-4-113019|%FTD-session-6-302021/;
if (/%FTD-auth-6-113039/) {
my ($tstamp, $user, $ip) = (/^(.{15}).+User <(.+?)> IP <(.+?)>/);
&send_message("$tstamp, $user, $ip\n");
}
if (/%FTD-auth-4-113019/) {
my ($tstamp, $user, $ip, $duration, $xmt, $rcv, $reason) =
(/^(.{15}).+Username = (.+?), IP = (.+?),.+ Duration: (.+?),.+ xmt: (.+),.+ rcv: (.+?), Reason: (.+)$/);
&send_message("$tstamp, $user, $ip, $duration, $xmt, $rcv, $reason");
}
# for debug only
if ($DEBUG && /%FTD-session-6-302021/) {
my ($faddr,$gaddr,$laddr) = (/faddr ([0-9.]+).+gaddr ([0-9.]+).+laddr ([0-9.]+)/);
&send_message("$faddr, $gaddr, $laddr");
}
}
}
$sz = $newsz;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment