LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2006, 02:37 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
What's wrong with this perl script


Output from
#pfctl -si

Status: Enabled for 4 days 02:45:36 Debug: Urgent

Hostid: 0x75952477

Interface Stats for rl0 IPv4 IPv6
Bytes In 165733705 0
Bytes Out 28725638 0
Packets In
Passed 260360 0
Blocked 107994 0
Packets Out
Passed 167002 0
Blocked 780 0

State Table Total Rate
current entries 7
searches 1040683 2.9/s
inserts 29185 0.1/s
removals 29178 0.1/s
Counters
match 574624 1.6/s
bad-offset 0 0.0/s
fragment 0 0.0/s
short 0 0.0/s
normalize 0 0.0/s
memory 0 0.0/s
bad-timestamp 0 0.0/s
congestion 0 0.0/s
ip-option 0 0.0/s
proto-cksum 0 0.0/s
state-mismatch 7 0.0/s
state-insert 0 0.0/s
state-limit 0 0.0/s
src-limit 0 0.0/s
synproxy 0 0.0/s

--------------
This script is not getting any value from above file

#!/usr/bin/perl
my($inp,$outp) = (0,0);
if ($ARGV[0] eq "pass") {
foreach (`/sbin/pfctl -si`) {
if (/Packets In/) { $in = 1; $out = 0; }
if (/Packets Out/) { $in = 0; $out = 1; }
if (/Passeds+(d+)/) { if ($in) { $inp += $1; } }
if (/Passeds+(d+)/) { if ($out) { $outp += $1; } }
}
}

elsif ($ARGV[0] eq "blocked") {
foreach (`/sbin/pfctl -si`) {
if (/Packets In/) { $in = 1; $out = 0; }
if (/Packets Out/) { $in = 0; $out = 1; }
if (/Blockeds+(d+)/) { if ($in) { $inp += $1; } }
if (/Blockeds+(d+)/) { if ($out) { $outp += $1; } }
}
}

elsif ($ARGV[0] eq "bytes") {
foreach (`/sbin/pfctl -si`) {
$outp += $1 if (/^s+Bytes.+Ins+(d+)/);
$inp += $1 if (/^s+Bytes.+Outs+(d+)/);
}
}

elsif ($ARGV[0] eq "statecurrent") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+current entriess+(d+)/);
$outp = 0;
}
}

elsif ($ARGV[0] eq "searches") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+searchess+(d+)/);
$outp += $1 if (/^s+searchess+d+s+(d+)/);
}
}

elsif ($ARGV[0] eq "inres") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+insertss+(d+)/);
$outp += $1 if (/^s+removalss+(d+)/);
}
}

elsif ($ARGV[0] eq "match") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+matchs+(d+)/);
$outp += $1 if (/^s+matchs+d+s+(d+)/);
}
}

elsif ($ARGV[0] eq "badfragment") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+bad-offsets+(d+)/);
$outp += $1 if (/^s+fragments+(d+)/);
}
}

elsif ($ARGV[0] eq "shortnormal") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+shorts+(d+)/);
$outp += $1 if (/^s+normalizes+(d+)/);
}
}

elsif ($ARGV[0] eq "memory") {
foreach (`/sbin/pfctl -si`) {
$inp += $1 if (/^s+memorys+(d+)/);
$outp += $1 if (/^s+memorys+d+s+(d+)/);
}
}

else { die "usage: pf pass|blocked|bytes|statecurrent|searches|inres|match|badfragment|shortnormal|memoryn"; }

print "$inp\n",
"$outp\n";
 
Old 11-01-2006, 03:35 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
your regexes are all wrong.

eg:
Code:
$outp += $1 if (/^s+Bytes.+Ins+(d+)/);
# better as
$outp += $1 if (/^Bytes\s+In\s+(\d+)/);
 
Old 11-01-2006, 04:22 AM   #3
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Hello billy

After modifying the regex as you have mentioed I am not able to get the Bytes In and Bytes Out values

Interface Stats for rl0 IPv4 IPv6
Bytes In 168784757 0
Bytes Out 29259169 0
Packets In
Passed 264166 0
Blocked 112768 0
Packets Out
Passed 170725 0
Blocked 780 0


elsif($ARGV[0] eq "bytes"){
foreach (`/sbin/pfctl -si`){
$outp += $1 if(/^Bytes\s+In\s+(\d+)/);
$inp += $1 if(/^Bytes\s+Out\s+(\d+)/);
}
}
print "$outp\n";
print "$inp\n";
 
Old 11-01-2006, 05:43 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
try this simplified a bit

Code:
#!/bin/perl -ws

$debug ||= 0;

$command = "/sbin/pfctl -si";


# slurp the command as one long line
# ==================================
$_ = join '', qx($command);

$debug and print ;  # use -debug on command line

# do all checks here
# ===================


if ($ARGV[0] eq "bytes" ) {
    ($in) =  /^Bytes\s+In\s+(\d+)/m ;
    ($out) = /^Bytes\s+Out\s+(\d+)/m ;
    print "$in\n$out\n";

}

# etc etc
 
Old 11-01-2006, 06:01 AM   #5
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
This is the output of your scripts

Status:: not found
Hostid:: not found
Interface: not found
Bytes: not found
Bytes: not found
Packets: not found
Passed: not found
Blocked: not found
Packets: not found
Passed: not found
Blocked: not found
State: not found
current: not found
searches: not found
inserts: not found
removals: not found
Counters: not found
match: not found
bad-offset: not found
fragment: not found
short: not found
normalize: not found
memory: not found
bad-timestamp: not found
congestion: not found
ip-option: not found
proto-cksum: not found
state-mismatch: not found
state-insert: not found
state-limit: not found
src-limit: not found
synproxy: not found
 
Old 11-01-2006, 06:53 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
oops,

#!/usr/bin/perl


maybe?
 
Old 11-01-2006, 08:35 AM   #7
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Yes kept "#!/usr/bin/perl" but still not getting any data.
#!/usr/bin/perl

$debug ||=0;
$command = `/sbin/pfctl -si`;

$_ = join '', qx($command);

$debug and print;

if ($ARGV[0] eq "bytes" ) {
($in) = /^Bytes\s+In\s+(\d+)/m ;
($out) = /^Bytes\s+Out\s+(\d+)/m ;
print "$in\n$out\n";

}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: what am I doing wrong? cramer Programming 2 08-15-2006 11:00 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Whats wrong with this PERL script? VisionZ Linux - Newbie 25 03-25-2004 08:58 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration