Extract IP address from file using Python

Let us see how to extract IP addresses from a file using Python.

Algorithm :

  1. Import the re module for regular expression.
  2. Open the file using the open() function.
  3. Read all the lines in the file and store them in a list.
  4. Declare the patter for IP addresses. The regex pattern is :
    r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
  5. For every element of the list search for the patter using the search() function, store the IP addresses in a list.
  6. Display the list containing the IP addresses.

The file to be processed is test.txt :

test.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import re
  
# opening and reading the file 
with open('C:/Users/user/Desktop/New Text Document.txt') as fh:
   fstring = fh.readlines()
  
# decalring the regex pattern for IP addresses
pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
  
# initializing the list object
lst=[]
  
# extracting the IP addresses
for line in fstring:
   lst.append(pattern.search(line)[0])
  
# displaying the extracted IP adresses
print(lst)

chevron_right


Output :



The above Python program displays any kind of IP addresses present in the file. We can also display the valid IP addresses.

Rules for a valid IP Address :

  • The numbers should be in a range of 0-255
  • It should consist of 4 cells separated by ‘.’

The regular expression for valid IP addresses is :

((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

Explanation of Regular Expression used for valid IP:

Since we cannot use 0-255 range in regular expression we divide the same in 3 groups:

  • 25[0-5] – represents numbers from 250 to 255
  • 2[0-4][0-9] – represents numbers from 200 to 249
  • [01]?[0-9][0-9]?- represents numbers from 0 to 199

The file to be processed is test2.txt :

test2.txt 

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import re
  
# opening and reading the file
with open('test2.txt') as fh:
  string = fh.readlines()
    
# decalring the regex pattern for IP addresses 
pattern =re.compile('''((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)
{3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)''')
  
# initializing the list objects
valid =[]
invalid=[]
  
# extracting the IP addresses
for line in string:
    line = line.rstrip()
    result = regex.search(line)
  
    # valid IP addresses
    if result:
      valid.append(line)
  
    # invalid IP addresses  
    else:
      invalid.append(line)
  
# displaying the IP addresses
print("Valid IPs")
print(valid)
print("Invalid IPs")
print(invalid)

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.