aceberg / WatchYourPorts

Open ports inventory for local servers. Exports data to InfluxDB2/Grafana
https://hub.docker.com/r/aceberg/watchyourports
MIT License
90 stars 3 forks source link

docker-export.sh produces a file with only container names #1

Open JSouthGB opened 1 month ago

JSouthGB commented 1 month ago

I appreciate this program, very handy! I'm also using WatchYourLan. These have both made a couple of my markdown files irrelevant, and that's a good thing!

I just want to share an issue I had with the docker-export.sh file. As it currently is, the only thing it produced for me is a file, /tmp/wyp-docker.txt, containing a list of all my docker container names. The information echoed to the command line was correct, but it didn't get saved to the file. I copied the list of container names to the hosts.yaml file, but it didn't seem to do anything for me.

It's also very possible I've misunderstood or done something wrong with the provided docker-export.sh file.

I'm sharing my modified version of the script that got me what I believe is the intended file.

This modified version outputs to a yaml file and cleans up the txt file. It has a done message, but doesn't output anything while executing. Even for my ~50 containers, run time is maybe a second or two, so it didn't seem necessary. My docker containers are on a Debian 12 host in case that's important. After I ran my modified version and did a scan, there were only two docker container ports it didn't catch.

Apologies if I messed up, misunderstood, or misread, something.

#!/usr/bin/env bash

# This script generates a WatchYourPorts config from Docker containers.

# HOW TO USE
#    1. Run this script on a server, where Docker is installed:
#   ./docker-export.sh $ADDR
#    $ADDR is IP or domain name of the server, without http(s):// prefix
#    It will be used to ping ports
#    2. Paste the output to hosts.yaml file in WYP config dir
#    3. You can add as many servers to hosts.yaml, as you want

docker ps -a --format "{{.Names}}">/tmp/wyp-docker.txt

{
  echo $1':'
  echo '    name:'
  echo '    addr: '$1
  echo '    portmap:'

  while read NAME; do
    PORT=$(docker inspect $NAME | grep HostPort | sed '1!d;s/"HostPort": //;s/,//;s/"//g')

    if [ ${#PORT} -ge 1 ]; then
      echo '       '$PORT':'
      echo '            name: '$NAME
      echo '            port: '$PORT
      echo '            state: false'
      echo '            watch: true'
    fi
  done </tmp/wyp-docker.txt
} > /tmp/wyp-docker-output.yaml

echo "Output has been saved to /tmp/wyp-docker-output.yaml"
rm /tmp/wyp-docker.txt

And thank you again for sharing this program!

Aetherinox commented 3 weeks ago

I decided to go another route, because I like to control prints, and I don't want to mess with multiple files.

#!/bin/bash

# #
#   This script generates a WatchYourPorts config from Docker containers.
#
#   HOW TO USE
#   1. Run this script on a server, where Docker is installed:
#           ./docker-export.sh $ADDR
#       $ADDR is IP or domain name of the server, without http(s):// prefix
#       It will be used to ping ports
#   2. Paste the output to hosts.yaml file in WYP config dir
#   3. You can add as many servers to hosts.yaml, as you want
#
#   If you get the error:
#       bash: ./ports_docker_import.sh: /bin/bash^M: bad interpreter: No such file or directory
#   Run the command:
#       sudo sed -i -e 's/\r$//' ports_docker_import.sh
# #

# #
#   Output File
# #

file_output="$(pwd)/data/wyp/hosts.yaml"

# #
#   Permissions
# #

sudo touch $file_output
sudo chmod 0644 $file_output

# #
#   Docker Container List
# #

CONTAINERS=$(docker ps -a --format "{{.Names}}")

# #
#   Build Host Config
# #

echo "$1:" | tee $file_output
echo "    name: $2" | tee -a $file_output
echo "    addr: $1" | tee -a $file_output
echo "    portmap:" | tee -a $file_output

# #
#   Iterate docker container names
# #

while IFS= read -r id; do
    PORT=`docker inspect $id | grep HostPort | sed '1!d;s/"HostPort": //;s/,//;s/"//g' | tr -d '[:space:]'`

    if [ ${#PORT} -ge 1 ]; then
        echo "        $PORT:" | tee -a $file_output
        echo "            name: $id" | tee -a $file_output
        echo "            port: $PORT" | tee -a $file_output
        echo "            state: false" | tee -a $file_output
        echo "            watch: true" | tee -a $file_output
    fi
done <<< "$CONTAINERS"

Also supports the name of the host

./ports_docker_import.sh xx.xx.xx.xx domain.com


But yeah, it wasn't just you @JSouthGB -- I was also just getting a list of container names. Nothing else.