Python Forum
Read list of IP addresses from file and return a network
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read list of IP addresses from file and return a network
#1
I have been trying for hours using Google and stack overflow to read a list of IP addresses and return networks. The ip address command works in the Python shell but seems to be tripping over the imported list. I have tried stripping the new line and reading the file in multiple different ways but I keep getting an error returned. I am sure it is something with how I am reading the file in but I just can't figure it out.

Here is the current code. Let's call it revision number 4186!

import ipaddress
def process(line):
    # Output network with mask bits (192.168.0.0/24)
    try:
        return ipaddress.IPv4Interface(line).network
    except Exception:
        return print("FAIL_OR_EMPTY")
with open('ipaddrlong.txt', 'r') as f:
    for line in f:
        process(line)
and the input file called looks like this. There is only the data and a newline (/n).

192.168.252.146/24
192.168.252.158/24
192.168.252.203/24
192.168.252.209/24
If I change the return line to a simple print, it looks fine to me. There are no extra spaces or characters.

'192.168.252.146/24', '192.168.252.158/24', '192.168.252.203/24', '192.168.252.209/24'
And when I try the command from the shell, it seems to work fine:

>>> x="192.168.0.1/24"
>>> ipaddress.IPv4Interface(x).network
IPv4Network('192.168.0.0/24')
But when I run the script the exception "FAIL_OR_EMPTY" is returned. I tried strip() but since there are no extra spaces, that did not help.
Reply


Messages In This Thread
Read list of IP addresses from file and return a network - by dflick - Oct-27-2018, 06:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 272 May-03-2024, 07:23 AM
Last Post: Pedroski55
  PyYAML read list of int zisco 2 381 Apr-02-2024, 12:36 PM
Last Post: zisco
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,841 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Recommended way to read/create PDF file? Winfried 3 2,972 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,606 Nov-09-2023, 10:56 AM
Last Post: mg24
  Input network device connection info from data file edroche3rd 6 1,145 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  How to read module/class from list of strings? popular_dog 1 517 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  read file txt on my pc to telegram bot api Tupa 0 1,175 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,167 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,420 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020