Set New Host in etc/hosts by Jamf Policy

a jamf pro bash script that utilizes script options and subsequent parameter labels to call out the ip and tab to hostname for adding custom entries to the macos client machines’ /etc/hosts file



# Script Parameters:
# $4: IP Address
# $5: Hostname

# Check if both IP address and hostname are provided
if [ -z "$4" ] || [ -z "$5" ]; then
    echo "Error: IP address or hostname not provided."
    exit 1

# Validate IP address format
if ! echo "$4" | grep -Eqo '^([0-9]{1,3}\.){3}[0-9]{1,3}$'; then
    echo "Error: Invalid IP address format."
    exit 1

# Add entry to /etc/hosts
echo "Adding entry to /etc/hosts..."
echo -e "$4\t$5" >> /etc/hosts

# Check if the entry was added
if grep -Eq "^$4\\s+$5$" /etc/hosts; then
    echo "Entry added successfully: $4 $5"
    echo "Error: Failed to add entry to /etc/hosts."
    exit 1


No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *