Offene Docker-Ports
· 1.8 KiB · Text
原始文件
#!/bin/bash
# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Initialize output variables and max length
exposed_output=""
not_exposed_output=""
max_length=0
color_blind_mode=0
# Check for color-blind mode flag
if [[ "$1" == "--i-am-colorblind" ]]; then
color_blind_mode=1
fi
# Get Docker container info
docker_output=$(docker ps --format '{{.Names}}\t{{.Ports}}')
# First pass to find the longest container name
while read -r line; do
container=$(echo -e "$line" | awk '{print $1}')
length=${#container}
if (( length > max_length )); then
max_length=$length
fi
done <<< "$docker_output"
# Second pass to generate the output
while read -r line; do
container=$(echo -e "$line" | awk '{print $1}')
ports=$(echo -e "$line" | cut -f2-)
# Skip containers with no ports or no mappings
if [ -z "$ports" ] || [[ "$ports" == *"<->"* ]] || [[ ! "$ports" == *'->'* ]]; then
continue
fi
# Remove IP prefixes and trailing comma
ports=$(echo "$ports" | sed 's/0.0.0.0://g' | sed 's/127.0.0.1://g' | sed 's/,$//')
# Align output
padding=$(printf "%*s" $((max_length - ${#container})) "")
# Check if ports are exposed
if [[ "$line" == *"0.0.0.0:"* ]] || [[ "$line" == *":::"* ]]; then
if (( color_blind_mode )); then
exposed_output+="Exposed: ${container}${padding} : ${ports}\n"
else
exposed_output+="${RED}${container}${padding} : ${ports}${NC}\n"
fi
else
if (( color_blind_mode )); then
not_exposed_output+="Not Exposed: ${container}${padding} : ${ports}\n"
else
not_exposed_output+="${GREEN}${container}${padding} : ${ports}${NC}\n"
fi
fi
done <<< "$docker_output"
# Sort and print output
echo -e "All green ports are locally open \n and all red Ports a available from the bad bad internet \n"
echo -e "$exposed_output"
echo -e "$not_exposed_output"
| 1 | #!/bin/bash |
| 2 | |
| 3 | # ANSI color codes |
| 4 | RED='\033[0;31m' |
| 5 | GREEN='\033[0;32m' |
| 6 | NC='\033[0m' # No Color |
| 7 | |
| 8 | # Initialize output variables and max length |
| 9 | exposed_output="" |
| 10 | not_exposed_output="" |
| 11 | max_length=0 |
| 12 | color_blind_mode=0 |
| 13 | |
| 14 | # Check for color-blind mode flag |
| 15 | if [[ "$1" == "--i-am-colorblind" ]]; then |
| 16 | color_blind_mode=1 |
| 17 | fi |
| 18 | |
| 19 | # Get Docker container info |
| 20 | docker_output=$(docker ps --format '{{.Names}}\t{{.Ports}}') |
| 21 | |
| 22 | # First pass to find the longest container name |
| 23 | while read -r line; do |
| 24 | container=$(echo -e "$line" | awk '{print $1}') |
| 25 | length=${#container} |
| 26 | if (( length > max_length )); then |
| 27 | max_length=$length |
| 28 | fi |
| 29 | done <<< "$docker_output" |
| 30 | |
| 31 | # Second pass to generate the output |
| 32 | while read -r line; do |
| 33 | container=$(echo -e "$line" | awk '{print $1}') |
| 34 | ports=$(echo -e "$line" | cut -f2-) |
| 35 | |
| 36 | # Skip containers with no ports or no mappings |
| 37 | if [ -z "$ports" ] || [[ "$ports" == *"<->"* ]] || [[ ! "$ports" == *'->'* ]]; then |
| 38 | continue |
| 39 | fi |
| 40 | |
| 41 | # Remove IP prefixes and trailing comma |
| 42 | ports=$(echo "$ports" | sed 's/0.0.0.0://g' | sed 's/127.0.0.1://g' | sed 's/,$//') |
| 43 | |
| 44 | # Align output |
| 45 | padding=$(printf "%*s" $((max_length - ${#container})) "") |
| 46 | |
| 47 | # Check if ports are exposed |
| 48 | if [[ "$line" == *"0.0.0.0:"* ]] || [[ "$line" == *":::"* ]]; then |
| 49 | if (( color_blind_mode )); then |
| 50 | exposed_output+="Exposed: ${container}${padding} : ${ports}\n" |
| 51 | else |
| 52 | exposed_output+="${RED}${container}${padding} : ${ports}${NC}\n" |
| 53 | fi |
| 54 | else |
| 55 | if (( color_blind_mode )); then |
| 56 | not_exposed_output+="Not Exposed: ${container}${padding} : ${ports}\n" |
| 57 | else |
| 58 | not_exposed_output+="${GREEN}${container}${padding} : ${ports}${NC}\n" |
| 59 | fi |
| 60 | fi |
| 61 | done <<< "$docker_output" |
| 62 | |
| 63 | # Sort and print output |
| 64 | echo -e "All green ports are locally open \n and all red Ports a available from the bad bad internet \n" |
| 65 | echo -e "$exposed_output" |
| 66 | echo -e "$not_exposed_output" |