Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

combine perl/tk and file::tail::select

by terrance (Initiate)
on Apr 03, 2011 at 22:20 UTC ( [id://897246]=perlquestion: print w/replies, xml ) Need Help??

terrance has asked for the wisdom of the Perl Monks concerning the following question:


How can i combine both?

This code I wish to put on `push_Start` sub function and will display the `$__->read` to the `$txt1` widget.
How should i make both or them work together?

And the `for foreach (@pending)` part, i just want to get the IP address and port? This condition :

if (@pending =~ /((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5}))/)0

does not make it work?

I'm summary my code

I had try two way but it does not work... `$_->read` isn't a file handle at sub push_button so how to correct it? when i click the Start Button it should be print out the result on text area.
#!/usr/local/bin/perl use Tk; use File::Tail; <br> #Main Window<br/> <br> my $mw = new MainWindow;<br/> <br> $mw-> title ("Packet Analyzer Tool");<br/> <br> my $frm_4 = $frm_3 -> Frame(-relief => 'groove', -borderwidth +=>2) -> pack(-side => "left");<br/> <br> my $but1 = $frm_1 -> Button(-text => "Start", -command =>\&push_start) -> pack(-side => "left", -anchor => 'nw', -ipadx => 30, -i +pady => 35);<br/> <br> my $txt1 = $frm_4 -> Text(-width => 60, -height =>20,-state => + "disable") -> pack(-side =>"left",-anchor => 's'); my $srl = $frm_4 -> Scrollbar(-orient=>'v', -command =>[yview => $ +txt]);<br/> <br> $txt1 -> configure(-yscrollcommand =>['set',$srl]);<br/> <br> #$txt -> insert('end', "XXX"); //display text<br/> <br> $txt1 -> grid(-row=>1, -column=>1);<br/> <br> $srl -> grid(-row=>1, -column=>2,-sticky=>"ns");<br/> <br>MainLoop;<br/> <br> #Executed START BUTTON<br/> <br> sub push_start<br/> <br> {<br/> <br> chdir( "/var/log/snort");<br/> <br> foreach my $fol(glob "*.*.*.*")<br/> <br> {<br/> <br> print "Opening $fol\n";<br/> <br> chdir("/var/log/snort/$fol");<br/> <br>foreach my $subfile(glob "*:*")<br/> <br>{<br/> <br>print "opening $subfile\n";<br/> <br> push(@files,File::Tail->new(name=>"$subfile",debug=>$d +ebug));<br/> <br> }<br/> <br> while (1)<br/> <br> {<br/> <br> ($nfound,$timeleft,@pending)= File::Tail::select(undef +,undef,undef,$timeout,@files);<br/> <br> unless ($nfound)<br/> <br> {<br/> <br> print "Nothing to print \n"; <br/><br> } <br/> <br> else <br/> <br> {<br/> <br> foreach(@pending)<br/> <br> {<br/> <br>$txt1 ->insert('end', $_->read);<br/> <br> }<br/> <br>}<br/> <br>}<br/> <br>}<br/> }

The second way is wrote as below, `tie *STDOUT ref $txt1, $txt1;` i refer to this documentation(http://search.cpan.org/~srezic/Tk-804.029/pod/Text.pod#___top) But it does not work either.
#!/usr/local/bin/perl <br> use Tk;<br/> <br> use File::Tail;<br/> <br> #Main Window<br/> <br> my $mw = new MainWindow;<br/> <br> $mw-> title ("Packet Analyzer Tool");<br/> <br> my $frm_4 = $frm_3 -> Frame(-relief => 'groove', -borderwidth +=>2) -> pack(-side => "left");<br/> <br> my $but1 = $frm_1 -> Button(-text => "Start", -command =>\&push_start) -> pack(-side => "left", -anchor => 'nw', -ipadx => 30, -i +pady => 35);<br/> <br> my $txt1 = $frm_4 -> Text(-width => 60, -height =>20,-state => + "disable") -> pack(-side =>"left",-anchor => 's'); my $srl = $frm_4 -> Scrollbar(-orient=>'v', -command =>[yview => $ +txt]);<br/> <br> $txt1 -> configure(-yscrollcommand =>['set',$srl]);<br/> <br> #$txt -> insert('end', "XXX"); //display text<br/> <br> $txt1 -> grid(-row=>1, -column=>1);<br/> <br> $srl -> grid(-row=>1, -column=>2,-sticky=>"ns");<br/> <br>MainLoop;<br/> <br> #Executed START BUTTON<br/> <br> sub push_start<br/> <br> {<br/> <br> chdir( "/var/log/snort");<br/> <br> foreach my $fol(glob "*.*.*.*")<br/> <br> {<br/> <br> print "Opening $fol\n";<br/> <br> chdir("/var/log/snort/$fol");<br/> <br>foreach my $subfile(glob "*:*")<br/> <br>{<br/> <br>print "opening $subfile\n";<br/> <br> push(@files,File::Tail->new(name=>"$subfile",debug=>$d +ebug));<br/> <br> }<br/> <br> while (1)<br/> <br> {<br/> <br> ($nfound,$timeleft,@pending)= File::Tail::select(undef +,undef,undef,$timeout,@files);<br/> <br> unless ($nfound)<br/> <br> {<br/> <br> print "Nothing to print \n"; <br/><br> } <br/> <br> else <br/> <br> {<br/> <br> foreach(@pending)<br/> <br> {<br/> <br>tie *STDOUT ref $txt1, $txt1;<br/> <br>print $_->read;<br/> <br> }<br/> <br>}<br/> <br>}<br/> <br>}<br/> }

Replies are listed 'Best First'.
Re: combine perl/tk and file::tail::select
by sflitman (Hermit) on Apr 03, 2011 at 23:38 UTC
    Use the code tags please, your code is not readable. To extract one IP address and port from a string, you can use this regular expression:
    my ($ipaddr,$port)=($1,$2) if $string=~/(\d+\.\d+\.\d+\.\d+):(\d+)/;
    HTH,
    SSF
      I believe there's a pretty big propaganda against parsing IPs using regexes right here, they recommend NetAddr::IP,Net::IPv6Addr.
      I saw many presentations about IPv6 where one of the slides was with this huge regex for matching IPv6 addresses, so I don't know, maybe this is useful -> Regex::IPv6.
        I believe there's a pretty big propaganda against parsing IPs using regexes right here, they recommend

        Big propaganda? That is an apocalyptic-ally psychotic-ally epic overstatement :)

        See also Socket6/Regexp::Common::net

Re: combine perl/tk and file::tail::select
by spx2 (Deacon) on Apr 04, 2011 at 07:15 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://897246]
Approved by sflitman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found