Agregar host al archivo /etc/hosts

¿En qué podemos ayudarte?
< Regresar
Estás aquí:
Imprimir

Script que agrega una entrada al /etc/hosts

#!/usr/bin/env bash

# Path to your hosts file
hostsFile=”/etc/hosts”

# Hostname to add/remove.
hostname=”mxserver3.kpt.com.mx”

#Alias for host name.
alias=”mxserver3″

# Resolution IP address for host
dnstoip=”dig +short $hostname”
newIP=$(eval $dnstoip)

yell() { echo “$0: $*” >&2; }
die() { yell “$*”; exit 111; }
try() { “$@” || die “cannot $*”; }
if [ -z $newIP ]; then

echo “El host $hostname no pudo ser resuelto validar nombre e intentar de nuevo.”;
else

if [ -n “$(grep -P “[[:space:]]$hostname” /etc/hosts)” ]; then

echo “El host $hostname fue encontrado en $hostsFile. Removiendo ahora…”;
try sudo sed -ie “/[[:space:]]$hostname/d” “$hostsFile”;

else

yell “El host $hostname no se encontro en $hostsFile, se creara la entrada…”;
fi

if [ -n “$(grep -P “[[:space:]]$hostname” /etc/hosts)” ]; then
yell “El host $hostname, ya existe: $(grep $hostname $hostsFile), checar si se desea cambiar”;
else
echo “Agregando el host $hostname a $hostsFile…”;
try printf “%s\t%s\t%s\n” “$newIP” “$hostname” “$alias” | sudo tee -a “$hostsFile” > /dev/null;

if [ -n “$(grep $hostname /etc/hosts)” ]; then
echo “El host $hostname fue agregado exitosamente:”;
echo “$(grep $hostname /etc/hosts)”;
else

die “Falló el intento de agregar el host $hostname”;
fi
fi

fi

$@

Table of Contents

Deja un comentario