Advertisement
jcofer555

check if port in use

Jun 3rd, 2025 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.81 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script checks unraids webui ports, docker container ports, ports that VM's use for vnc, and whether VM manager service is running which then uses port 53
  4.  
  5. # Change this to the port you want to search
  6. PORT_TO_SEARCH="61208"
  7.  
  8. # DON'T CHANGE ANYTHING BELOW
  9.  
  10. # Check Unraid WebUI ports
  11. UNRAID_HTTP_PORT=$(grep -E "PORT=" /boot/config/ident.cfg | sed -E 's/[^0-9]//g')
  12. UNRAID_HTTPS_PORT=$(grep -E "PORTSSL=" /boot/config/ident.cfg | sed -E 's/[^0-9]//g')
  13.  
  14. # Check if VM Manager service (libvirt) is running
  15. VM_MANAGER_ACTIVE=$(pgrep -x libvirtd)
  16.  
  17. # Check docker container ports
  18. RESULT=$(docker ps -q | xargs -I {} docker inspect --format='{{.Name}} {{range $k, $v := .NetworkSettings.Ports}}{{if $v}}{{(index $v 0).HostPort}}{{end}}{{end}}' {} | sed 's/^\///' | awk -v port="$PORT_TO_SEARCH" '$2 == port {print $1}')
  19.  
  20. # Check VM VNC ports
  21. VM_NAMES=$(virsh list --name)
  22. for VM in $VM_NAMES; do
  23.     VNC_PORT=$(virsh dumpxml "$VM" | grep "graphics type='vnc'" | sed -E "s/.*port='([0-9]+)'.*/\1/")
  24.    
  25.     if [[ -n "$VNC_PORT" && "$PORT_TO_SEARCH" == "$VNC_PORT" ]]; then
  26.         echo "Port $PORT_TO_SEARCH is in use by VM '$VM' for VNC."
  27.         exit 0
  28.     fi
  29. done
  30.  
  31. # Check what the searched port matches
  32. if [[ "$PORT_TO_SEARCH" == "$UNRAID_HTTP_PORT" ]]; then
  33.     echo "Port $PORT_TO_SEARCH is being used by Unraid's WebUI for HTTP."
  34. elif [[ "$PORT_TO_SEARCH" == "$UNRAID_HTTPS_PORT" ]]; then
  35.     echo "Port $PORT_TO_SEARCH is being used by Unraid's WebUI for HTTPS."
  36. elif [[ "$PORT_TO_SEARCH" == "53" && -n "$VM_MANAGER_ACTIVE" ]]; then
  37.     echo "Port $PORT_TO_SEARCH is in use because Unraid's VM Manager service is running."
  38. elif [[ -z "$RESULT" ]]; then
  39.     echo "Nothing that is checked is using port $PORT_TO_SEARCH."
  40. else
  41.     echo "$RESULT container has port $PORT_TO_SEARCH in use."
  42. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement