Created
March 7, 2025 18:47
-
-
Save ConnerWill/52a9344db90396ae6734e1b8d6ae70d0 to your computer and use it in GitHub Desktop.
Script to update a file on a remote server with the external IP address of your local computer. This is handy for semi-automated DDNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
### VARIABLES | |
VERBOSE=1 | |
DEBUG=0 | |
REMOTE_USER="your_remote_user" # Set your remote server username | |
REMOTE_HOST="your.remote.server.com" # Set your remote server's IP or domain | |
REMOTE_FILE_PATH="/home/${REMOTE_USER}/home-external-ip" # Set the path to the file on the remote server | |
SSH_KEY_PATH="${HOME}/.ssh/id_rsa" # Path to your SSH private key | |
EXTERNAL_IP_SERVICE="https://ifconfig.me" # External IP address service | |
### SETUP | |
set -e | |
[[ ${DEBUG} == 1 ]] && set -x | |
### FUNCTIONS | |
function write_error(){ | |
local input error_msg | |
input="${1}" | |
error_msg="[ERROR]" | |
printf "\x1B[0;48;5;196;38;5;255m%s\x1b[0m: \x1B[0;38;5;196m%s\x1B[0m\n" "${error_msg}" "${input}" >&2 | |
return 1 | |
} | |
function write_verbose(){ | |
local input verbose_msg | |
input="${1}" | |
verbose_msg="[VERBOSE]" | |
if [[ "${VERBOSE}" == 1 ]]; then | |
printf "\x1B[0;48;5;4;38;5;226m%s\x1b[0m: \x1B[0;38;5;226m%s\x1B[0m\n" "${verbose_msg}" "${input}" | |
fi | |
} | |
function is_installed(){ | |
local input | |
input="${1}" | |
if command -v "${input}" >/dev/null 2>&1; then | |
write_verbose "Package: '${input}' is installed :)" | |
else | |
write_error "Package: '${input}' is NOT installed :(" | |
exit 1 | |
fi | |
} | |
function file_exists(){ | |
local input | |
input="${1}" | |
if [[ -e "${input}" ]]; then | |
write_verbose "File: '${input}' exists :)" | |
else | |
write_error "File: '${input}' does NOT exist :(" | |
exit 1 | |
fi | |
} | |
function get_external_ip(){ | |
write_verbose "Fetching external IP address..." | |
IP_ADDRESS=$(curl -s "${EXTERNAL_IP_SERVICE}") | |
if [[ -z "${IP_ADDRESS}" ]]; then | |
write_error "Failed to fetch external IP address." | |
exit 1 | |
fi | |
write_verbose "External IP Address: ${IP_ADDRESS}" | |
} | |
function update_remote_file(){ | |
write_verbose "Updating remote file with external IP: ${IP_ADDRESS} ..." | |
if ssh -i "${SSH_KEY_PATH}" "${REMOTE_USER}@${REMOTE_HOST}" "echo '${IP_ADDRESS}' >! ${REMOTE_FILE_PATH}"; then # NOTE: Replace '>!' with just '>' if you do not have prevent overwrite files enabled in your shell (e.g. zsh) | |
write_verbose "Remote file updated successfully." | |
else | |
write_error "Failed to update remote file on ${REMOTE_HOST}:${REMOTE_FILE_PATH}" | |
exit 1 | |
fi | |
} | |
### MAIN | |
is_installed "curl" | |
is_installed "ssh" | |
file_exists "${SSH_KEY_PATH}" | |
get_external_ip | |
update_remote_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment