Ad Widget

Collapse

check if port is in use by certain program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frater
    Senior Member
    • Oct 2010
    • 340

    #1

    check if port is in use by certain program

    Sometimes you can have 2 programs fighting for a socket.
    I'm running Plesk on some servers and it's controlling most of the services.
    Although it's being told in the webif that it shouldn't use port 587 for SMTPAUTH it sometimes is in some state that it still does....

    I'm normally running an ASSP proxy on that port.
    I had some people that couldn't use SMTP-AUTH and after some debugging I found out it was postfix itself listening to port 587 for a week.
    This went unnoticed because there was actually something listening on port 587 (I even test the certificate with Zabbix) and most people didn't have any problems.

    I therefore created this little test which will check if a certain port is taken by a certain program. It returns an unsigned numeric...

    Have fun....

    Code:
    Number of running assp.pl listening on port 587
    net.statproc[587, assp.pl]
    unsigned numeric
    Decimal
    60 seconds interval
    Code:
    UserParameter=net.statproc[*], sudo /usr/local/sbin/statproc "$1" "$2"
    # cat /usr/local/sbin/statproc
    Code:
    #!/bin/sh
    PORT=$1
    PROC=$2
    /bin/netstat -lntp 2>/dev/null | grep LISTEN |  awk '{print $4" "$7}' | sed 's/.*:/:/;s/ [0-9]*\// /' | grep -c "^:${PORT} ${PROC}"

    Because I was busy anyhow I immediately created this informative little tool that would give me all the procs that are tied to a tcp port...

    # cat /usr/local/sbin/statprocs
    Code:
    #!/bin/sh
    /bin/netstat -lntp 2>/dev/null | grep LISTEN |  awk '{print $4" "$7}' | tr -d '()' | sed 's/.*://;s/ [0-9]*\//\t/' | sort -rn | uniq
    Last edited by frater; 21-03-2014, 10:52.
    Zabbix agents on Linux, FreeBSD, Windows, AVM-Fritz!box, DD-WRT and QNAP
Working...