#!/usr/bin/perl -- use threads stack_size => 4096; use threads::shared; use Thread::Queue; use strict; use warnings; { my $qin = Thread::Queue->new(); my $qout = Thread::Queue->new(); my $guithread = threads->create( \&tkguithread, $qin, $qout ); my $nmapthread = threads->create( \&nmapthread, $qin, $qout ); $guithread->join; } exit; sub tkguithread { my( $qin, $qout ) = @_; require Tk; my $mw = Tk::tkinit(); my $b1 = $mw->Button(-text => 'Go', -command => sub { $qin->enqueue( 'localhost' ); })->pack(); my $b2 = $mw->Button(-text => 'Exit', -command => [ $mw, 'destroy' ] )->pack(); my $text = $mw->Scrolled('Text',-scrollbars=>'e')->pack; $mw->repeat( 500, #ms [ \&appendFromQToText, $qout, $text ], ); $mw->MainLoop; return; } sub appendFromQToText { my( $qout, $text ) = @_; for(1..5){ if( defined( my $line = $qout->dequeue_nb )){ $text->insert('end', $line ); } } return; } sub Sleeps(){ Time::HiRes::usleep( 33 * 1000 ); ## microseconds } sub nmapthread { my( $qin, $qout ) = @_; threads->detach(); ## can't join me :) while( 1 ) { #~ if( defined( my $url = $qin->dequeue_nb ) ) { if( defined( my $url = $qin->dequeue ) ) { nmap( $url, $qout ); } Sleeps(); } return; } sub nmap { my( $host , $qout ) = @_; $qout->enqueue("got nmap($host)\n"); $host =~ s{[^a-zA-Z0-9.]}{}g; $host = 'localhost' if ! length $host; #~ my $pid = open my($H), qq{nmap -A "$host" |}; my $pid = open my($H), qq{echo faker "$host" |}; $qout->enqueue("pid($pid) \$! @{[int$!]} $!\n"); $qout->enqueue("eof @{[eof($H)]}\n"); while(<$H>) { $qout->enqueue( "nmap($host) $_" ); } close $H; return; }