######################################################
# QueryPortL.pl
# 根据端口查询对应的进程(Suse Linux 版本)
# Author: Rick Cheng
# Date: 2005-10-29
# Version: 1.0
######################################################
#! /usr/bin/perl -w
use strict;
my @portList; #端口号列表
foreach (@ARGV)
{
push @portList, $_ if (/^[^-]/d+$/);
}
&checkUsage();
print "PORT/tPID/tCOMMAND/n"; #输出表格头
foreach (@portList)
{
my $port = $_;
my $pid = undef;
#根据端口定位PID
`lsof -i:$port > ./QueryPortL.port`;
open TMP, "./QueryPortL.port" or die "Can't open QueryPortL.port: $!";
my $index = 0;
while (<TMP>)
{
chomp;
if ($index == 1)
{
my @tokenList = split;
$pid = $tokenList[1];
}
$index++;
}
close TMP;
`rm ./QueryPortL.port`;
#未找到PID,跳出本次循环
unless (defined $pid)
{
print "$port/tNA/tNA/n";
next;
}
#根据PID定位进程名
`ps -ef | grep $pid | grep -v grep > ./QueryPortL.pid`;
open TMP, "./QueryPortL.pid" or die "Can't open QueryPortL.pid: $!";
while (<TMP>)
{
chomp;
my @tokenList = split;
if ($tokenList[1] == $pid)
{
print "$port/t$pid/t$tokenList[7]/n";
last;
}
}
close TMP;
`rm ./QueryPortL.pid`;
}
print ":Done!/n";
#检测命令行输入
sub checkUsage()
{
if ($#portList == -1)
{
print "[USAGE] perl QueryPortL.pl port1 port2 .../n";
exit -1;
}
}
本文提供了一个名为QueryPortL.pl的Perl脚本,用于在Suse Linux系统中根据指定的端口号查询相应的进程信息。作者Rick Cheng在2005年10月29日编写了这个1.0版本的脚本。脚本首先通过lsof命令获取端口占用的PID,然后使用ps命令和grep查找进程名称。
5090

被折叠的 条评论
为什么被折叠?



