fix: set default ssh sock based on what is available instead of forcing 1password locally
Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
59
.bashrc
59
.bashrc
@@ -12,7 +12,7 @@ fi
|
|||||||
if [ -f /etc/os-release ]; then
|
if [ -f /etc/os-release ]; then
|
||||||
distro=$(awk -F= '/^NAME/{print $ssss2}' /etc/os-release | tr -d '"')
|
distro=$(awk -F= '/^NAME/{print $ssss2}' /etc/os-release | tr -d '"')
|
||||||
if [[ "$distro" == *"Pop!_OS"* ]]; then
|
if [[ "$distro" == *"Pop!_OS"* ]]; then
|
||||||
export CGO_CFLAGS="-I/usr/include"
|
export CGO_CFLAGS="-I/usr/include"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -22,6 +22,11 @@ if [[ "$(uname -a)" == *"microsoft-standard-WSL2"* ]]; then
|
|||||||
alias winget='winget.exe'
|
alias winget='winget.exe'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Set SSH_AUTH_SOCK to ~/.1password/agent.sock, but only if we don't already have a SSH_AUTH_SOCK
|
||||||
|
if [ -z "$SSH_AUTH_SOCK" ]; then
|
||||||
|
export SSH_AUTH_SOCK=~/.1password/agent.sock
|
||||||
|
fi
|
||||||
|
|
||||||
# Docker Compose Alias (Mostly for old shell scripts)
|
# Docker Compose Alias (Mostly for old shell scripts)
|
||||||
alias docker-compose='docker compose'
|
alias docker-compose='docker compose'
|
||||||
|
|
||||||
@@ -77,7 +82,7 @@ inuse() {
|
|||||||
local CYAN='\033[0;36m'
|
local CYAN='\033[0;36m'
|
||||||
local BOLD='\033[1m'
|
local BOLD='\033[1m'
|
||||||
local NC='\033[0m' # No Color
|
local NC='\033[0m' # No Color
|
||||||
|
|
||||||
# Input validation
|
# Input validation
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
echo -e "${RED}Usage:${NC} inuse <port_number>"
|
echo -e "${RED}Usage:${NC} inuse <port_number>"
|
||||||
@@ -87,7 +92,7 @@ inuse() {
|
|||||||
echo -e "${YELLOW} inuse --list${NC}"
|
echo -e "${YELLOW} inuse --list${NC}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Handle --help option
|
# Handle --help option
|
||||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||||
echo -e "${CYAN}${BOLD}inuse - Check if a port is in use${NC}"
|
echo -e "${CYAN}${BOLD}inuse - Check if a port is in use${NC}"
|
||||||
@@ -114,24 +119,24 @@ inuse() {
|
|||||||
echo
|
echo
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Handle --list option
|
# Handle --list option
|
||||||
if [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
|
if [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
|
||||||
if ! command -v docker >/dev/null 2>&1; then
|
if ! command -v docker >/dev/null 2>&1; then
|
||||||
echo -e "${RED}Error:${NC} Docker is not available"
|
echo -e "${RED}Error:${NC} Docker is not available"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "${CYAN}${BOLD}Docker Services with Listening Ports:${NC}"
|
echo -e "${CYAN}${BOLD}Docker Services with Listening Ports:${NC}"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# Get all running containers
|
# Get all running containers
|
||||||
local containers=$(docker ps --format "{{.Names}}" 2>/dev/null)
|
local containers=$(docker ps --format "{{.Names}}" 2>/dev/null)
|
||||||
if [ -z "$containers" ]; then
|
if [ -z "$containers" ]; then
|
||||||
echo -e "${YELLOW}No running Docker containers found${NC}"
|
echo -e "${YELLOW}No running Docker containers found${NC}"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local found_services=false
|
local found_services=false
|
||||||
while IFS= read -r container; do
|
while IFS= read -r container; do
|
||||||
# Get port mappings for this container
|
# Get port mappings for this container
|
||||||
@@ -140,9 +145,9 @@ inuse() {
|
|||||||
# Get container image name (clean it up)
|
# Get container image name (clean it up)
|
||||||
local image=$(docker inspect "$container" 2>/dev/null | grep -o '"Image": *"[^"]*"' | cut -d'"' -f4 | head -1)
|
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/^.*\///')
|
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}"
|
echo -e "${GREEN}📦 ${BOLD}$container${NC} ${CYAN}($clean_image)${NC}"
|
||||||
|
|
||||||
# Parse and display ports nicely
|
# Parse and display ports nicely
|
||||||
echo "$ports" | while IFS= read -r port_line; do
|
echo "$ports" | while IFS= read -r port_line; do
|
||||||
if [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*0\.0\.0\.0:([0-9]+) ]]; then
|
if [[ "$port_line" =~ ([0-9]+)/(tcp|udp).*0\.0\.0\.0:([0-9]+) ]]; then
|
||||||
@@ -161,7 +166,7 @@ inuse() {
|
|||||||
found_services=true
|
found_services=true
|
||||||
fi
|
fi
|
||||||
done <<< "$containers"
|
done <<< "$containers"
|
||||||
|
|
||||||
# Also check for host networking containers
|
# Also check for host networking containers
|
||||||
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
local host_containers=$(docker ps --format "{{.Names}}" --filter "network=host" 2>/dev/null)
|
||||||
if [ -n "$host_containers" ]; then
|
if [ -n "$host_containers" ]; then
|
||||||
@@ -174,22 +179,22 @@ inuse() {
|
|||||||
echo
|
echo
|
||||||
found_services=true
|
found_services=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$found_services" = false ]; then
|
if [ "$found_services" = false ]; then
|
||||||
echo -e "${YELLOW}No Docker services with exposed ports found${NC}"
|
echo -e "${YELLOW}No Docker services with exposed ports found${NC}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local port="$1"
|
local port="$1"
|
||||||
|
|
||||||
# Validate port number
|
# Validate port number
|
||||||
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
|
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."
|
echo -e "${RED}Error:${NC} Invalid port number. Must be between 1 and 65535."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if port is in use first
|
# Check if port is in use first
|
||||||
local port_in_use=false
|
local port_in_use=false
|
||||||
if command -v ss >/dev/null 2>&1; then
|
if command -v ss >/dev/null 2>&1; then
|
||||||
@@ -201,15 +206,15 @@ inuse() {
|
|||||||
port_in_use=true
|
port_in_use=true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$port_in_use" = false ]; then
|
if [ "$port_in_use" = false ]; then
|
||||||
echo -e "${RED}✗ Port $port is FREE${NC}"
|
echo -e "${RED}✗ Port $port is FREE${NC}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Port is in use, now find what's using it
|
# Port is in use, now find what's using it
|
||||||
local found_process=false
|
local found_process=false
|
||||||
|
|
||||||
# Method 1: Try netstat first (most reliable for PID info)
|
# Method 1: Try netstat first (most reliable for PID info)
|
||||||
if command -v netstat >/dev/null 2>&1; then
|
if command -v netstat >/dev/null 2>&1; then
|
||||||
local netstat_result=$(netstat -tulpn 2>/dev/null | grep ":$port ")
|
local netstat_result=$(netstat -tulpn 2>/dev/null | grep ":$port ")
|
||||||
@@ -218,7 +223,7 @@ inuse() {
|
|||||||
local pid=$(echo "$line" | awk '{print $7}' | cut -d'/' -f1)
|
local pid=$(echo "$line" | awk '{print $7}' | cut -d'/' -f1)
|
||||||
local process_name=$(echo "$line" | awk '{print $7}' | cut -d'/' -f2)
|
local process_name=$(echo "$line" | awk '{print $7}' | cut -d'/' -f2)
|
||||||
local protocol=$(echo "$line" | awk '{print $1}')
|
local protocol=$(echo "$line" | awk '{print $1}')
|
||||||
|
|
||||||
if [[ "$pid" =~ ^[0-9]+$ ]] && [ -n "$process_name" ]; then
|
if [[ "$pid" =~ ^[0-9]+$ ]] && [ -n "$process_name" ]; then
|
||||||
# Check if it's a Docker container
|
# Check if it's a Docker container
|
||||||
local docker_info=""
|
local docker_info=""
|
||||||
@@ -246,14 +251,14 @@ inuse() {
|
|||||||
fi
|
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"
|
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
||||||
found_process=true
|
found_process=true
|
||||||
fi
|
fi
|
||||||
done <<< "$netstat_result"
|
done <<< "$netstat_result"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Method 2: Try ss if netstat didn't work
|
# Method 2: Try ss if netstat didn't work
|
||||||
if [ "$found_process" = false ] && command -v ss >/dev/null 2>&1; then
|
if [ "$found_process" = false ] && command -v ss >/dev/null 2>&1; then
|
||||||
local ss_result=$(ss -tulpn 2>/dev/null | grep ":$port ")
|
local ss_result=$(ss -tulpn 2>/dev/null | grep ":$port ")
|
||||||
@@ -261,7 +266,7 @@ inuse() {
|
|||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
local pid=$(echo "$line" | grep -o 'pid=[0-9]*' | cut -d'=' -f2)
|
local pid=$(echo "$line" | grep -o 'pid=[0-9]*' | cut -d'=' -f2)
|
||||||
local protocol=$(echo "$line" | awk '{print $1}')
|
local protocol=$(echo "$line" | awk '{print $1}')
|
||||||
|
|
||||||
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
if [[ "$pid" =~ ^[0-9]+$ ]]; then
|
||||||
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
local process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
|
||||||
if [ -n "$process_name" ]; then
|
if [ -n "$process_name" ]; then
|
||||||
@@ -287,7 +292,7 @@ inuse() {
|
|||||||
fi
|
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"
|
echo -e "${GREEN}✓ Port $port ($protocol) in use by ${BOLD}$process_name${NC} ${GREEN}as PID ${BOLD}$pid${NC}$docker_info"
|
||||||
found_process=true
|
found_process=true
|
||||||
fi
|
fi
|
||||||
@@ -295,7 +300,7 @@ inuse() {
|
|||||||
done <<< "$ss_result"
|
done <<< "$ss_result"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Method 3: Try fuser as last resort
|
# Method 3: Try fuser as last resort
|
||||||
if [ "$found_process" = false ] && command -v fuser >/dev/null 2>&1; then
|
if [ "$found_process" = false ] && command -v fuser >/dev/null 2>&1; then
|
||||||
local fuser_pids=$(fuser "$port/tcp" 2>/dev/null)
|
local fuser_pids=$(fuser "$port/tcp" 2>/dev/null)
|
||||||
@@ -312,7 +317,7 @@ inuse() {
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Method 4: Check for Docker containers more accurately
|
# Method 4: Check for Docker containers more accurately
|
||||||
if [ "$found_process" = false ] && command -v docker >/dev/null 2>&1; then
|
if [ "$found_process" = false ] && command -v docker >/dev/null 2>&1; then
|
||||||
# First, try to find containers with published ports matching our port
|
# First, try to find containers with published ports matching our port
|
||||||
@@ -343,13 +348,13 @@ inuse() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If we still haven't found the process, show a generic message
|
# If we still haven't found the process, show a generic message
|
||||||
if [ "$found_process" = false ]; then
|
if [ "$found_process" = false ]; then
|
||||||
echo -e "${YELLOW}⚠ Port $port is in use but unable to identify the process${NC}"
|
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}"
|
echo -e "${CYAN} This might be due to insufficient permissions or the process being in a different namespace${NC}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
Host *
|
Host *
|
||||||
IdentityAgent ~/.1password/agent.sock
|
|
||||||
AddKeysToAgent yes
|
AddKeysToAgent yes
|
||||||
ForwardAgent yes
|
ForwardAgent yes
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user