Is there a way to echo only the server ip adress from Bash? I am on a Linux/Debian6.
My intent is to write to “/etc/hosts” the pair domain_name IP for each new domain I register in the server.
As this bash script will be opened for public use, I am trying to find a way of getting this information via cli.
CONCLUSION:
This is the final code, based on the clues my friends helped me with:
newhost() {
DMN=$1
X=`ifconfig | grep Bcast`
Y=`echo "${X#*:}"`
DNS=`echo "${Y%\ B*}"` # server DNS(207.112.37.222)
H='/etc/hosts' #hostfile
PAIR="$DNS\t$DMN"
if grep -i --silent "$DMN" "$H"; then
echo -e "$DMN already exists in $H"
else
bash <<EOF
echo -e "$PAIR" >> "$H"
EOF
echo -e "$PAIR added to $H"
fi
}
By the way, Isn’t it funny that some born-already-smart dudes voted to close the question?
Thanks.
use:
ifconfig– your ip will be after theinet addr:in this case1.1.1.1sample output:
http://www.debianadmin.com/network-interface-configuration-using-ifconfig.html