Skip to content

Instantly share code, notes, and snippets.

@wiedehopf
Created April 8, 2026 04:12
Show Gist options
  • Select an option

  • Save wiedehopf/16da39cf84438aa820c8cb490e1fdbb4 to your computer and use it in GitHub Desktop.

Select an option

Save wiedehopf/16da39cf84438aa820c8cb490e1fdbb4 to your computer and use it in GitHub Desktop.
haproxy scripts
#!/bin/bash
if [[ -z "$1" ]]; then
echo no arg
exit 1
fi
IDS=$(echo show sess | socat stdio UNIX-CLIENT:/run/haproxy-admin.sock | grep -F "$1")
echo dropping the following sessions:
echo "$IDS"
IDS=$(echo "$IDS" | cut -f1 -d:)
CMD=""
for ID in $IDS
do
CMD+="shutdown session $ID;"
done
CMD=${CMD::-1}
echo
echo sending command to haproxy: "$CMD"
echo "$CMD" | socat stdio UNIX-CLIENT:/run/haproxy-admin.sock
#!/bin/bash
hacommand() { echo "$@" | socat stdio UNIX-CLIENT:/run/haproxy-admin.sock; }
acl=/etc/haproxy/$1
if [[ -n "$2" ]]; then
if grep -qsF -e "$2" "$acl" &>/dev/null; then
echo "already in list: $2"
else
echo "$2" >> "$acl"
fi
fi
WD=/run/updatetable
mkdir -p "$WD"
live="$WD/live"
disk="$WD/disk"
add="$WD/add"
del="$WD/del"
# remove key from live acl, sort, remove duplicates, remove empty lines
hacommand show acl "$acl" | cut -d ' ' -f 2 | sort | uniq | awk 'NF' > "$live"
# sort file on disk, remove comments and empty lines, remove duplicate lines, remove leading spaces
sort < "$acl" | awk '/^[^#]/' | awk 'NF' | sed -e 's/^ *//' | uniq > "$disk"
diff "$live" "$disk" | grep -E '^> ' | sed -e 's/^> *//' > "$add"
diff "$disk" "$live" | grep -E '^> ' | sed -e 's/^> *//' > "$del"
# add entries missing in live
echo Adding ............
while IFS='' read -r line; do
echo -n "$line"
hacommand add acl "$acl" "$line"
done < "$add"
echo .............. done.
# delete entries missing on disk
echo Deleting ..........
while IFS='' read -r line; do
echo -n "$line"
hacommand del acl "$acl" "$line"
done < "$del"
echo .............. done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment