Solarum Tech Boards Back to Solarum.com
September 10, 2010, 04:08:20 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to the Solarum Tech Boards, get in there and ask/answer some questions!
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Port Ping in PERL  (Read 1143 times)
Laz
Solarum Admin
Administrator
Jr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 59


SunLitLaz
View Profile WWW Email
« on: July 06, 2007, 05:52:36 PM »

One tool that I haven't seen on *nix that I see my Windows brethren using, is called portping.  There is a little freeware app called portping for Windows, that is really a misnomer as it doesn't ping anything really, but simply does a tcp connection attempt to see if the port on the destination side is answering.  This is actually a really cool tool in my book, so I wrote my own in PERL since I couldn't find something similar.  Basically, you call the script, give it a destination, a protocol (TCP is used by default), and a payload to deliver if you want to send something, otherwise it simply tries to connect to the port specified.  I have used it for a while now, and it has come in handy for troubleshooting network connections.  Check it out ...

Code:
#!/usr/bin/perl

use IO::Socket;

$remote_host = $ARGV[0];
$remote_port = $ARGV[1];
$remote_prot = $ARGV[2];
$msg_to_send = $ARGV[3];

$usage = "Usage: perlportping.pl host_or_IP_address port TCP/udp \"something to send to the server\"\n";

if (($remote_host eq "") || ($remote_port eq "")) {
  die ($usage);
}

if ($remote_prot eq "") {
  $remote_prot = "tcp";
} elsif (($remote_prot ne "tcp") && ($remote_prot ne "udp")) {
  die ($usage);
}

if ($socket = IO::Socket::INET->new(PeerAddr => $remote_host,
                                PeerPort => $remote_port,
                                Proto    => $remote_prot,
                                Type     => SOCK_STREAM)) {
  print "Connection to $remote_host:$remote_port ($remote_prot) successful.\n";

  if ($msg_to_send ne "") {
    print $socket "$msg_to_send\n";
    $answer = <$socket>;
    # and terminate the connection when we're done
    close($socket);
    print "\n\n  Sent: $msg_to_send\n\n  Got back: $answer\n\n";
  }

} else {
  print "Couldn't connect to $remote_host:$remote_port ($remote_prot) : $@.\n";
}

Let me know if you have any questions or comments!
Logged

Do you know what the chain of command is? It's the chain I go get and beat you with til you understand who's in rutting command here!
mack
Jr. Member
**

Karma: +0/-0
Offline Offline

Posts: 68



View Profile WWW
« Reply #1 on: July 07, 2007, 11:16:24 AM »

so, is this like a port scanner??  like scanning an ip block for open ports on nodes? 
Logged

He is no fool who gives up what he cannot keep to gain what he cannot lose --Jim Elliot, missionary martyr
Laz
Solarum Admin
Administrator
Jr. Member
*****

Karma: +2/-0
Offline Offline

Posts: 59


SunLitLaz
View Profile WWW Email
« Reply #2 on: July 07, 2007, 01:59:48 PM »

No, not really.  It is for testing to see if a particular port on a particular host is listening.  Sometimes we are trying to get connectivity to a server somewhere.  From the source server we can use this to see if we can reach the destination server at the specific port.  It's a troubleshooting tool, if you can't talk between two servers for example, but you know you can make the connection with this script, then the network layer is ok, it's something in the application.
Logged

Do you know what the chain of command is? It's the chain I go get and beat you with til you understand who's in rutting command here!
mack
Jr. Member
**

Karma: +0/-0
Offline Offline

Posts: 68



View Profile WWW
« Reply #3 on: July 08, 2007, 01:45:22 PM »

Sounds like a very cool script and very useful.  Next time I am in a network environment again I will keep this in mind.  Appreciate it.  :-)
Logged

He is no fool who gives up what he cannot keep to gain what he cannot lose --Jim Elliot, missionary martyr
Pages: [1]   Go Up
  Print  
 
Jump to:  

  Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC  
Page created in 0.141 seconds with 19 queries.