Created
July 11, 2023 08:48
-
-
Save WingTillDie/5e23528f528effe53cb93d323590f6a6 to your computer and use it in GitHub Desktop.
Get WSL IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Modified from https://superuser.com/questions/1586386/how-to-find-wsl2-machines-ip-address-from-windows#1749524 | |
import re | |
import subprocess | |
from itertools import repeat | |
class Regex_ip: | |
def regex_ip_with_assert(self): | |
return f'{self.regex_ip_assert()}{self.regex_ip()}' | |
def regex_ip_assert(self): | |
return '(?<=inet\s)' | |
def regex_ip(self) -> str: | |
field = self.regex_field() | |
return '.'.join(repeat(field, 4)) | |
#return f'{field}.{field}.{field}.{field}' | |
def regex_field(self) -> str: | |
# BETTER from 0 to 255 | |
# BETTER with assertion ? | |
return '\d{1,3}' | |
output = subprocess.check_output('ip addr show eth0', shell=True) | |
print(re.search(Regex_ip().regex_ip_with_assert(), output.decode()).group()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment