什么进程正在侦听Solaris上的某个端口?

时间:2008-09-18 09:22:38

标签: unix solaris

所以我登录到Solaris框,尝试启动Apache,发现已经有一个进程正在侦听端口80,而且它不是Apache。我们的盒子没有安装lsof,所以我不能用它来查询。我想我能做到:

pfiles `ls /proc` | less

并寻找“端口:80”,但如果有人有更好的解决方案,我全都耳朵!如果我可以在没有root的情况下寻找听力过程,那就更好了。我对shell和C解决方案都持开放态度;我不介意在下次出现时随身携带一个小的自定义可执行文件。

更新:我说的是我不是管理员的solaris的通用安装(尽管我有超级用户访问权限),所以从免费软件磁盘安装东西不是一种选择。显然,两者都没有使用针对fuser,netstat或其他工具的特定于Linux的扩展。到目前为止,在所有进程上运行pfiles似乎是最好的解决方案,不幸的是。如果仍然如此,我可能会发布一个答案,其中包含一些稍微高效的代码。

11 个答案:

答案 0 :(得分:28)

我在某个地方找到了这个脚本。我不记得在哪里,但它对我有用:

#!/bin/ksh

line='---------------------------------------------'
pids=$(/usr/bin/ps -ef | sed 1d | awk '{print $2}')

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"
      /usr/bin/ps -ef -o pid -o args | egrep -v "grep|pfiles" | grep $f
   fi
done
exit 0

编辑:这是原始来源: [Solaris] Which process is bound to a given port ?

答案 1 :(得分:8)

这是一个单行:

ps -ef| awk '{print $2}'| xargs -I '{}' sh -c 'echo examining process {}; pfiles {}| grep 80'

'回声检查过程PID'将在每次搜索之前打印,因此一旦看到输出引用端口80,您就会知道哪个进程持有句柄。

或者使用:

ps -ef| grep $USER|awk '{print $2}'| xargs -I '{}' sh -c 'echo examining process {}; pfiles {}| grep 80'

由于'pfiles'可能不喜欢你试图访问其他用户的进程,除非你当然是root。

答案 2 :(得分:4)

Mavroprovato的回答报告的不仅仅是监听端口。侦听端口是没有对等端口的套接字。以下Perl程序仅报告侦听端口。它适用于SunOS 5.10。

#! /usr/bin/env perl
##
## Search the processes which are listening on the given port.
##
## For SunOS 5.10.
##

use strict;
use warnings;

die "Port missing" unless $#ARGV >= 0;
my $port = int($ARGV[0]);
die "Invalid port" unless $port > 0;

my @pids;
map { push @pids, $_ if $_ > 0; } map { int($_) } `ls /proc`;

foreach my $pid (@pids) {
    open (PF, "pfiles $pid 2>/dev/null |") 
        || warn "Can not read pfiles $pid";
    $_ = <PF>;
    my $fd;
    my $type;
    my $sockname;
    my $peername;
    my $report = sub {
        if (defined $fd) {
            if (defined $sockname && ! defined $peername) {
                print "$pid $type $sockname\n"; } } };
    while (<PF>) {
        if (/^\s*(\d+):.*$/) {
            &$report();
            $fd = int ($1);
            undef $type;
            undef $sockname;
            undef $peername; }
        elsif (/(SOCK_DGRAM|SOCK_STREAM)/) { $type = $1; }
        elsif (/sockname: AF_INET[6]? (.*)  port: $port/) {
            $sockname = $1; }
        elsif (/peername: AF_INET/) { $peername = 1; } }
    &$report();
    close (PF); }

答案 3 :(得分:3)

从Solaris 11.2开始,您确实可以使用netstat命令执行此操作。看看here-u开关是您正在寻找的。

如果您使用的是较低版本的Solaris,那么 - 正如其他人所指出的那样 - Solaris的这种做法是围绕pfiles命令的某种脚本包装。请注意,pfiles命令会暂停该过程一秒钟以检查它。对于99.9%的过程而言,这并不重要。不幸的是,如果使用pfiles命令命中,我们有一个进程可以提供核心转储,因此我们对使用该命令有点谨慎。如果你处于99.9%,你的情况可能会完全不同,这意味着你可以安全地使用pfiles命令。

答案 4 :(得分:2)

#!/usr/bin/bash
# This is a little script based on the "pfiles" solution that prints the PID and PORT.

pfiles `ls /proc` 2>/dev/null | awk "/^[^ \\t]/{smatch=\$0;next}/port:[ \\t]*${1}/{print smatch, \$0}{next}"

答案 5 :(得分:2)

Solaris上的netstat不会告诉你这个,也不会告诉你旧版本的lsof,但如果你下载并构建/安装更新版本的lsof,这可以告诉你。

$ lsof -v
lsof version information:
    revision: 4.85
    latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
    latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
    latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
    configuration info: 64 bit kernel
    constructed: Fri Mar 7 10:32:54 GMT 2014
    constructed by and on: user@hostname
    compiler: gcc
    compiler version: 3.4.3 (csl-sol210-3_4-branch+sol_rpath)
    8<- - - - ***SNIP*** - - -

使用此选项可以使用-i选项:

$ lsof -i:22
COMMAND   PID     USER   FD   TYPE             DEVICE   SIZE/OFF NODE NAME
sshd      521     root    3u  IPv6 0xffffffff89c67580        0t0  TCP *:ssh (LISTEN)
sshd     5090     root    3u  IPv6 0xffffffffa8668580   0t322598  TCP host.domain.com:ssh->21.43.65.87:52364 (ESTABLISHED)
sshd     5091   johngh    4u  IPv6 0xffffffffa8668580   0t322598  TCP host.domain.com:ssh->21.43.65.87:52364 (ESTABLISHED)

它向您显示了您正在要求的内容。

我昨天遇到了一个崩溃的Jetty(Java)进程问题,该进程只在其/ proc / [PID]目录中保留了2个文件(psinfo&amp; usage)。

pfiles未能找到该过程(因为它所需的日期不存在)

lsof找到了它。

答案 6 :(得分:1)

您可能不想,但最好的办法是抓住sunfreeware CD并安装lsof。

除此之外,是的,你可以使用shell脚本在/ proc中徘徊。

答案 7 :(得分:0)

最可能是sun的管理服务器.. 它通常与sun的目录以及默认安装中的一些其他webmin-ish内容捆绑在一起

答案 8 :(得分:0)

这是一种间接方法,但您可以看到网站是否在您选择的Web浏览器上加载了端口80上运行的任何内容。或者您可以telnet到端口80并查看是否收到了回复关于该端口上运行的内容的线索,您可以将其关闭。由于端口80是http流量的默认端口,因此默认情况下会运行某种http服务器,但不能保证。

答案 9 :(得分:0)

我认为第一个答案是最好的 我编写了自己的shell脚本来开发这个想法:

#!/bin/sh
if [ $# -ne 1 ]
then
    echo "Sintaxis:\n\t"
    echo " $0 {port to search in process }"
    exit
else
    MYPORT=$1
    for i in `ls /proc`
    do

       pfiles $i | grep port | grep "port: $MYPORT" > /dev/null
       if [ $? -eq 0 ]
         then
           echo " Port $MYPORT founded in $i proccess !!!\n\n"
           echo "Details\n\t"
           pfiles $i | grep port | grep "port: $MYPORT"
           echo "\n\t"
           echo "Process detail: \n\t"
           ps -ef | grep $i  | grep -v grep
       fi
    done
fi

答案 10 :(得分:-3)

如果您有权访问netstat,那就可以做到。