2

I'm looking to bind to a port for SSH tunnelling via a simple shell script (likely: BASH), and I'd like a more ... "sane" manner of testing whether or not the port is already bound to another process, and I'd rather not like to grok (read: grep) the output of netstat.

Is there some command or utility that makes doing this sane? I either have terrible google-fu and a terrible vocabulary, or most people don't try to iterate over ports programmatically via a shell, but instead via a programming language.

1
  • 3
    A bit less heavy than netstat, though still same concept: fuser <port>/tcp. Nov 5, 2009 at 1:50

2 Answers 2

6

you could use lsof:

-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]

lsof -i [email protected]:80
2
  • 1
    You were a few seconds ahead of me :)
    – Alex
    Nov 5, 2009 at 2:59
  • 1
    While lsof works, looping through every process and it's open ports is slow. netstat -tan will give you the list of open ports.
    – David
    Nov 5, 2009 at 3:42
2

Another option would be lsof. You can do something like:

lsof -i tcp:<port> -s tcp:listen

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .