0
#!/usr/local/bin/perl
use Tk;
# Main Window
$mw = new MainWindow;
$label = $mw -> Label(-text=>"Hello folks") -> pack();
$button = $mw -> Button(-text => "Click here to Flush rules",
                -command =>\&flush) -> pack();
MainLoop;

sub flush {
$mw->messageBox(-message=>"Initiating flushing.. click on OK button");
system ("iptables -L");
system ("iptables -F");
system ("iptables -L");
}

I made this code and what it does is that when a user click on the Button a message box appears

enter image description here

Then when I click on OK button it calls the subroutine flush and then the output is shown on terminal like this:

enter image description here

I want it to be appear on the same message box. How can I do it?

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133

2 Answers2

1

  • don't use system
  • capture STDOUT/STDERR ( qx, IPC::System::Simple, IPC::Run...)
  • update label (as simple as updating $textvariable ... see Tk demo program widget for example )
  • vayay
    • 86
    • 2
    0

    I have got the answer of this question at perlmonks.

    The link of the post at perlmonks is-> http://www.perlmonks.org/index.pl?node_id=920414

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Tk;
    
    # Main Window
    my $mw = new MainWindow;
    $mw->geometry('+100+100');
    
    my $label = $mw -> Label(-text=>"Hello folks") -> pack();
    my $button = $mw -> Button(-text => "Click here to Flush rules",
                    -command =>\&flush) -> pack();
    MainLoop;
    
    
    sub flush {
    $mw->messageBox(-message=>"Initiating flushing.. click on OK button");
    # the script hangs here, until the messagebox OK button is pressed.
    
    my $text = $mw->Scrolled('Text')->pack();
    
    #my $out1 =  `iptables -L`;
    my $out1 =  `ls -la`;
    $text->insert('end',"$out1\n");
    $text->see('end');
    
    #my $out2 =  `iptables -F`;
    my $out2 =  `dir`;
    $text->insert('end',"$out2\n");
    $text->see('end');
    
    #my $out3 =  `iptables -L`;
    my $out3 =  `ps auxww`;
    $text->insert('end',"$out3\n");
    $text->see('end');
    }
    
    Chankey Pathak
    • 21,187
    • 12
    • 85
    • 133