Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with ipaddress package
#1
I have some code where its reading from an input txt file... in the file are both valid IP and invalid IP, as well as valid CIDR. Im trying to do some validation before adding these IP to my firewall to make sure they are ( for the need of this case) only Public IP not private RFC1918 addresses. Here is the funciton im trying to make work:

def isPrivate_validaton():
    for newIpAddress in addressList:
        if ipaddress.ip_address(newIpAddress).is_private:
            print(newIpAddress + " this is a private address and cannot be used to access our Public SFTP service.")
            quit()
        elif ipaddress.ip_network(newIpAddress).is_private:
            print(newIpAddress) + " this is a private network CIDR and cannot be used to access our Public SFTP service"
            quit()
        else:
            continue
in my file i have the following values :
192.168.1.0/24
7.7.7.0/24

i keep getting errors stating 192.168.1.0/24 does not appear to be an IPV4 or IPV6 address and it stops.. ( as i expect it to, but the ip is a valid IP, its private but its valid). I'm sure I'm not fully understanding how the package works. But im stuck currently. Thanks in Advance for any tips.
Reply
#2
The /24 indicated the number of bits in the network mask.
I am not sure, but expect that this should be stripped before calling ipaddress methods.
Reply
#3
(Jul-12-2022, 10:08 PM)Larz60+ Wrote: The /24 indicated the number of bits in the network mask.
I am not sure, but expect that this should be stripped before calling ipaddress methods.

I wouldnt think so, because i have this other function below in the same total code block, that works as expected..

def valid_ip_or_cidr(ip):
    try:
        ipaddress.IPv4Address(ip)
        #print('This is a valid IPs address')
        return True
    except:
        try:
            ipaddress.IPv4Network(ip)
            #print('This is a valid network CIDR')
            return True
        except:
            print(ip + ' This is neither a valid IPV4 address  or a valid IPV4 CIDR notation, please check your data and try again')
            #return False
            sys.exit()
Reply
#4
(Jul-12-2022, 10:08 PM)Larz60+ Wrote: The /24 indicated the number of bits in the network mask.
I am not sure, but expect that this should be stripped before calling ipaddress methods.

but now that ive copied and pasted my two different code blocks i think i see my error... ill post up again once i fix my code :)
Reply
#5
I have never seen the /24 notation; if, as suggested, it is the subnet mask, it is irrelevant and the simplest thing would be to remove it.

It is a very, very, very bad idea to ever use exit() in any form within a program. Ever. Forget that you ever heard of this function, or any other function that terminates execution. They are all fundamentally bad ideas.

For example in your program, you run the program, there is a bad address in it, the program exits. You fix the bad address. You run it again. There is a second bad address. You fix it. You run it again. Etc. This is a terrible interface. You should simply note that there is an error, perhaps simply by implementing an error count. When you are all done, if the error count is greater than zero, you might consider raising an exception that you interpret as requesting the program terminate, which you capture in an except clause. But never, ever, think that it is valid to exit a program.

And, frankly, I'd store a tuple, with at least the IP address and line number in it, so the error message can give useful information, such as what line the error appeared on. This is part of being a professional programmer and knowing how to treat the users right. In addition, there is no reason to exit the program, because the correct response is to simply not add the bad IP address to the list of addresses you are going to use. Since the only thing the user can do is delete the offending line, why not just do that by ignoring it and generate a list of valid IP addresses which you will then use. The error messages are warning messages, the program runs to completion.

exit() and all similar functions are evil. Do not be evil.
Reply
#6
(Jul-13-2022, 02:01 AM)supuflounder Wrote: I have never seen the /24 notation; if, as suggested, it is the subnet mask, it is irrelevant and the simplest thing would be to remove it.

It is a very, very, very bad idea to ever use exit() in any form within a program. Ever. Forget that you ever heard of this function, or any other function that terminates execution. They are all fundamentally bad ideas.

For example in your program, you run the program, there is a bad address in it, the program exits. You fix the bad address. You run it again. There is a second bad address. You fix it. You run it again. Etc. This is a terrible interface. You should simply note that there is an error, perhaps simply by implementing an error count. When you are all done, if the error count is greater than zero, you might consider raising an exception that you interpret as requesting the program terminate, which you capture in an except clause. But never, ever, think that it is valid to exit a program.

And, frankly, I'd store a tuple, with at least the IP address and line number in it, so the error message can give useful information, such as what line the error appeared on. This is part of being a professional programmer and knowing how to treat the users right. In addition, there is no reason to exit the program, because the correct response is to simply not add the bad IP address to the list of addresses you are going to use. Since the only thing the user can do is delete the offending line, why not just do that by ignoring it and generate a list of valid IP addresses which you will then use. The error messages are warning messages, the program runs to completion.

exit() and all similar functions are evil. Do not be evil.

thank you for the feedback. Im a 20+year network engineer starting my journey to become a capable coder. so i appreciate the advice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ipaddress.IPv4Network tomthirteen 11 6,635 Jan-08-2019, 10:20 PM
Last Post: tomthirteen

Forum Jump:

User Panel Messages

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