DHCP fixedhosts to /etc/hosts file


This was a completely random (and not that useful) thought!

I have memorized which IPs are assigned to which machines in our home LAN as simply pinging a hostname doesnt really work in linux (by default) as it did with windows (unless I setup WINS… I think… correct me if I’m wrong).
So short of setting up an in-house DNS server, hosts files come in handy for stuff like this.
(I really didn’t need to setup a hosts file, but assuming I did want the convenience)

I thought I’ve already defined which hosts get which IPs in the fixed hosts table of the DHCP server in my router, surely I could just use that!
I remembered the router has telnet open internally, so I wrote a one-liner to do it:

echo “admin^M$PASSWORD^Mdhcpserver list fixedhosts^M^\]^D” | nc 192.168.1.1 23 | \\grep “\[ \]\*\[0-9\]” | sed “s/\[ 0-9\]\*| \\(\[^ \]\*\\)\[ \]\*| \\(\[^ \]\*\\).\*/\\2\\t\\1/g”

I echo the username, password, the command to give me the table and logout then grep for the (numbered) entries of the printed table, which gives me entries that look like this:


   4 | stevepc       | 192.168.1.3      |  00:11:22:33:44:55  | 0         |
   5 | NAS           | 192.168.1.10     |  00:22:33:44:55:66  | 0         |
   9 | Minecraft_SVR | 192.168.1.62     |  00:33:44:55:66:77  | 0         |

Then I use sed to put it into the same format as /etc/hosts:


192.168.1.3     stevepc
192.168.1.10    NAS
192.168.1.62    Minecraft_SVR

This isn’t really that useful, but it took a couple of minutes, just to “see if I could do it”… and it worked :)

… I don’t think I’ll actually use it, lol, but it was fun anyway :P