Port inuse function from bash to Go
This commit is contained in:
286
.bashrc
286
.bashrc
@@ -70,292 +70,6 @@ alias gcb='git checkout -b'
|
||||
alias kubectl="minikube kubectl --"
|
||||
alias zed=zeditor
|
||||
|
||||
# Check if a specific port is in use with detailed process information
|
||||
inuse() {
|
||||
# Color definitions
|
||||
local RED='\033[0;31m'
|
||||
local GREEN='\033[0;32m'
|
||||
local YELLOW='\033[1;33m'
|
||||
local BLUE='\033[0;34m'
|
||||
local CYAN='\033[0;36m'
|
||||
local BOLD='\033[1m'
|
||||
local NC='\033[0m' # No Color
|
||||
|
||||
# Input validation
|
||||
if [ $# -eq 0 ]; then
|
||||
echo -e "${RED}Usage:${NC} inuse <port_number>"
|
||||
echo -e "${YELLOW} inuse --list${NC}"
|
||||
echo -e "${YELLOW} inuse --help${NC}"
|
||||
echo -e "${YELLOW}Example:${NC} inuse 80"
|
||||
echo -e "${YELLOW} inuse --list${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Handle --help option
|
||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo -e "${CYAN}${BOLD}inuse - Check if a port is in use${NC}"
|
||||
echo
|
||||
echo -e "${BOLD}USAGE:${NC}"
|
||||
echo -e " inuse <port_number> Check if a specific port is in use"
|
||||
echo -e " inuse --list, -l List all Docker services with listening ports"
|
||||
echo -e " inuse --help, -h Show this help message"
|
||||
echo
|
||||
echo -e "${BOLD}EXAMPLES:${NC}"
|
||||
echo -e " ${GREEN}inuse 80${NC} Check if port 80 is in use"
|
||||
echo -e " ${GREEN}inuse 3000${NC} Check if port 3000 is in use"
|
||||
echo -e " ${GREEN}inuse --list${NC} Show all Docker services with ports"
|
||||
echo
|
||||
echo -e "${BOLD}DESCRIPTION:${NC}"
|
||||
echo -e " The inuse function checks if a specific port is in use and identifies"
|
||||
echo -e " the process using it. It can detect regular processes, Docker containers"
|
||||
echo -e " with published ports, and containers using host networking."
|
||||
echo
|
||||
echo -e "${BOLD}OUTPUT:${NC}"
|
||||
echo -e " ${GREEN}✓${NC} Port is in use - shows process name, PID, and Docker info if applicable"
|
||||
echo -e " ${RED}✗${NC} Port is free"
|
||||
echo -e " ${YELLOW}⚠${NC} Port is in use but process cannot be identified"
|
||||
echo
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Handle --list option
|
||||
if [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
|
||||
if ! command -v docker >/dev/null 2>&1; then
|
||||
echo -e "${RED}Error:${NC} Docker is not available"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo -e "${CYAN}${BOLD}Docker Services with Listening Ports:${NC}"
|
||||
echo
|
||||
|
||||
# Get all running containers
|
||||
local containers=$(docker ps --format "{{.Names}}" 2>/dev/null)
|
||||
if [ -z "$containers" ]; then
|
||||
echo -e "${YELLOW}No running Docker containers found${NC}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local found_services=false
|
||||
while IFS= read -r container; do
|
||||
# Get port mappings for this container
|
||||
local ports=$(docker port "$container" 2>/dev/null)
|
||||
if [ -n "$ports" ]; then
|
||||
# Get container image name (clean it up)
|
||||
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
||||
local clean_image=$(echo "$image" | sed 's/sha256:[a-f0-9]*/[image-hash]/' | sed 's/^.*\///')
|
||||
|
||||
echo -e "${GREEN}📦 ${BOLD}$container${NC} ${CYAN}($clean_image)${NC}"
|
||||
|
||||
# Parse and display ports nicely
|
||||
echo "$ports" | while IFS= read -r port_line; do
|
||||
if [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*0\.0\.0\.0:([0-9]+) ]]; then
|
||||
local container_port="${BASH_REMATCH[1]}"
|
||||
local protocol="${BASH_REMATCH[2]}"
|
||||
local host_port="${BASH_REMATCH[3]}"
|
||||
echo -e "${CYAN} ├─ Port ${BOLD}$host_port${NC}${CYAN} → $container_port ($protocol)${NC}"
|
||||
elif [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*\[::\]:([0-9]+) ]]; then
|
||||
local container_port="${BASH_REMATCH[1]}"
|
||||
local protocol="${BASH_REMATCH[2]}"
|
||||
local host_port="${BASH_REMATCH[3]}"
|
||||
echo -e "${CYAN} ├─ Port ${BOLD}$host_port${NC}${CYAN} → $container_port ($protocol) [IPv6]${NC}"
|
||||
fi
|
||||
done
|
||||
echo
|
||||
found_services=true
|
||||
fi
|
||||
done <<< "$containers"
|
||||
|
||||
# Also check for host networking containers
|
||||
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
||||
if [ -n "$host_containers" ]; then
|
||||
echo -e "${YELLOW}${BOLD}Host Networking Containers:${NC}"
|
||||
while IFS= read -r container; do
|
||||
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
||||
local clean_image=$(echo "$image" | sed 's/sha256:[a-f0-9]*/[image-hash]/' | sed 's/^.*\///')
|
||||
echo -e "${YELLOW}🌐 ${BOLD}$container${NC} ${CYAN}($clean_image)${NC} ${YELLOW}- uses host networking${NC}"
|
||||
done <<< "$host_containers"
|
||||
echo
|
||||
found_services=true
|
||||
fi
|
||||
|
||||
if [ "$found_services" = false ]; then
|
||||
echo -e "${YELLOW}No Docker services with exposed ports found${NC}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
local port="$1"
|
||||
|
||||
# Validate port number
|
||||
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
|
||||
echo -e "${RED}Error:${NC} Invalid port number. Must be between 1 and 65535."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if port is in use first
|
||||
local port_in_use=false
|
||||
if command -v ss >/dev/null 2>&1; then
|
||||
if ss -tulpn 2>/dev/null | grep -q ":$port "; then
|
||||
port_in_use=true
|
||||
fi
|
||||
elif command -v netstat >/dev/null 2>&1; then
|
||||
if netstat -tulpn 2>/dev/null | grep -q ":$port "; then
|
||||
port_in_use=true
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$port_in_use" = false ]; then
|
||||
echo -e "${RED}✗ Port $port is FREE${NC}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Port is in use, now find what's using it
|
||||
local found_process=false
|
||||
|
||||
# Method 1: Try netstat first (most reliable for PID info)
|
||||
if command -v netstat >/dev/null 2>&1; then
|
||||
local netstat_result=$(netstat -tulpn 2>/dev/null | grep ":$port ")
|
||||
if [ -n "$netstat_result" ]; then
|
||||
while IFS= read -r line; do
|
||||
local pid=$(echo "$line" | awk '{print $7}' | cut -d'/' -f1)
|
||||
local process_name=$(echo "$line" | awk '{print $7}' | cut -d'/' -f2)
|
||||
local protocol=$(echo "$line" | awk '{print $1}')
|
||||
|
||||
if [[ "$pid" =~ ^[0-9]+$ ]] && [ -n "$process_name" ]; then
|
||||
# Check if it's a Docker container
|
||||
local docker_info=""
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
# Check for docker-proxy
|
||||
if [ "$process_name" = "docker-proxy" ]; then
|
||||
local container_name=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
||||
if [ -n "$container_name" ]; then
|
||||
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
||||
else
|
||||
docker_info=" ${CYAN}(Docker proxy)${NC}"
|
||||
fi
|
||||
else
|
||||
# Check if process is in a container by examining cgroup
|
||||
if [ -f "/proc/$pid/cgroup" ] && grep -q docker "/proc/$pid/cgroup" 2>/dev/null; then
|
||||
local container_id=$(cat "/proc/$pid/cgroup" 2>/dev/null | grep docker | grep -o '[a-f0-9]\{64\}' | head -1)
|
||||
if [ -n "$container_id" ]; then
|
||||
local container_name=$(docker inspect "$container_id" 2>/dev/null | grep -o '"Name": *"[^"]*"' | cut -d'"' -f4 | sed 's/^\/*//' | head -1)
|
||||
if [ -n "$container_name" ]; then
|
||||
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
||||
else
|
||||
docker_info=" ${CYAN}(Docker: ${container_id:0:12})${NC}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
||||
found_process=true
|
||||
fi
|
||||
done <<< "$netstat_result"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 2: Try ss if netstat didn't work
|
||||
if [ "$found_process" = false ] && command -v ss >/dev/null 2>&1; then
|
||||
local ss_result=$(ss -tulpn 2>/dev/null | grep ":$port ")
|
||||
if [ -n "$ss_result" ]; then
|
||||
while IFS= read -r line; do
|
||||
local pid=$(echo "$line" | grep -o 'pid=[0-9]*' | cut -d'=' -f2)
|
||||
local protocol=$(echo "$line" | awk '{print $1}')
|
||||
|
||||
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
||||
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
||||
if [ -n "$process_name" ]; then
|
||||
# Check for Docker container
|
||||
local docker_info=""
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
if [ "$process_name" = "docker-proxy" ]; then
|
||||
local container_name=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
||||
if [ -n "$container_name" ]; then
|
||||
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
||||
else
|
||||
docker_info=" ${CYAN}(Docker proxy)${NC}"
|
||||
fi
|
||||
elif [ -f "/proc/$pid/cgroup" ] && grep -q docker "/proc/$pid/cgroup" 2>/dev/null; then
|
||||
local container_id=$(cat "/proc/$pid/cgroup" 2>/dev/null | grep docker | grep -o '[a-f0-9]\{64\}' | head -1)
|
||||
if [ -n "$container_id" ]; then
|
||||
local container_name=$(docker inspect "$container_id" 2>/dev/null | grep -o '"Name": *"[^"]*"' | cut -d'"' -f4 | sed 's/^\/*//' | head -1)
|
||||
if [ -n "$container_name" ]; then
|
||||
docker_info=" ${CYAN}(Docker: $container_name)${NC}"
|
||||
else
|
||||
docker_info=" ${CYAN}(Docker: ${container_id:0:12})${NC}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
||||
found_process=true
|
||||
fi
|
||||
fi
|
||||
done <<< "$ss_result"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 3: Try fuser as last resort
|
||||
if [ "$found_process" = false ] && command -v fuser >/dev/null 2>&1; then
|
||||
local fuser_pids=$(fuser "$port/tcp" 2>/dev/null)
|
||||
if [ -n "$fuser_pids" ]; then
|
||||
for pid in $fuser_pids; do
|
||||
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
||||
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
||||
if [ -n "$process_name" ]; then
|
||||
echo -e "${GREEN}✓ Port $port (tcp) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}"
|
||||
found_process=true
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
# Method 4: Check for Docker containers more accurately
|
||||
if [ "$found_process" = false ] && command -v docker >/dev/null 2>&1; then
|
||||
# First, try to find containers with published ports matching our port
|
||||
local container_with_port=$(docker ps --format "{{.Names}}" --filter "publish=$port" 2>/dev/null | head -1)
|
||||
if [ -n "$container_with_port" ]; then
|
||||
local image=$(docker inspect "$container_with_port" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
||||
echo -e "${GREEN}✓ Port $port in use by Docker container ${BOLD}$container_with_port${NC} ${CYAN}(published port, image: $image)${NC}"
|
||||
found_process=true
|
||||
else
|
||||
# Only check host networking containers if we haven't found anything else
|
||||
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
||||
if [ -n "$host_containers" ]; then
|
||||
local host_container_count=$(echo "$host_containers" | wc -l)
|
||||
if [ "$host_container_count" -eq 1 ]; then
|
||||
# Only one host networking container, likely candidate
|
||||
local image=$(docker inspect "$host_containers" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
||||
echo -e "${YELLOW}⚠ Port $port possibly in use by Docker container ${BOLD}$host_containers${NC} ${CYAN}(host networking, image: $image)${NC}"
|
||||
found_process=true
|
||||
else
|
||||
# Multiple host networking containers, can't determine which one
|
||||
echo -e "${YELLOW}⚠ Port $port is in use, multiple Docker containers using host networking:${NC}"
|
||||
while IFS= read -r container; do
|
||||
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
||||
echo -e "${CYAN} - $container (image: $image)${NC}"
|
||||
done <<< "$host_containers"
|
||||
found_process=true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# If we still haven't found the process, show a generic message
|
||||
if [ "$found_process" = false ]; then
|
||||
echo -e "${YELLOW}⚠ Port $port is in use but unable to identify the process${NC}"
|
||||
echo -e "${CYAN} This might be due to insufficient permissions or the process being in a different namespace${NC}"
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# random string (Syntax: random <length>)
|
||||
alias random='openssl rand -base64'
|
||||
|
||||
|
Reference in New Issue
Block a user