Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: socket checker in multiple hosts for multiple destination

by neilwatson (Priest)
on Feb 09, 2016 at 14:46 UTC ( [id://1154732]=note: print w/replies, xml ) Need Help??


in reply to socket checker in multiple hosts for multiple destination

Use the three argument open

open( my $ssh_fh, "|-", 'ssh user@host...' ) or die "Could not ssh [$!]"; open( my $data_fh, ">", $file2 ) or die "Could not open [$file2] [$!]";

Don't use 'use' in the middle of code

Put use IO::Socket::INET at the top with other use statements.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re^2: socket checker in multiple hosts for multiple destination
by u65 (Chaplain) on Feb 15, 2016 at 16:28 UTC
    Don't use 'use' in the middle of code

    Note that in Perl 6 you can use a module in the middle of code and it is only valid within that block.

      ... in Perl 6 you can use a module in the middle of code and it is only valid within that block

      Does that mean that Perl6 unloads the module when the block is exited ? ... in which case if there's a second block later on that wants to use the same module then that second block has to load that module all over again ?

      Note that in Perl5 you can also require a module in a block of code, such that the module doesn't get loaded until that block is executed ... but once loaded that module stays loaded even after the block is exited.

      Cheers,
      Rob
        I don't think Rakudo yet unloads the module in the sense of freeing up the space it takes but it does unload it in the sense the symbols that the module added go away again (become unavailable to non-mop code).

        If multiple blocks want to use the same module then you'll typically `use` that module in an outer scope:

        use foo; # foo's and waldo's symbols available { use bar; # foo's, waldo's, and bar's symbols available } # foo's and waldo's symbols available { use qux; # foo's, waldo's, and qux's symbols available } # foo's and waldo's symbols available use waldo;

        A reply falls below the community's threshold of quality. You may see it by logging in.

        I can't say definitively, but I think the module stays loaded and can only be used in the scope where it is used. The gory details are described here.

        Use of uninitialized value $servername in printf at ./new.pl line 42. This is the error message I am getting when I executed the code after modifying change suggested by "poj" Please help. Any guidance would be greatly appreciated

Re^2: socket checker in multiple hosts for multiple destination
by Bams (Initiate) on Feb 10, 2016 at 05:07 UTC

    Hi Neil, Thanks for the response. I have modified the code accordingly. Still I am getting error. I am very new to perl This is the error ********* ERROR: Could not create socket: Invalid argument Use of uninitialized value $servername in printf at ./new.pl line 42. Use of uninitialized value $portnumber in printf at ./new.pl line 42. **************

    #!/usr/bin/perl -w use Data::Dumper; use Expect; use strict; use IO::Socket; use warnings; my $exp = new Expect; $exp->log_file("SSHLOGFILE.txt"); $exp->log_stdout(0); my $user = '*****'; my $pw = '**********'; if( ! defined $ARGV[0] ) { print "Usage: new.pl source destination\n"; exit; } my $file1 = $ARGV[0] or die "Need to get CSV file on the command line\ +n"; my $file2 = $ARGV[1] or die "Need to get CSV file on the command line\ +n"; open my $info, $file1 or die "Could not open $file1: $!"; while( my $line = <$info>) { print $line; open( my $ssh_fh, "|-", 'ssh xxxxx@yyyy ps aux |' ) or die " Could not ssh [$!]\n"; open( my $data_fh, ">", $file2 ) or die "Could not open [$file2] [$!]"; for (my $nice = <$data_fh>) { chomp ($nice); my ($servername, $portnumber) = split (',', $nice); print "$servername \n"; print "$portnumber \n"; while ($ssh_fh) { printf "%s %s\n", $servername, $portnumber; my $sock = new IO::Socket::INET (PeerAddr => $ +servername,PeerPort => $portnumber,Proto => 'tcp'); print "ERROR: Could not create socket: $!\n" +unless $sock; printf "%s\n", "SUCCESS: Port Responded" if $s +ock; close($sock) if $sock; } close $ssh_fh; exit; redo; } close $data_fh; } close $info; exit

      > opens file for writing not reading. see open

      # change > to < open( my $data_fh, ">", $file2 ) or die "Could not open [$file2] [$!]"; for (my $nice = <$data_fh>) {
      poj

        Thats the code I have presently. I did not understand your comment #@poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1154732]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found