-
-
Notifications
You must be signed in to change notification settings - Fork 8
Description
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!