Bams has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to write a program in perl. it is to check socket from my local machine to multiple remote machines, to multiple destinations. The limitation i have is, on my local machine i only have standard modules installed. I am using expect to skip password as well 1. source hosts list abc.com def.com ghi.com 2. destination list contains host and port as a cvs file 123.com,1586 456.com,1234 789.com,5678 these files should be passed as argument , my program is still incomplete without expect. any help would be greatly appreciated

Please help, it is urgent, any help would be greatly appreciated. I am very new to perl. Kindly assist on completing this

#!/usr/bin/perl -w use Data::Dumper; use Expect; use strict; use warnings; my $exp = new Expect; $exp->log_file("SSHLOGFILE.txt"); $exp->log_stdout(0); my $user = 'xxxxxx'; my $pw = 'yyyyyyyy'; 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(SSH,"/usr/bin/ssh xxxxxx\@host ps aux |") or die "$!\n"; open my $data, $file2 or die "Could not open $file2: $!"; for (my $nice = <$data>) { chomp ($nice); my ($servername, $portnumber) = split (',', $nice); print "$servername \n"; print "$portnumber \n"; while (<SSH>) { printf "%s %s\n", $servername, $portnumber; use IO::Socket; my $sock = new IO::Socket::INET (PeerAddr => $servername,P +eerPort => $portnumber,Proto => 'tcp'); print "ERROR: Could not create socket: $!\n" unless $sock +; printf "%s\n", "SUCCESS: Port Responded" if $sock; close($sock) if $sock; } close SSH; exit; redo; } close $data; } close $info; exit

Replies are listed 'Best First'.
Re: socket checker in multiple hosts for multiple destination
by neilwatson (Priest) on Feb 09, 2016 at 14:46 UTC

    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

      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
          A reply falls below the community's threshold of quality. You may see it by logging in.

      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