perl_boy has asked for the wisdom of the Perl Monks concerning the following question:
I have tried to replace that line 11 withdie "perl citrix-enum-apps-xml-PORT-STATE-SERVICE+IP-disk.pl <INPUT_FI +LE_ports> <INPUT_FILE_IP> <OUTPUT_FILE> " if $#ARGV < 2; open(F0, $ARGV[0]); open(F1, $ARGV[1]); open(F2, ">>$ARGV[2]"); $line_no=$line_stop=0; while (<F0>) { s/\r\n//; /^[0-9]+/; $line=$'; $line_no=$&; while (<F1> && $line_stop++ < $line_no) { s/\r\n//; } print F2 $_, "\t$line\n" if /[0-9]+\//; } close F0; close F1; close F2;
but nothing comes out what's wrong ? Remember I have already one input file openned F0 the idea of the script is to replace the line number (3 and 5 here) in port.txt input filewhile (<F1> && $line_stop++ < $line_no) { s/\r\n//;print $_; }
and get the IP corresponding to the nth ($line_stop) (3 and 5 here) IP by 1.0.131.74 and 1.0.138.1543 PORT state protocol 3 80/tcp closed http 3 443/tcp closed https 3 8080/tcp open http-proxy 5 80/tcp open http 5 443/tcp filtered https 5 8080/tcp filtered http-proxy
to produce1.0.129.197 1.0.131.49 1.0.131.74 1.0.138.143 1.0.138.154 1.0.139.72
any ideas ? thank's1.0.131.74 80/tcp closed http 1.0.131.74 443/tcp closed https 1.0.131.74 8080/tcp open http-proxy 1.0.138.154 80/tcp open http 1.0.138.154 443/tcp filtered https 1.0.138.154 8080/tcp filtered http-proxy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can't read the 2nd input file (updated)
by haukex (Archbishop) on Mar 15, 2021 at 18:21 UTC | |
You need to check your open calls for errors, as shown in "open" Best Practices - once you've got a more specific error message, we can help better. (And if you're not already, Use strict and warnings!) Update: While what I wrote above is correct, looking at your code again, there are further issues: after while (<F1> ... reads the input file, it doesn't automatically reset to the beginning of the file. Also, since you're not using the while (<F1>) form and instead have added additional conditionals in the while condition, Perl is not going to assign the current line to $_ for you like it is with while (<F0>). And another thing I see is that you never reset $line_stop. So overall, because even I am having a bit of trouble understanding what your code is supposed to be doing, I think you'll need to take a step back and rethink your logic. Note that instead of nested loops, a common approach is to first read one of the files into a hash (or other data structure), and then use that for lookups while looping over the other file. I would suggest you take a look at perlintro and the Basic debugging checklist. Update 2: Here's a suggestion: You can read the list of IP addresses into an array by simply saying chomp( my @ips = <$ip_filehandle> ); (see also chomp). Then, in a basic while loop, read the lines of the file with the port specifications, and you can then select the IP address from the array - remember that arrays are indexed starting from 0, while your lines appear to be numbered starting from 1. | [reply] [d/l] [select] |
|
Re: can't read the 2nd input file
by haukex (Archbishop) on Mar 15, 2021 at 20:02 UTC | |
Since you provided your code and sample input and output data, I'll give you some code to get you started:
Note that this makes a few small assumptions:
| [reply] [d/l] [select] |
by perl_boy (Novice) on Feb 03, 2022 at 16:20 UTC | |
the memory version reads the IP file and into memory and prints the IP from the array to the output file the disk version reads a files at once, altering from one file to the other until it find the numbered IP then write the IP and associated ports to the output file. the memory version is much faster and thus much easier to write but may not work on computers with less memory. the disk version is much slower due to frequent disk access. port-state-service+IP-mem.pl and port-state-service+IP-disk.pl https://www.perlmonks.com/?node_id=11129698 thank you for your help. | [reply] |
|
Re: can't read the 2nd input file
by tybalt89 (Monsignor) on May 07, 2022 at 21:12 UTC | |
NOTE: requires portfile to be in sorted order
Outputs (for your larger example with blank lines in the IP file):
| [reply] [d/l] [select] |
|
Re: can't read the 2nd input file
by perl_boy (Novice) on Feb 01, 2022 at 22:07 UTC | |
The port file contains a number at the left that is intented to pick-up the Nth 0-based IP from the IP file,empty lines are ignored. I have made it on purpose to insert SPACEs/TABs and empty NEWLINEs just to show you that the program takes care of this and bellow the normal IP and port files WITHOUT SPACEs/TABs and empty NEWLINEs. The port-state-service+IP-mem.pl was much easier to write and runs much faster than port-state-service+IP-disk.pl but may not work on computers with less memory. On the other hand port-state-service+IP-disk.pl runs with low constant memory but is much slower due to frequent disk access. Again, thank you all for your help. port file WITH SPACEs/TABs and empty NEWLINEs IP file WITH SPACEs/TABs and empty NEWLINEs
IP file WITHOUT SPACEs/TABs and empty NEWLINEs I have tested different forms of IPs (IP file) 1.0.XXX.XXX 1.0.0.X 10.00.00.XX 100.000.000.XXX 01.00.00.XX 001.000.000.XXX
and have the following output 1.0.XXX.XXX 1.0.0.X 10.00.00.XX 100.000.000.XXX 01.00.00.XX 001.000.000.XXX
port file WITHOUT SPACEs/TABs and empty NEWLINEs (there are 2 TABs between the line number and the rest of the line) I have tested different forms of Nth IPs with different number of leading '0's (IP port)
port-state-service+IP-mem.pl port-state-service+IP-disk.pl
| [reply] [d/l] [select] |
|
Re: can't read the 2nd input file;partial memory via IP start (ARGV 3) using split
by perl_boy (Novice) on Jun 07, 2022 at 12:24 UTC | |
Everything works find with the same test files added -ARGV-line-IP-start to the following memory programs filenames
to make
added die "ARGV[3] <LINE_IP_LINE> MUST contain just a number: got =>$ARGV[3]<=" if $ARGV[3] =~ /[^0-9]/; to check that the third command line arguement is indeed a JUST number and changed line 1 die "perl port-state-service+IP-mem-array.pl <INPUT_FILE_ports> <INPUT_FILE_IP> <OUTPUT_FILE> " if $#ARGV < 2; to
line 4 in
line 8 in
$line_no=0; to $line_no=$ARGV[3]; here is the program code port-state-service+IP-mem-array-ARGV-line-IP-start.pl
port-state-service+IP-mem-array-delete-ARGV-line-IP-start.pl
port-state-service+IP-mem-array-delete-xtra-ARGV-line-IP-start.pl
port-state-service+IP-mem-hash-direct-ARGV-line-IP-start.pl
port-state-service+IP-mem-hash-direct-delete-ARGV-line-IP-start.pl
port-state-service+IP-mem-hash-direct-delete-xtra-ARGV-line-IP-start.pl
port-state-service+IP-mem-hash-indirect-ARGV-line-IP-start.pl
port-state-service+IP-mem-list-direct-ARGV-line-IP-start.pl
port-state-service+IP-mem-list-indirect-ARGV-line-IP-start.pl
port-state-service+IP-mem-list-splice-ARGV-line-IP-start.pl
port-state-service+IP-mem-list-splice-xtra-ARGV-line-IP-start.pl
| [reply] [d/l] [select] |
|
Re: can't read the 2nd input file;12 new versions of PORT-STATE-SERVICE+IP;14 in all;3 disk,11 memory (part 1)
by perl_boy (Novice) on Apr 04, 2022 at 16:32 UTC | |
3 versions/programs work from disk
11 versions/programs work from memory
The ones operating from disk is in the case where the list is IPs taken from IP.txt file, which may be very big will not fit in a computers memory (some legacy computers from the 70/80 have only 64K!) in which case will work much slower due to frequent disk access Those are goto, sub and while. The goto version is a bit contreversial and the black sheep since it uses a goto to jump in goto get_IP (line 13) and out goto CONT (line 20) of the outter while (line 7) loop and I was a bit hesitant of including it here,but I did EVERYTHING I could, but could not find any instruction/statement/command to return from a goto (see below) and decided to post it anyway for completeness The sub version uses a subroutine calling &get_IP (line 13) defined below (lines 18 .. 28) The while version uses an inner while loop (lines 13 .. 15) I did the port-state-service+IP-disk-while.pl from port-state-service+IP-disk-goto.pl replacing goto get_IP with the code of that label , indented and deleted the labals get_IP CONT END All 3 versions (subroutine calling &get_IP, while loop, goto get_IP label) read the IP.txt file upto the line number 1st column ($1 as in awk) of port.txt file and prints the IP associated with the nth,that is, that number The ones operating from memory are much faster since IPs are fetched directly from memory. They use 3 different data structures (array, list, hash) from perl to store IPs read from IP.txt list and hash both have a direct and indirect version. The direct version port-state-service+IP-mem-list-direct.pl and port-state-service+IP-mem-hash-direct.pl read from the data structure (list, hash) itself without passing by a subroutine, while the indirect version port-state-service+IP-mem-hash-indirect.pl and port-state-service+IP-mem-list-indirect.pl uses the idx subroutine The array and hash both have 2 delete versions
the delete ONLY versions port-state-service+IP-mem-array-delete.pl and port-state-service+IP-mem-hash-direct-delete.pl delete ONLY the index item while port-state-service+IP-mem-array-delete-xtra.pl and port-state-service+IP-mem-hash-direct-delete-xtra.pl delete items between interveining line numbers from the port.txt file | [reply] [d/l] [select] |
|
Re: can't read the 2nd input file;12 new versions of PORT-STATE-SERVICE+IP;14 in all;3 disk,11 memory (part 2)
by perl_boy (Novice) on Apr 06, 2022 at 12:51 UTC | |
As in the above port-state-service+IP-mem-list-splice.pl delete ONLY the index item while port-state-service+IP-mem-list-splice-xtra.pl delete items between interveining line numbers from the port.txt file port-state-service+IP-mem-list-splice.pl calls splice on list @IP this one REALLY caused be headeachs as is I was struggling with a moving target, @IP when calling splice so I have created $w $x $y $z variable to deal with this,which finally works, but NOT WITHOUT effort ! I have done port-state-service+IP-mem-list-direct.pl from port-state-service+IP-mem.pl (now renamed port-state-service+IP-mem-array.pl) changing $IP[$line_no++] = for push @IP, at line 8 I have done port-state-service+IP-mem-hash-direct.pl from port-state-service+IP-mem.pl (now renamed port-state-service+IP-mem-array.pl) changing $IP[$line_no++] for $IP{$line_no++} at line 8 and changing $IP[$line_no - 1] for $IP{$line_no - 1} at line 16 Finally, the plain array version port-state-service+IP-mem-array.pl leaves the array untouched A few words on the contreversial goto version port-state-service+IP-disk-goto.pl I have posted 11141855 on "how to return from a goto ?" but got no replies as an instruction/statement/command that would allow me to return FROM WITHIN the label,the only reply I got was to goto sub using a subroutine,but this is not a label and is alread listed here (port-state-service+IP-disk-sub.pl) I have Google searched for a perl statement to get out of a goto label and resume where it left off and I have read a few files about the goto statement all URLs uploaded here http://bitcoinshell.mooo.com/users/p3rl/URL-00.html but nothing satisfactory I have made port-state-service+IP-disk-goto.pl out of port-state-service+IP-disk.pl now renamed port-state-service+IP-disk-sub.pl replacing the subroutine call &get_IP with goto get_IP AND replacing the subroutine with a label Althought both programs work fine I m a bit concerned about the above warning messages for jumping in the middle of a while I have fixed this from the outside (shell) using perl port-state-service+IP-disk-goto.pl ports.txt IP.txt output.txt 2> /dev/null redirecting STDERR to /dev/null using the 2> bash shell operator I have even tested with a large list of IPs in addition to those listed on 11129698 under "IP file WITH SPACEs/TABs and empty NEWLINEs", "IP file WITHOUT SPACEs/TABs and empty NEWLINEs" and "and get the IP corresponding to the nth", "I have tested different forms of Nth IPs with different number of leading '0's (IP port)" for the purpose of port-state-service+IP-disk-goto.pl (warnings being thorws up to STDERR) and port-state-service+IP-mem-list-splice-xtra.pl delete items between interveining line numbers in the @IP list and both work fine (see output file below) | [reply] [d/l] [select] |
|
Re: can't read the 2nd input file;12 new versions of PORT-STATE-SERVICE+IP;14 in all;3 disk,11 memory (part 3)
by perl_boy (Novice) on Apr 07, 2022 at 12:21 UTC | |
IP file
output file
all 14 programs have the same results/output and take the same ARGVs <INPUT_FILE_ports> <INPUT_FILE_IP> <OUTPUT_FILE> complete list of programs
list of new programs
program listing: port-state-service+IP-disk-sub.pl
port-state-service+IP-disk-goto.pl
port-state-service+IP-mem-list-direct.pl
port-state-service+IP-mem-hash-direct.pl
port-state-service+IP-mem-array.pl
port-state-service+IP-disk-while.pl
port-state-service+IP-mem-array-delete.pl
port-state-service+IP-mem-array-delete-xtra.pl
port-state-service+IP-mem-hash-direct-delete.pl
port-state-service+IP-mem-hash-direct-delete-xtra.pl
port-state-service+IP-mem-hash-indirect.pl
port-state-service+IP-mem-list-indirect.pl
port-state-service+IP-mem-list-splice.pl
port-state-service+IP-mem-list-splice-xtra.pl
| [reply] [d/l] [select] |
| A reply falls below the community's threshold of quality. You may see it by logging in. | |