Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

socket checker in multiple hosts for multiple destination

by Bams (Initiate)
on Feb 09, 2016 at 11:45 UTC ( [id://1154722]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

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

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

    No recent polls found