<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matias</title>
    <description>The latest articles on DEV Community by Matias (@codeluggage).</description>
    <link>https://dev.to/codeluggage</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F168720%2F67399234-5d8f-428f-bd4f-efc5b2cfe58f.jpeg</url>
      <title>DEV Community: Matias</title>
      <link>https://dev.to/codeluggage</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeluggage"/>
    <language>en</language>
    <item>
      <title>Today I wrote a handy little snippet to easily access Ubuntu from Windows (in WSL2)</title>
      <dc:creator>Matias</dc:creator>
      <pubDate>Sat, 06 Jul 2019 03:07:27 +0000</pubDate>
      <link>https://dev.to/codeluggage/today-i-wrote-a-handy-little-snippet-to-easily-access-ubuntu-from-windows-in-wsl2-19l</link>
      <guid>https://dev.to/codeluggage/today-i-wrote-a-handy-little-snippet-to-easily-access-ubuntu-from-windows-in-wsl2-19l</guid>
      <description>&lt;p&gt;In case you haven't been following Windows news lately, you can now run &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/wsl2-about" rel="noopener noreferrer"&gt;Linux in Windows&lt;/a&gt;! &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes" rel="noopener noreferrer"&gt;latest update&lt;/a&gt; to this makes it very usable, as Docker and other tools are now supported. &lt;/p&gt;

&lt;p&gt;There have been &lt;a href="https://github.com/shayne/go-wsl2-host" rel="noopener noreferrer"&gt;several&lt;/a&gt; different &lt;a href="https://github.com/microsoft/WSL/issues/4150" rel="noopener noreferrer"&gt;solutions&lt;/a&gt; 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 &lt;code&gt;awk&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I took the regular command to get the IP: &lt;code&gt;ip addr&lt;/code&gt;, which gives you something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1: lo: &amp;lt;LOOPBACK,UP,LOWER_UP&amp;gt; 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: &amp;lt;BROADCAST,MULTICAST,MASTER&amp;gt; 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: &amp;lt;NOARP&amp;gt; mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
4: eth0: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I grabbed the most important part for us; &lt;code&gt;eth0&lt;/code&gt; with &lt;code&gt;grep eth0&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4: eth0: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the &lt;code&gt;inet&lt;/code&gt; part specifically with &lt;code&gt;grep inet0&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inet 172.17.177.74/16 brd 172.17.255.255 scope global eth0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;172.17.177.74/16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;172.17.177.74
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;172.17.177.74:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;explorer.exe&lt;/code&gt;, and pipe to the clipboard with &lt;code&gt;echo "hello from Ubuntu!" | clip.exe&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;All put together, the entire snippet looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ip addr | grep eth0 | grep inet | awk '{print $2}' | awk -F '/' '{print $1}' | awk '{printf "%s:3000", $0}' | clip.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
      <category>wsl</category>
      <category>windows</category>
      <category>ubuntu</category>
      <category>awk</category>
    </item>
  </channel>
</rss>
