regex help to get the local ip address - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: regex help to get the local ip address (/thread-13062.html) |
regex help to get the local ip address - evilcode1 - Sep-26-2018 hey all... need a help in regex .. im trying to get the local ip address form a windows system by running this code : ( i dont want to use python socket ) import os import re q = os.popen("""ipconfig | findstr /R /C:"IPv4 Address""").read() print q dev = re.findall('[0-9]+' , q) print devthe result is : ['4', '192', '168', '74', '163'] the cmd result is : IPv4 Address. . . . . . . . . . . : 192.168.74.163 i need to get just this result : 192.168.74.163 how i can do it by regex ?? thanks in advance RE: regex help to get the local ip address - evilcode1 - Sep-26-2018 solved : from os import popen from re import findall q = popen("""ipconfig | findstr /R /C:"IPv4 Address""").read() print q dev = findall( r'[0-9]+(?:\.[0-9]+){3}' , q) print dev[0] RE: regex help to get the local ip address - Axel_Erfurt - Sep-26-2018 import socket print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]) RE: regex help to get the local ip address - evilcode1 - Sep-26-2018 (Sep-26-2018, 12:15 PM)Axel_Erfurt Wrote:thank u very much dearimport socket print([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]) RE: regex help to get the local ip address - nilamo - Sep-26-2018 >>> import re >>> response = 'IPv4 Address. . . . . . . . . . . : 192.168.74.163 ' >>> match = re.search(r'((\d+\.?){4})', response) >>> match.groups() ('192.168.74.163', '163')But what happens if it's ipv6? |