[Solaris] Which process is bound to a given port ?

Again I was faced with the problem of a port being busy and needed to determine what process was bound to it. The little script below I picked up some time ago from the internet came in handy. I unfortunately cannot remember though to whom I owe credits. Here it is:

#!/bin/ksh

line='---------------------------------------------'
pids=$(/usr/bin/ps -ef -o pid=)

if [ $# -eq 0 ]; then
read ans?"Enter port you would like to know pid for: "
else
ans=$1
fi

for f in $pids
do
/usr/proc/bin/pfiles $f 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
if [ $? -eq 0 ]; then
echo $line
echo "Port: $ans is being used by PID:\\c"
pargs -l $f
#/usr/bin/ps -o pid,args -p $f
fi
done
exit 0

Comments:

"ps -e -o pid | sed 1d" would be more efficient than your current $pids assignment.

Also, in your final line, there is no point using -f to ps if you then specify the fields you want with -o, and instead of using grep to find the right process you could just use "-p $f". And the whole line could be replaced with "pargs -l $f" too.

Ceri

Posted by Ceri Davies on January 23, 2008 at 09:13 AM CET #

The first pids= ps pipe can be done in a single line thus:

pids=$(ps -ef -o pid=)

The second ps invocation again can be done in a single call thus:

ps -o pid,args -p $f

Posted by Darren Moffat on January 23, 2008 at 10:39 AM CET #

@Ceri
@Darren

Thanks for your comments. Much appreciated. I updated the original blog entry.

Posted by JoachimAndres on January 24, 2008 at 12:41 AM CET #

I think you must put something like a "$" at the end of the pfiles line:

...grep -q "port: $ans$"

otherwise pfiles will find all processes STARTING with port-, like:

53
53053
53144

...and so on

Posted by Pelle Lonnborg on October 16, 2008 at 10:07 AM CEST #

I found this to be a bit slow and so I made up a "short" perl version, including man page.

#!/usr/bin/perl
use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

my $help= 0;
my $man= 0;
my $verbose= 0;
if ( not GetOptions(
'v|verbose+' => \\$verbose,
help => \\$help,
man => \\$man,
) or $help) {
pod2usage(1);
}
pod2usage(-exitstatus =>0, -verbose =>2) if $man;

open my $procinfo, '-|', '/usr/bin/pfiles `/usr/bin/ps -e -o pid` 2>/dev/null'
or die "Couldn't run the commands: $!";

my (
$pid,
$proc,
);
my $ports= join('|', map quotemeta,@ARGV);
$ports= qr/\\bport: ($ports)\\b/;

while (<$procinfo>) {
SWITCH: {
/\^(\\d+):\\s+(.\*)/ && do {
$pid= $1;
$proc=$2;
last SWITCH;
};
/$ports/ && do {
next unless defined $pid;
print "Port: $1 is being used by PID: $pid\\n";
if ($verbose) {
if ($verbose>1) {
print `/usr/bin/pargs -l $pid`;
}
elsif ($verbose>0) {
print $proc,"\\n";
}
print '-' x 65,"\\n";
}
last SWITCH;
};
}
}
close $procinfo;

=head1 NAME

procport - a tool to find who's using a port

=head1 SYNOPSIS

procport [options] pid ...

=head1 DESCRIPTION

procport is a script which allows zou to find out, which process is using a port.

=head1 OPTIONS

=over 8

=item B<-help>

display a short help.

=item B<-man>

display the man page.

=item B<-v>, B<-verbose>

print more information. Can be given several (2) times to increase verbosity.

=back

=head1 AUTHOR

Version 1.0 by Stephan [dot] Hradek [at] vodafone [dot] com

Posted by Stephan Hradek on March 31, 2009 at 06:06 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

joachimandres

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today