Assume that you are in a big network where the ip addresses are given via dhcp.
Assume also that you want to be able to communicate with the same pc which can change the ip address every time it reboots.
One easy and “legal” way to always find is using netbios, a protocol that makes a computer shout over the network it’s pairing Name -> IP address.
For windows and macosx this works out of the box, for linux/bsd machines you can just set a minimal smb.conf like this.
[global] workgroup = WORKGROUP server string = My Samba Server netbios name = koala
It’s useful to have the correct pairing right in /etc/hosts for every kind of command line program that doesn’t speak netbios.
So we’ll use nmblookup and sed for the task.
Just substitute the netbios name that you need here:
export koala=$(nmblookup koala | sed -n 2p | cut -d ' ' -f 1) # Now I can substitute the right ip in /etc/hosts sed "/koala/ s/.*/$koala\tkoala" < /etc/hosts > /tmp/hosts sudo mv /etc/hosts /etc/hosts.bak sudo mv /tmp/hosts /etc/hosts
Maybe you also want to modify sudoers to use it without inserting a password each time.