Skip to content

Instantly share code, notes, and snippets.

@Sharpie
Last active February 26, 2019 20:05
Show Gist options
  • Save Sharpie/6d66a0f499459055d075a05c01a6ec35 to your computer and use it in GitHub Desktop.
Save Sharpie/6d66a0f499459055d075a05c01a6ec35 to your computer and use it in GitHub Desktop.
Simple PSQL connection tester in bash
#!/bin/bash
# ARGV[1]: hostname or IP address to test for postgresql connectivity.
HOST="${1:?A hostname must be passed to this script}"
cleanup() {
printf '%s\n' "Closing TCP connection to ${HOST}."
exec 3<&-
exit
}
trap cleanup EXIT INT
printf '%s\n' "Opening TCP connection to ${HOST}:5432"
exec 3<>/dev/tcp/$HOST/5432
printf '%s\n' 'Sending Postgres client hello message'
printf '\0\0\0\x2f\0\x03\0\0user\0pe-puppetdb\0database\0pe-puppetdb\0\0' >&3
printf '%s\n' 'Postgres response:'
cat <&3
printf '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment