shayne / go-wsl2-host

Automatically update your Windows hosts file with the WSL2 VM IP address
1.65k stars 142 forks source link

Modify the method of getting ip and host alias #16

Closed GoodbyeNJN closed 4 years ago

GoodbyeNJN commented 4 years ago

Command hostname -I is not available to get VM's IP address in some cases, e.g in Arch Linux, so I did something to let user set custom command. For the reason that ~/.wsl2hosts was already used to set custom host alias, I combined these. Now we can set custom command and custom host alias in one file.

GoodbyeNJN commented 4 years ago

15

shayne commented 4 years ago

Thanks for the PR.

Curious if we can come up with a solution that works on any distro. If we only rely on the kernel we should be able to find a solution.

Mind running this in Arch or any other distro to see if it works?

awk '/32 host LOCAL/ { if (f != "127.0.0.1") { print f; exit } } {f=$2}' /proc/net/fib_trie

We wouldn't rely on awk but for the sake of checking, the above command should print out the WSL VM IP.

GoodbyeNJN commented 4 years ago

Thanks for your reply. This command works fine in Arch. Need more test in any other distro.

shayne commented 4 years ago

Pushed release v0.3.3 which uses the fib_trie approach to get IP address in a distro agnostic way. https://github.com/shayne/go-wsl2-host/releases/tag/v0.3.3