![]() |
|
Trying to create IP address lookup script, getting errors - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Trying to create IP address lookup script, getting errors (/Thread-Trying-to-create-IP-address-lookup-script-getting-errors) |
Trying to create IP address lookup script, getting errors - Cheshire - 07-02-2014 Hey HC, I'm trying to create a python script that will allow me to check if an IP address that I enter belongs to any of various subnets that I will define. I'm trying to use Code: ipaddressCode: ipaddress.ip_network('192.168.1.0/24')
Traceback (most recent call last):
File "/var/folders/3d/67fb39kx2mb21yd8tqkzgrg40000gn/T/PythonRunner/dummy.py", line 1, in <module>
ipaddress.ip_network('192.168.1.0/24')
NameError: name 'ipaddress' is not definedIf Code: ipaddressBonus Points: Anyone have any suggestions on how to create an IP lookup for subnets if they think this isn't the way to go about doing it? RE: Trying to create IP address lookup script, getting errors - Ligeti - 07-02-2014 (07-02-2014, 12:05 AM)Cheshire Wrote: If ipaddress is a module, why am I getting an error saying it's not defined? It doesn't matter if I'm doing ipaddress.ip_address or any other variations, I still get the same "name not defined" error. Can we see the whole script/code? Did you import the module? Thanks RE: Trying to create IP address lookup script, getting errors - Cheshire - 07-02-2014 (07-02-2014, 12:11 AM)Ligeti Wrote: Can we see the whole script/code? Did you import the module? *Blush* Well, that would be one reason... Sorry for the stupid mistake. However, I'm trying to import it now and its saying there's no module named ipaddress. And I'm using python 3.4.1, btw. Here's what I'm working with right now. It's very basic, and I'm very new at python. Code: import ipaddress
ip = raw_input("Type IP:")
network = ipaddress.ip_network('192.168.1.0/24')
if ip in network:
print 'Yes'Side note: Thank you @Ligeti for the continual help. RE: Trying to create IP address lookup script, getting errors - Ligeti - 07-02-2014 (07-02-2014, 12:33 AM)Cheshire Wrote: I'm trying to import it now and its saying there's no module named ipaddress. And I'm using python 3.4.1, btw. You need to install the module... Do you have easy_install? If not... What OS are you using? Thanks [note]: BTW here is some info about easy_install and why one should not use it: https://workaround.org/easy-install-debian RE: Trying to create IP address lookup script, getting errors - Cheshire - 07-02-2014 (07-02-2014, 12:48 AM)Ligeti Wrote: You need to install the module... Do you have easy_install? If not... What OS are you using? No, I'm not using easy_install. I'm using a Mac OSX for this. RE: Trying to create IP address lookup script, getting errors - Cheshire - 07-02-2014 Also, I'm using python 3.4.1, and the documentation says that ipaddress is in the Python Standard Library (see Standard Library section 21.28) Why would I need to install it? Sorry if this is a dumb question. RE: Trying to create IP address lookup script, getting errors - Ligeti - 07-02-2014 I think so ... Why do I think? Because I am using Python 2.7 (still) and it is not installed... On Kali (Linux) I also have python 3.2 ... I tested that there and same negative result! So my guess is that yes you need to install it! Now... let's get to the root of the problem, and definitely it is not about ipaddress module... it is what you are trying to do: Quote:I'm trying to create a python script that will allow me to check if an IP address that I enter belongs to any of various subnets that I will define.I would do that manually (specially if you are studying networks it will give you a very good practice), so don't count on ipaddress or any other module... and to make your life a little easier (if possible) here is a link to a similar issue: http://stackoverflow.com/questions/819355/how-can-i-check-if-an-ip-is-in-a-network-in-python Thanks [note]: if you need more help please let me know! RE: Trying to create IP address lookup script, getting errors - Cheshire - 07-02-2014 Thanks for your help, @Ligeti. I'll check out the method you linked to. I also discovered what was giving me the error in the first place from trying to import the module: While I had downloaded and installed 3.4.1 from Python.org, python actually comes standard on Mac computers (which I'm using), so it was defaulting to the 2.7 pathway and not running in 3.4 when I ran my script. That's why it wouldn't import the module that was only in python3. Anyway, thanks again! |