Skip to content

Instantly share code, notes, and snippets.

@link89
Created December 8, 2025 02:58
Show Gist options
  • Select an option

  • Save link89/e69199ef9bc5327ebf9ae8828fcc4d28 to your computer and use it in GitHub Desktop.

Select an option

Save link89/e69199ef9bc5327ebf9ae8828fcc4d28 to your computer and use it in GitHub Desktop.
A crontab script to test and keep tcp tunnel alive.
#!/bin/bash
TARGET_LIST=(
"127.0.0.1 22001"
"127.0.0.1 22002"
"127.0.0.1 22003"
)
LOG_FILE=~/log/tcp_keep_alive.log
TIMEOUT_BIN=/usr/bin/timeout
NC_BIN=/usr/bin/nc
TIMEOUT_NC="1"
TIMEOUT_CMD="5"
mkdir -p ~/log
for TARGET_ENTRY in "${TARGET_LIST[@]}"; do
if ! $TIMEOUT_BIN $TIMEOUT_CMD $NC_BIN -z -w $TIMEOUT_NC $TARGET_ENTRY 2>&1 > /dev/null; then
echo "$(date "+%Y-%m-%d %H:%M:%S") [Error]:fail to connect to $TARGET_ENTRY" >> $LOG_FILE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment