DEV Community

Matias
Matias

Posted on

Today I wrote a handy little snippet to easily access Ubuntu from Windows (in WSL2)

In case you haven't been following Windows news lately, you can now run Linux in Windows!

The latest update to this makes it very usable, as Docker and other tools are now supported.

There have been several different solutions posted for automating access to an exposed program - say, your Node server running in Ubuntu. None of these worked for me, so I resorted to some good old fashioned awk.

I took the regular command to get the IP: ip addr, which gives you something like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether b6:5d:52:7b:32:42 brd ff:ff:ff:ff:ff:ff
3: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:16:5a:42:cb:c2 brd ff:ff:ff:ff:ff:ff
    inet 172.17.177.74/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe71:cac1/64 scope link
       valid_lft forever preferred_lft forever
Enter fullscreen mode Exit fullscreen mode

Then I grabbed the most important part for us; eth0 with grep eth0:

4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:16:5a:42:cb:c2 brd ff:ff:ff:ff:ff:ff
    inet 172.17.177.74/16 brd 172.17.255.255 scope global eth0
Enter fullscreen mode Exit fullscreen mode

Then the inet part specifically with grep inet0:

inet 172.17.177.74/16 brd 172.17.255.255 scope global eth0
Enter fullscreen mode Exit fullscreen mode

From here on out, I wanted to only work with the IP address, and awk is really good at this kind of thing. I cut the second word from the line with awk {'print $2'}:

172.17.177.74/16
Enter fullscreen mode Exit fullscreen mode

I always forget how to split a string with awk, but after some searching, not googling (pro tip: use https://ecosia.org as your search engine! Literally plant trees with your searches instead of feeding Google ;) ) I found that it can be done with awk -F. So, to split on the character / we can do awk -F '/' '{print $1}' to grab the first part of the split string:

172.17.177.74
Enter fullscreen mode Exit fullscreen mode

Because I tend to work with Node, I generally always want to hit port 3000 by default. After some fiddling, I did this with awk '{printf, with the IP address as the argument, like so: awk '{printf "%s:3000", $0}':

172.17.177.74:3000
Enter fullscreen mode Exit fullscreen mode

And finally, the cherry on top: sending the result to the Windows clipboard so I can just paste it into my browser (pro tip: Support web diversity! I use Firefox and it's awesome). The Microsoft team has really done a great job with this through exposing "exe" files as pipeable tools in Ubuntu. For example, you can open the Windows Explorer with explorer.exe, and pipe to the clipboard with echo "hello from Ubuntu!" | clip.exe.

All put together, the entire snippet looks like this:

ip addr | grep eth0 | grep inet | awk '{print $2}' | awk -F '/' '{print $1}' | awk '{printf "%s:3000", $0}' | clip.exe
Enter fullscreen mode Exit fullscreen mode

I put it in a file I called copy_ip.sh, made it executable with chmod +x copy_ip.sh, and now whenever I want to access my server from Windows I can just ./copy_ip.sh and paste it into my browser <3

Latest comments (3)

Collapse
 
thiscodeworks profile image
Mishka

Found your snippet useful! Hope you don't mind, shared your snippet on thiscodeworks:

thiscodeworks.com/useful-snippet-t...

Collapse
 
codeluggage profile image
Matias

Neat! Thanks for sharing and for letting me know!

Collapse
 
codeluggage profile image
Matias

Added this as a PR to the great hacks at github.com/shayne/wsl2-hacks/pull/1 - definitely worth checking out!