What processes are using which ports on unix?

半世苍凉 提交于 2019-11-28 18:37:06

问题


I need to find out what ports are attached to which processes on a Unix machine (HP Itanium). Unfortunately, lsof is not installed and I have no way of installing it.

Does anyone know an alternative method? A fairly lengthy Googling session hasn't turned up anything.


回答1:


Assuming this is HP-UX? What about the Ptools - do you have those installed? If so you can use "pfiles" to find the ports in use by the application:

pfiles prints information about all open file descriptors of a process. If file descriptor corresponds to a file, then pfiles prints the fstat(2) and fcntl(2) information.

If the file descriptor corresponds to a socket, then pfiles prints socket related info, such as the socket type, socket family, and protocol family.

In the case of AF_INET and AF_INET6 family of sockets, information about the peer host is also printed.

for f in $(ps -ex | awk '{print $1}'); do echo $f; pfiles $f | grep PORTNUM; done

switch PORTNUM for the port number. :) may be child pid, but gets you close enough to identify the problem app.




回答2:


netstat -l (assuming it comes with that version of UNIX)




回答3:


Given (almost) everything on unix is a file, and lsof lists open files...

Linux : netstat -putan or lsof | grep TCP

OSX : lsof | grep TCP

Other Unixen : lsof way...




回答4:


netstat -pln

EDIT: linux only, on other UNIXes netstat may not support all these options.




回答5:


netstat -ln | awk '/^(tcp|udp)/ { split($4, a, /:/); print $1, a[2]}' | sort -u

gives you the active tcp/udp ports. Then you can use the ports with fuser -n tcp or fuser -n udp, as root, and supposing that fuser is GNU fuser or has similar options.

If you need more help, let me know.




回答6:


Try pfiles PID to show all open files for a process.




回答7:


Which process uses port in unix;

1. netstat -Aan | grep port

root> netstat -Aan | grep 3872

output> f1000e000bb5c3b8 tcp 0 0 *.3872 . LISTEN

2. rmsock f1000e000bb5c3b8 tcpcb

output> The socket 0xf1000e000bb5c008 is being held by proccess 13959354 (java).

3. ps -ef | grep 13959354




回答8:


I use this command:

netstat -tulpn | grep LISTEN

You can have a clean output that shows process id and ports that's listening on




回答9:


If you want to know all listening ports along with its details: local address, foreign address and state as well as Process ID (PID). You can use following command for it in linux.

 netstat -tulpn


来源:https://stackoverflow.com/questions/126002/what-processes-are-using-which-ports-on-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!