Beating the fork() horse to death...
Title: Beating the fork() horse to death... I have read PerlIPC and Perlfork. I have read through the ASPN refrence material. I have searched Google. I am just not getting the concept of how fork() works... So maybe someone can show me.. Here is the scenario.. I'd like to ping a range of addresses and list the responding hosts in an array. I am able to do this using a foreach loop, but only one ip at a time.. This works but it is slow. I'd like to ping several (64,128,256) hosts at a time. I don't care what order they go into the responding hosts array. Also, I'd like to hit a port on each of the responding hosts to see if the port is open. Here is my logic so far... (actual code is much longer) [create array(ip_array) from inputed range] Foreach $ip(@ip_array) { pingem($ip); } Foreach $host($host_array){ portcheck($host); } #define subs Sub pingem(){ [ping $ip and add $ip to array (@host_array) if responds] } Sub portcheck(){ [connect to port and add to array of services running] } ... How would I introduce fork()? Thanks in advance... ...::: Matt :::... Not everything that is counted counts, and not everything that counts can be counted. - A. Einstein
RE: Beating the fork() horse to death...
Title: Message oh,yeah... Win32 platform for the example code.. but Unix info would be interesting.. ...::: Matt :::... Not everything that is counted counts, and not everything that counts can be counted. - A. Einstein -Original Message-From: Matthew Wagenknecht [mailto:[EMAIL PROTECTED]] Sent: Saturday, May 11, 2002 2:15 PMTo: [EMAIL PROTECTED]Subject: Beating the fork() horse to death... I have read PerlIPC and Perlfork. I have read through the ASPN refrence material. I have searched Google. I am just not getting the concept of how fork() works... So maybe someone can show me.. Here is the scenario.. I'd like to ping a range of addresses and list the responding hosts in an array. I am able to do this using a foreach loop, but only one ip at a time.. This works but it is slow. I'd like to ping several (64,128,256) hosts at a time. I don't care what order they go into the responding hosts array. Also, I'd like to hit a port on each of the responding hosts to see if the port is open. Here is my logic so far... (actual code is much longer) [create array(ip_array) from inputed range] Foreach $ip(@ip_array) { pingem($ip); } Foreach $host($host_array){ portcheck($host); } #define subs Sub pingem(){ [ping $ip and add $ip to array (@host_array) if responds] } Sub portcheck(){ [connect to port and add to array of services running] } ... How would I introduce fork()? Thanks in advance... ...::: Matt :::... Not everything that is counted counts, and not everything that counts can be counted. - A. Einstein
Re: Sending info to prog (writing to and reading from an external program)
- Original Message - From: smackdab smackdab [EMAIL PROTECTED] I have looked at open and pipe, but it seems like it is going to call fork(), which I am avoiding on Win32... What is the best way to handle this? Is this one of the things that Win32 Firstly, you can feel pretty safe with a pipe open on Win32, I've never really experienced any issues with this, and I'm not aware of there being any issues with it (please inform me otherwise if I am incorrect). However, what I think that you're trying to do is both supply input and read output to/from an external program. Avoiding all the discussion of the many ways to provide input to programs you've written, and assuming you want to just put it in the called program's STDIN, you'll want to look at IPC::Open2, IPC::Session, or the 'here file' shell directive. BTW, programs that read from STDIN usually consider the end of input to be specified by an EOF character, which is often the ^D (control+D) character. close()ing the handle, like you did below, simply kills the program. But, back to the problem at hand... You cannot say open(FH,| $command |) to give input and read output. open() doesn't work like that. So here are two examples (untested!) one using IPC::Open2 (I've never used open2 directly, but this is from the docs...) and the other using a simple here file directive with a piped open(). ipc-open2: ---begin--- use IPC::Open2; my($readhan,$writehan,$pid); my @values = ('bbb','aaa','ccc','eee','ddd'); if($pid = open2($readhan,$writehan,sort.exe)) { my $val_str = join(\n,@values); print($writehan $val_str\n); close($writehan); my $output = $readhan; print(OUT: $output\n); close($readhan); } else { die(ERROR: Could not execute open2 - $!\n); } ---end--- or, the far simpler 'here file' directive, which says that the file following the directive will be supplied to the program's STDIN. ---begin--- my $output = 'bbb aaa ccc eee ddd'; my $here_file = 'tmp.txt'; if(open(TWFH,$here_file)) { print(TWFH $output\n); close(TWFH); } else { die(ERROR: Could not open $here_file - $!\n); } # look at this open, we use the 'here file' directive () # to specify the input for the program, and use a pipe # at the end to specify that we want to read the output # of the program being executed. # you could also do this as: # my $return = `sort.exe $here_file`; # but I think open() is more explicit in this # case. if(open(TRXFH,sort.exe $here_file |)) { while(TXRFH) { print(OUT: $_); } close(TXRFH); } else { die(ERROR: Could not execute sort.exe - $!\n); } ---end--- FYI, you'll probably have a much easier time with IPC::Session than IPC::Open2. Enjoy! !c ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: GetTcpTable API?
Flowers, Jay wrote: Almost, acctualy if the remote address is 0.0.0.0 then the remote is populated with garbage so you need a conditional statement. snip This might be a bit prettier output: #!perl -w -- use strict; use Win32::API; my $api = new Win32::API(iphlpapi.dll, GetTcpTable, ['P','P','N'], 'N') or die Get GetTcpTable: . Win32::FormatMessage(Win32::GetLastError()); my $table = pack 'I', 0;# not used my $size = pack 'I', length $table; my $sort = 1; my $status = $api-Call($table, $size, $sort); # get table size on 1st call my $tmp = unpack 'I', $size; $table = pack 'A*', \0 x $tmp; # now fill TCP table with: # 1: A longword containing the number of 20 byte elements below # 2..N: A 20 byte element ($status = $api-Call($table, $size, $sort)) == 0 or die Win32::FormatMessage(Win32::GetLastError()); my $rows = unpack 'L', $table; # get row count my @rows = unpack 'L' . (Lnnnn x $rows), $table; shift @rows;# drop row count element printf %15s%15s%-15s\n, 'Local Addr', 'Remote Addr', 'State'; my %states = (1 = 'CLOSED', 2 = 'LISTEN', 3 = 'SYN_SENT', 4 = 'SYN_RCVD', 5 = 'ESTABLISHED', 6 = 'FIN_WAIT1', 7 = 'FIN_WAIT2', 8 = 'CLOSE_WAIT', 9 = 'CLOSING', 10 = 'LAST_ACK', 11 = 'TIME_WAIT', 12 = 'DELETE_TCB'); for (my $ii = 0; $ii @rows; $ii += 13) { my $tmp = join '.', @rows[$ii+7..$ii+10]; if ($tmp eq '0.0.0.0') { $rows[$ii+7] = '*'; $rows[$ii+11] = '*'; } else { $rows[$ii+7] = sprintf %3u.%03u.%03u.%03u, @rows[$ii+7..$ii+10]; } $tmp = join '.', @rows[$ii+1..$ii+4]; if ($tmp eq '0.0.0.0') { $rows[$ii+1] = '*'; } else { $rows[$ii+1] = sprintf %3u.%03u.%03u.%03u, @rows[$ii+1..$ii+4]; } printf %15s:%-5u %15s:%-5s %-11s\n, @rows[$ii+1, $ii+5, $ii+7, $ii+11], $states{$rows[$ii]}; } __END__ -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /-- o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/__/_/_ Castle of Medieval Myth Magic http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Preferred PERL Editor
Martin Moss wrote: Doesn't anybody still use VI these days?? :-) VI(M) - yes. Only thing always available on UNIX with a very good Win32 GUI port. Probably faster than any of the others and on more platforms. Emacs may be more powerful, but has a longer learning curve and not the best default key bindings (although you can change that). But if you know neither it might be the way to go for pure power. -- ,-/- __ _ _ $Bill Luebkert ICQ=14439852 (_/ / )// // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /-- o // // http://dbecoll.tripod.com/ (Free site for Perl) -/-' /___/__/_/_ Castle of Medieval Myth Magic http://www.todbe.com/ ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs