Skip to content

Instantly share code, notes, and snippets.

@ceelian
Last active January 23, 2024 21:15
Show Gist options
  • Save ceelian/0c293cf0e10c924a2124e2c9fa3805a9 to your computer and use it in GitHub Desktop.
Save ceelian/0c293cf0e10c924a2124e2c9fa3805a9 to your computer and use it in GitHub Desktop.
Adds a hostname to a given IP address in /etc/hosts.
#!/usr/bin/env bash
# The MIT License (MIT)
#
# Copyright (c) 2023 Christian Haintz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###############################################################
# Adds a hostname to a given IP address. #
# Doesn't add a new line if IP is already present #
# #
# Usage: add_hostname_to_hosts.sh <ip_address> <hostname> #
###############################################################
# Check if the script is run with root privileges
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root or with sudo."
exit 1
fi
# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <IP_ADDRESS> <HOSTNAME>"
exit 1
fi
IP_ADDRESS="$1"
HOSTNAME="$2"
HOSTS_FILE="/etc/hosts"
# Check if the IP address is already present in the hosts file
IP_EXISTS=$(grep -w "$IP_ADDRESS" "$HOSTS_FILE")
if [ -z "$IP_EXISTS" ]; then
# Add the IP address and hostname to the hosts file
echo "$IP_ADDRESS $HOSTNAME" >> "$HOSTS_FILE"
echo "Added $IP_ADDRESS $HOSTNAME to $HOSTS_FILE."
else
# Check if the hostname is already present for the IP address
HOSTNAME_EXISTS=$(echo "$IP_EXISTS" | grep -w "$HOSTNAME")
if [ -z "$HOSTNAME_EXISTS" ]; then
# Add the hostname to the existing IP address entry
TEMP_FILE=$(mktemp)
sed "/$IP_ADDRESS/s/$/ $HOSTNAME/" "$HOSTS_FILE" > "$TEMP_FILE"
cat "$TEMP_FILE" > "$HOSTS_FILE"
rm "$TEMP_FILE"
echo "Added $HOSTNAME to the existing IP address entry in $HOSTS_FILE."
else
echo "The hostname $HOSTNAME is already present for the IP address $IP_ADDRESS in $HOSTS_FILE."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment