Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check to see if ports are open

Status
Not open for further replies.
Apr 16, 2001
29
0
0
US
Hi all!

I'm trying to figure out a better way of using DBI::proxy. We have an NT server with SQL7 on it and a RUMBA connection to DB2 on an AS400. I need to be able to access both databases from a linux box, so I'm using DBI and DBIProxy. Problem is, DBIProxy on NT doesn't have either threading or fork, so I have to have multiple ports open. I need a way to check to see if a port is open to prevent collisions. We have a lot of automated processes that run throughout the day extracting and inserting data.

Right now, I'm checking by using alarm() and doing a query one of the databases. But, if that database is down, none of the ports work, even if I'm just trying to go to the other database.

I'm sure there must be a better way to do this, but all of my attempts have failed. Any ideas?
 
Hey. I have a solution for you in Perl. I wrote a Little Script to check for listening ports. This should be of some use to you. If this does nothing than sorry for inconvenience. I'm just tryin to offer help cause everyone helps me out.


use strict;
use Socket;

use constant DEFAULT => '127.0.0.1';
use constant PortYouWannaCheck => 80;
use constant IPPROTO_TCP => 6;

my $address = shift || DEFAULT;

my $packed_addr = inet_aton($address);
my $destination = sockaddr_in(PortYouWannaCheck, $packed_addr);

socket(SOCK, PF_INET, SOCK_STREAM, IPPROTO_TCP) or die " Cant make socket";
connect(SOCK, $destination) or die "Can;t Connect";

print <SOCK>;

 
With NT you can use NETSTAT.EXE. I don't know whether linux has a netstat command. This is not a perl solution :)
 
Thanks to both of you. I ended up using the nmap program on linux, and then parsing the results that it returned. Works pretty well.
 
I'll just throw this in on the chance(small) that it might help - I don't have any experience with this, but I think I saw a recent message on someone using Net::ping(or something like that) to do something similar.
Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top