Does alarm work on w2k?

D

Default

Hi, i just started programming a month ago.
I know almost nothing... any sort of help with this script
would be excellent.

For some reason the script seems to do the same thing with
or without the alarm thing in there. Does this alarm work on w2k?

Also although i may be able to work this out eventually, any tips on
how to display the current port in a more effective way would be very
helpfull.

Here is the script.

#!
use strict;
use warnings;
use Socket;
$| = 1;

my ($internet_addr, $paddr, $remote_port, $remote_host, $kidpid1,
$kidpid2, $kidpid3, @ports_even1, @ports_odd1, @ports_even2,
@ports_odd2,);
my $counter = 1;
my $p_e_loader1 = 2;
my $p_o_loader1 = 1;
my $p_e_loader2 = 514;
my $p_o_loader2 = 513;
print "\n" . ' ' . '='x78 . "\n";
print "\t\t\t\tPort Scanner\n";
print ' ' . '='x78 . "\n\n";

while ($counter <= 256)
{
push (@ports_even1, $p_e_loader1);
$p_e_loader1++;$p_e_loader1++;
$counter++;
if ($counter >= 250) {print "\n@ports_even1\n";my $pause = <STDIN>;}
}
$counter = 1;
while ($counter <= 256)
{
push (@ports_odd1, $p_o_loader1);
$p_o_loader1++;$p_o_loader1++;
$counter++;
}
$counter = 1;
while ($counter <= 256)
{
push (@ports_even2, $p_e_loader2);
$p_e_loader2++;$p_e_loader2++;
$counter++;
}
$counter = 1;
while ($counter <= 256)
{
push (@ports_odd2, $p_o_loader2);
$p_o_loader2++;$p_o_loader2++;
$counter++;
}
undef $counter;

#resolve targets address
$remote_host = shift || 'localhost';
$internet_addr = inet_aton($remote_host) ||
die "Couldn't resolve $remote_host"."'s address\n($!)\n($^E)*";

#create two processes
die "can't fork\n($!)\n($^E)\n*" unless defined($kidpid1 = fork());

if ($kidpid1) #first parent process
{
foreach $remote_port (@ports_even1)
{
print "\b\b\b\b \b\b\b\b";
print "$remote_port"
socket(SOCK_OUT, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
$paddr = sockaddr_in($remote_port, $internet_addr);
if (connect(SOCK_OUT, $paddr))
{print "\nPort: $remote_port is open.\n";}
eval
{
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm 1;
close(SOCK_OUT);
alarm 0;
};
}
}
else #first child process
{
#create a third process
die "can't fork\n($!)\n($^E)\n*" unless defined($kidpid2 = fork());
if ($kidpid2) #second parent process
{
foreach $remote_port (@ports_odd1)
{
print "\b\b\b\b \b\b\b\b";
print "$remote_port"
socket(SOCK_OUT, PF_INET, SOCK_STREAM,
getprotobyname('tcp'));
$paddr = sockaddr_in($remote_port, $internet_addr);
if (connect(SOCK_OUT, $paddr))
{print "\nPort: $remote_port is open.\n";}
eval
{
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 1;
close(SOCK_OUT);
alarm 0;
};
}
}
else #second child process
{
#create a fourth process
die "can't fork\n($!)\n($^E)\n*" unless defined($kidpid3=fork());
if ($kidpid3) #third parent process
{
foreach $remote_port (@ports_even2)
{
print "\b\b\b\b \b\b\b\b";
print "$remote_port"
socket(SOCK_OUT, PF_INET, SOCK_STREAM,
getprotobyname('tcp'));
$paddr = sockaddr_in($remote_port, $internet_addr);
if (connect(SOCK_OUT, $paddr))
{print "\nPort: $remote_port is open.\n";}
eval
{
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 1;
close(SOCK_OUT);
alarm 0;
};
}
}
else
{
foreach $remote_port (@ports_odd2)
{
print "\b\b\b\b \b\b\b\b";
print "$remote_port";
socket(SOCK_OUT, PF_INET, SOCK_STREAM,
getprotobyname('tcp'));
$paddr = sockaddr_in($remote_port, $internet_addr);
if (connect(SOCK_OUT, $paddr))
{print "\nPort: $remote_port is open.\n";}
eval
{
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 1;
close(SOCK_OUT);
alarm 0;
};
}
}
}
}
kill("TERM", $kidpid1);
kill("TERM", $kidpid2);
kill("TERM", $kidpid3);
print "\nDone.\n" && exit;
 
D

Default

No. You can always check the perlport perldoc to see if something is
not implemented completely or at all for your platform.
ahhh thanks.. didnt know about perlport.
alarm Not implemented. (Win32)

any ideas about the display issues?
 
A

Anno Siegel

ahhh thanks.. didnt know about perlport.
alarm Not implemented. (Win32)

any ideas about the display issues?

You posted 150 lines of code with nothing but a leading comment about alarm.
What display issues?

Anno
 
D

Default

You posted 150 lines of code with nothing but a leading comment about alarm.
What display issues?

Anno
Well the currently scanned port is printing in all four threads and they are
copying over eachother this looks messy, ive been trying to get it work better
with curses but when i add curses the program simply ends with no errors at all.
therefore i would like to use simple print statements in a similar fasion to this version
of the script but im not sure how to make the currently scanned port print in one x,y
coord. please note i've started a new thread with the curses version of the script
with the subject Curses %$#@!

Thank You - very much for replying your help is very appreciated.
 
B

Ben Liddicott

Hi Default IO Error,

The basic thing about threads is that things may happen simultaneously, so in order to guarantee that the threads don't write over each other, you need to provide some synchronisation, in general, at any rate.

If you don't need perfection, but just want something better, try either flushing after every write, or turning autoflush on. This will cause output to be sent to the console immediately rather than being buffered, and result in cleaner output, though there are still no guarantees.

For STDOUT:

use English qw(-no-match-vars);
$OUTPUT_AUTOFLUSH=1;


For others, I suggest using IO::Handle and autoflush.

my $stdout = IO::Handle->new_from_fd(STDOUT);
$stdout->autoflush(1);


Cheers,
Ben Liddicott
 
J

James Willmore

use English qw(-no-match-vars);
$OUTPUT_AUTOFLUSH=1;

Or more idiomatic Perl:
$| = 1;

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
In a museum in Havana, there are two skulls of Christopher
Columbus, "one when he was a boy and one when he was a man."
-- Mark Twain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top