Tag Archives: script

Enumerate applications listening to a computer port

How to figure which application/process is already using a port?

For example, below apache is failing to start because port 80 is already taken by some other application.

Apr 11 21:08:13 ip-172-26-0-194 systemd[1]: Starting The Apache HTTP Server...
Apr 11 21:08:13 ip-172-26-0-194 apachectl[17997]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
Apr 11 21:08:13 ip-172-26-0-194 apachectl[17997]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

Here is a script which can help you find this info.

application_port_list.sh
# list out the processes using a port.
# This script does not give the exact answer but potential solutions. It uses
# three tools one after another.
#
# - lsof
# - netstat
# - ss
set -e
PORT="$1"
echo "Looking for application using port $PORT"
if command -v lsof &> /dev/null
then
sudo lsof -i :$PORT
fi
if command -v netstat &> /dev/null
then
netstat -tulpn | grep ":$PORT"
exit;
fi
if command -v ss &> /dev/null
then
sudo ss -tulp | grep ":$PORT"
exit;
fi

self-hosted ntfy service with admin account

  • First, we need a script that setup admin account for us after starting the service. init.sh file that you should put inside ntfy/init.sh
    #!/bin/sh
    ntfy serve &
    sleep 2
    NTFY_PASSWORD=${NTFY_PASSWORD} ntfy user add --role=admin ${NTFY_USER}
    wait
    
  • Now the compose file compose.yaml or docker-compose.yaml file.
    services:
      ntfy:
        image: binwiederhier/ntfy
        container_name: ntfy
        entrypoint: [ "/bin/sh", "/var/lib/ntfy/init.sh" ]
        environment:
          - TZ=UTC    \# optional: set desired timezone
          - NTFY_BASE_URL=https://ntfy.dilawars.me
          - NTFY_AUTH_FILE=/var/lib/ntfy/auth.db
          - NTFY_CACHE_FILE=/var/lib/ntfy/cache.db
          - NTFY_AUTH_DEFAULT_ACCESS=deny-all
          - NTFY_ATTACHMENT_CACHE_DIR=/var/lib/ntfy/attachments
          - NTFY_ENABLE_LOGIN=true
          - NTFY_USER=admin
          - NTFY_PASSWORD=yoyodillusingh
        volumes:
          - /var/cache/ntfy:/var/cache/ntfy
          - /etc/ntfy:/etc/ntfy
          - ./ntfy/:/var/lib/ntfy
        ports:
          - 8001:80
        healthcheck: \# optional: remember to adapt the host:port to your environment
            test: ["CMD-SHELL", "wget -q --tries=1 http://localhost:80/v1/health -O - | grep -Eo '\"healthy\"\\s*:\\s*true' || exit 1"]
            interval: 60s
            timeout: 10s
            retries: 3
            start_period: 40s
        restart: unless-stopped
    

A python script to uninstall pkg file on MacOS

https://github.com/dilawar/Scripts/blob/master/pkg_uninstall.py

Usage

Pass pkg name and it will show you installed packages that matches the query and can be removed.

dilawar@halwa ~/Scripts (master)> sudo python3 pkg_uninstall.py clamav
0: com.cisco.ClamAV.programs
1: com.cisco.ClamAV.libraries
2: com.cisco.ClamAV.documentation
Select a package to uninstall 0            
Uninstalling com.cisco.ClamAV.programs
Forgot package 'com.cisco.ClamAV.programs' on '/'.
dilawar@halwa ~/Scripts (master)> sudo python3 pkg_uninstall.py clamav
0: com.cisco.ClamAV.libraries
1: com.cisco.ClamAV.documentation
Select a package to uninstall 0
Uninstalling com.cisco.ClamAV.libraries
Forgot package 'com.cisco.ClamAV.libraries' on '/'.

And that’s it.