Link to home
Start Free TrialLog in
Avatar of dshrenik
dshrenikFlag for United States of America

asked on

Check status of a port

Please let me know how I can check if a port is available or not. If it is not available, i want to know what service is using it. I want to do this on both mac and windows.

Thanks!
Avatar of for_yan
for_yan
Flag of United States of America image

You need to run network utility with Runtime.getRuntime().exec which will list ports and services
and then get the output into back to java and anlyze it.
And it would be pdifferent on Windows and Mac.
Let me dig what is the utility.
if you use the dos telnet command and a blank dos window pops up the port is open..

telnet 127.0.1.1:portnumber

telnet 127.0.0.1:40

Also there are tons of opensource portscanners available if you want more details..

http://sourceforge.net/search/?type_of_search=soft&words=port+scanner


netstat  on windows:
http://security.fnal.gov/handouts/IdentifyingOpenPortsWinXP-2003.pdf

I guess Mac is like Unix they probably also have netstat. let's dig more
Avatar of dshrenik

ASKER

I want to do it through java code. Thanks!
Java or Javascript?
Java
If your goal is to find one port to use then trying telnet will be better
if your goal is to know which piorts are doing what then you need some utility to use
I want to find out if a given port number is available. If it is not, I want to know what's using the port.
Thanks!

this is waht prints the open ports on Mac
"sudo lsof ..." (see caommand in there)
http://juretta.com/log/2007/08/08/list_open_ports_on_your_machine_mac_os_x_/
ASKER CERTIFIED SOLUTION
Avatar of H
H
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You probably want to find options in these commands which will list you only your particular port which you are intesrested in.
So if you hget nothing - then your port is not used, otherwise you'll read from the output waht it is used for

@hollecar:
Can you tell me how I can find out what service is using the port in case the port is unavailable. Thanks!
Why do you need to know hwat service?
that's the difference - if you want to know if it is available - that;s mucvh easier inside java,
if you want to know who uses it - it is more dofficultb to do through pure java
I want to make sure the right service is using the port.
Avatar of CEHJ

>>I want to make sure the right service is using the port.

You can only do that by getting a shell to the host on which the service is running and checking what process is involved
In windows and mac

netstat -o

This will give you the PID

MAC syntax may be different try looking at help on netstat

If you need this in Java also you could use a shell command
Example...
final String cmd = "netstat -ano";

int pid = -1;

try {
    // Run netstat
    Process process = Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
    e.printStackTrace(System.err);
}

Open in new window

@CEHJ:
Could you please elaborate a little more. If possible, please provide some sample code. Thanks!
WIndows

After you get the PID you can also use TASKLIST to get more details...

MAC

You may need a 3rd party tasklist utility

Here is a useful list of mac os X commands that you could use in a java shellcommand

http://ss64.com/osx/


Actually i retract that comment. You can only be sure by connecting to it and successfully implementing its protocol as a client.

btw, are you able to get a shell to the server?
Also you may need to test what o/s the program is on prior to executing any shell commands...Here is a sample I refer to occasionally....


public class OpertingSystemInfo 
{
  public static void main(String[] args)
  {
  String nameOS = "os.name";  
  String versionOS = "os.version";  
  String architectureOS = "os.arch";
  System.out.println("\n  The information about OS");
  System.out.println("\nName of the OS: " + 
System.getProperty(nameOS));
  System.out.println("Version of the OS: " + 
System.getProperty(versionOS));
  System.out.println("Architecture of THe OS: " + 
System.getProperty(architectureOS));
  }
}

Open in new window

You might look at Nmap and related:

http://seclists.org/nmap-dev/2010/q2/536
Not sure how that accepted solution is relevant to your question. Can you explain?
The code tells me if a given port is available or nor. Right?