Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

netcat in perl

by jose_m (Acolyte)
on Mar 21, 2012 at 17:08 UTC ( [id://960819]=perlquestion: print w/replies, xml ) Need Help??

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

I need to check if we can connect to an ip/port netcat:

nc -z <ip> <port>

will return successful if it works anyone know of a way to do this in perl? thanks

Replies are listed 'Best First'.
Re: netcat in perl
by roboticus (Chancellor) on Mar 21, 2012 at 17:23 UTC

    jose_m:

    Use Socket or IO::Socket to try to connect to the computer, and see if it works or not.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: netcat in perl
by zentara (Archbishop) on Mar 21, 2012 at 19:01 UTC
    Here is a start for you. But I believe netcat will tell you if it can't connect. Why not run netcat thru some IPC. Or use a netcat written in Perl, see netcat.pl
    #!/usr/bin/perl use IO::Socket; $addr = '192.168.0.1'; $port = '80'; $socket = eval { return IO::Socket::INET->new( Proto => "tcp", PeerAddr => $addr, PeerPort => $port, Reuse => 1, Timeout => 10) or return undef; }; if ($socket) { print "Port open\n"; eval { return $socket->close; }; return 1; } else { print "Port NOT open\n"; return undef; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: netcat in perl
by repellent (Priest) on Mar 22, 2012 at 04:59 UTC

      sweet..thank you both. i added a few things, i am going to use nslookup to get the ips, then i am putting them into an array and looping through that. this way i dont have to modify the script when the ip changes. also this script needs to return and exit status for nagios it will be part of the monitoring framework..

      here is what i came up with, it runs fine, but i am open for suggestions..
      #!/usr/bin/perl #nagios return codes: 0->All Good, 2->Critical use Net::Nslookup; use Socket; #global var. exit status# $exit_status = 0; sub GetIps { #here we are getting the ip addresses and populating the ip array @ips; @ips= nslookup(domain =>"google.com"); } sub FirstCheck { foreach (@ips) { $socket = new IO::Socket::INET->new(PeerAddr => $_, PeerPort => '777', Proto => 'tcp', Timeout => 2, ) or print "could not create bind to $_. port 777: $@\n" + and $exit_status=2 ; close($socket); } } sub SecondCheck { foreach (@ips) { $socket = new IO::Socket::INET->new(PeerAddr => $_, PeerPort => '443', Proto => 'tcp', Timeout => 2, ) or print "could not create bind to $_. port 443: $@\n" + and $exit_status=2; close($socket); } } #calling in the subz! &GetIps; &FirstCheck; &SecondCheck; #exiting the program with an exit status. exit ($exit_status);

      also perl throws a bunch of errors if i run this with strict. anyone want to try it in strict and tell me whats wrong?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found