• Home
  • Free Stuff
  • About
  • Contact

Script to Add Dynamic IP to /etc/hosts

October 28, 2018

I have a dynamic IP from my internet service provider and sometimes it takes longer to SSH to my server as it needs to resolve the address. On static IP, I can put the address into public DNS server to a quick resolving process,  but as it uses dynamic IP, I would need to use a different approach.

As I have dynamic DNS from Synology (can also work with other DDNS provider), a little bash script below can help me to automatically add IP on an interval basis, replace the selected predefined row on /etc/hosts.

Script : add-host-markasdj.sh

[code lang=”bash”]
FILE=”/etc/hosts”
ip=`nslookup myhomesweethome.myds.me | grep -i addr | grep -v “#” | cut -d “:” -f2 | sed -e ‘s/^[[:space:]]*//’`
if grep -q $ip $FILE;
then
echo “Ignore, IP has been previously added”
else
echo “$ip myhomesweethome.myds.me” >> /etc/hosts
echo “$ip has been added to /etc/hosts”
fi
#echo “IP:$ip”
[/code]

LinuxScript
Share

Linux

Masim "Vavai" Sugianto
Traveller, Open Source Enthusiast & Book Lover. Works as Independent Worker & Self-Employer.

Comment


Mike F
November 17, 2020 at 15:13
Reply

Great script. Thanks for sharing.



Leave a Reply Cancel reply

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

  • Recent Posts

    • Januari 2021 Portfolio Update
    • December 2020 Portfolio Update
    • After a While
    • Life goes On : Farming
    • Pursuing FIRE in Indonesia : Are You Prepared For A Recession?
    • VirtualBox Error Kernel Driver not Installed (rc=-1908) on Zorin OS 15.2
    • ZorinOS on Intel NUC Hades Canyon Series NUC817HVK
    • Banana Farm
    • Zorin OS 15.2 Ultimate
    • Haraka and LDAP Authentication with Zimbra



© Copyright LetsBlog Theme Demo - Theme by ThemeGoods