ValentinBELYN / icmplib

Easily forge ICMP packets and make your own ping and traceroute.
GNU Lesser General Public License v3.0
267 stars 45 forks source link

AttributeError: module 'socket' has no attribute 'IPPROTO_ICMPV6' #33

Closed HardipinderS closed 3 years ago

HardipinderS commented 3 years ago

Hi,

I came across you're package and was hoping to check multiple servers whether they're up or not.

So, I went for multiping as suggested in the docs. The problem is when the socket opens it gives the error

AttributeError: module 'socket' has no attribute 'IPPROTO_ICMPV6'

I went snooping in the code base, it refers to line 579 in sockets.py

return socket.socket(
family=socket.AF_INET6,            
type=type,            
proto=socket.IPPROTO_ICMPV6)

I've tried the module with both py 3.7.9 and 3.9.5. Works perfectly fine in 3.9.5 but gives the error in 3.7.9.

I changed the codebase in 3.7.9, with

IPPROTO_ICMP

and it worked.

So my request is if we can amend the current codebase to

try:

return socket.socket(
family=socket.AF_INET6,            
type=type,            
proto=socket.IPPROTO_ICMPV6)

except AttributeError:

return socket.socket(
family=socket.AF_INET6,            
type=type,            
proto=socket.IPPROTO_ICMP)

let me know if it is possible. or any help required from my side.

Also, on side, haven't gone through the whole code base but needed some guidance, will be multiping be quicker on roughly 70 servers, or should I think about async socket programming? PS: the client wants the result after every 30 seconds.

Thanks in advance!

ValentinBELYN commented 3 years ago

Hi @HardipinderS!

The problem you see is really strange. Can you tell me which operating system you are using as well as its version?

Moreover, what are the parameters transmitted to the multiping function?

Finally, what are the values of IPPROTO_ICMP and IPPROTO_ICMPV6 under Python 3.7.9 and Python 3.9.5? IPPROTO_ICMP with IPv6 sockets should not work.

Regarding your second question, the multiping function was designed to be very efficient on a large number of hosts. You can ping over 1000 hosts in just seconds! This shouldn't be a problem for 70 servers ๐Ÿ˜„

HardipinderS commented 3 years ago

Hi @HardipinderS!

The problem you see is really strange. Can you tell me which operating system you are using as well as its version?

Moreover, what are the parameters transmitted to the multiping function?

Finally, what are the values of IPPROTO_ICMP and IPPROTO_ICMPV6 under Python 3.7.9 and Python 3.9.5? IPPROTO_ICMP with IPv6 sockets should not work.

Regarding your second question, the multiping function was designed to be very efficient on a large number of hosts. You can ping over 1000 hosts in just seconds! This shouldn't be a problem for 70 servers ๐Ÿ˜„

First of all thank you for replying so quickly.

The error occurred on windows 10 and windows 2016 server os.

Parameters were as per the example just the ips. I tried adding count and payload but same error occurred. I believe it was the socket creation that caused the problem. Because when I did

It never showed IPPROTOV6

True, ipv6 won't work, but if we have a condition statement that checks if the ip entered is v6 or v4 that might solve the issue. I would suggest explicitly as per Zen, but if we want implicit, I don't exactly remember, but python has an inbuild library for checking. EDIT: Found the module it is ipaddress (https://stackoverflow.com/a/56038331)

Let me know what you think.

And thanks for the clarification of multiping, I wanted expert advise, so that helps๐Ÿ˜Š

ValentinBELYN commented 3 years ago

Can you give me the result of dir(socket) under Python 3.7.9 and 3.9.5? It may just be a bug present in Python 3.7.9 version of Windows. I will also do some tests today.

If I understood correctly, you only encounter this problem when using IPv6 addresses?

Otherwise, it is normal if IPPROTO_IPV6 is not present on Windows. A fix has already been set up for this constant: https://github.com/ValentinBELYN/icmplib/blob/main/icmplib/sockets.py#L547

And there is already a condition to check the type of IP address ๐Ÿ˜‰: https://github.com/ValentinBELYN/icmplib/blob/main/icmplib/ping.py#L133

HardipinderS commented 3 years ago

hey,

I thought the same, but was not sure if changing anything or raising a bug in 3.7.9 will be beneficial, as they're planning mostly to push towards 3.10 and 4 eventually.

dir(socket) result =

['AF_APPLETALK', 'AF_DECnet', 'AF_INET', 'AF_INET6', 'AF_IPX', 'AF_IRDA', 'AF_SNA', 'AF_UNSPEC', 'AI_ADDRCONFIG', 'AI_ALL', 'AI_CANONNAME', 'AI_NUMERICHOST', 'AI_NUMERICSERV', 'AI_PASSIVE', 'AI_V4MAPPED', 'AddressFamily', 'AddressInfo', 'CAPI', 'EAGAIN', 'EAI_AGAIN', 'EAI_BADFLAGS', 'EAI_FAIL', 'EAI_FAMILY', 'EAI_MEMORY', 'EAI_NODATA', 'EAI_NONAME', 'EAI_SERVICE', 'EAI_SOCKTYPE', 'EBADF', 'EWOULDBLOCK', 'INADDR_ALLHOSTS_GROUP', 'INADDR_ANY', 'INADDR_BROADCAST', 'INADDR_LOOPBACK', 'INADDR_MAX_LOCAL_GROUP', 'INADDR_NONE', 'INADDR_UNSPEC_GROUP', 'IPPORT_RESERVED', 'IPPORT_USERRESERVED', 'IPPROTO_ICMP', 'IPPROTO_IP', 'IPPROTO_RAW', 'IPPROTO_TCP', 'IPPROTO_UDP', 'IPV6_CHECKSUM', 'IPV6_DONTFRAG', 'IPV6_HOPLIMIT', 'IPV6_HOPOPTS', 'IPV6_JOIN_GROUP', 'IPV6_LEAVE_GROUP', 'IPV6_MULTICAST_HOPS', 'IPV6_MULTICAST_IF', 'IPV6_MULTICAST_LOOP', 'IPV6_PKTINFO', 'IPV6_RECVRTHDR', 'IPV6_RECVTCLASS', 'IPV6_RTHDR', 'IPV6_TCLASS', 'IPV6_UNICAST_HOPS', 'IPV6_V6ONLY', 'IP_ADD_MEMBERSHIP', 'IP_DROP_MEMBERSHIP', 'IP_HDRINCL', 'IP_MULTICAST_IF', 'IP_MULTICAST_LOOP', 'IP_MULTICAST_TTL', 'IP_OPTIONS', 'IP_RECVDSTADDR', 'IP_TOS', 'IP_TTL', 'IntEnum', 'IntFlag', 'MSG_BCAST', 'MSG_CTRUNC', 'MSG_DONTROUTE', 'MSG_ERRQUEUE', 'MSG_MCAST', 'MSG_OOB', 'MSG_PEEK', 'MSG_TRUNC', 'MSG_WAITALL', 'MsgFlag', 'NI_DGRAM', 'NI_MAXHOST', 'NI_MAXSERV', 'NI_NAMEREQD', 'NI_NOFQDN', 'NI_NUMERICHOST', 'NI_NUMERICSERV', 'RCVALL_MAX', 'RCVALL_OFF', 'RCVALL_ON', 'RCVALL_SOCKETLEVELONLY', 'SHUT_RD', 'SHUT_RDWR', 'SHUT_WR', 'SIO_KEEPALIVE_VALS', 'SIO_LOOPBACK_FAST_PATH', 'SIO_RCVALL', 'SOCK_DGRAM', 'SOCK_RAW', 'SOCK_RDM', 'SOCK_SEQPACKET', 'SOCK_STREAM', 'SOL_IP', 'SOL_SOCKET', 'SOL_TCP', 'SOL_UDP', 'SOMAXCONN', 'SO_ACCEPTCONN', 'SO_BROADCAST', 'SO_DEBUG', 'SO_DONTROUTE', 'SO_ERROR', 'SO_EXCLUSIVEADDRUSE', 'SO_KEEPALIVE', 'SO_LINGER', 'SO_OOBINLINE', 'SO_RCVBUF', 'SO_RCVLOWAT', 'SO_RCVTIMEO', 'SO_REUSEADDR', 'SO_SNDBUF', 'SO_SNDLOWAT', 'SO_SNDTIMEO', 'SO_TYPE', 'SO_USELOOPBACK', 'SocketIO', 'SocketKind', 'SocketType', 'TCP_FASTOPEN', 'TCP_MAXSEG', 'TCP_NODELAY', '_GLOBAL_DEFAULT_TIMEOUT', '_GiveupOnSendfile', '_LOCALHOST', '_LOCALHOST_V6', 'all', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', '_blocking_errnos', '_intenum_converter', '_realsocket', '_socket', 'close', 'create_connection', 'dup', 'errno', 'error', 'errorTab', 'fromfd', 'fromshare', 'gaierror', 'getaddrinfo', 'getdefaulttimeout', 'getfqdn', 'gethostbyaddr', 'gethostbyname', 'gethostbyname_ex', 'gethostname', 'getnameinfo', 'getprotobyname', 'getservbyname', 'getservbyport', 'has_ipv6', 'herror', 'htonl', 'htons', 'inet_aton', 'inet_ntoa', 'inet_ntop', 'inet_pton', 'io', 'ntohl', 'ntohs', 'os', 'selectors', 'setdefaulttimeout', 'socket', 'socketpair', 'sys', 'timeout']

"Thought it might be easier compared to screenshot "

No no, the problem is while using normal ips(IPv4) itself, I don't think I'll ever be able to utilize IPv6 in my current company.

For the rest I guess you were one step ahead of me ;D I'll have to go through your code to know if something is getting missed, But as of now, changing it to IPPROTO_ICMP fixed the issue for me

ValentinBELYN commented 3 years ago

There is something that I do not understand. If you don't use an IPv6 address, why are you getting this kind of error...

If you are using the multiping function with only one IP address (ex: multiping(['1.1.1.1'])), do you have this same problem?

Now, if you use the ping function this time with the same IP address (ex: ping(['1.1.1.1'])), are you experiencing the same phenomenon?

Thanks in advance!

ValentinBELYN commented 3 years ago

I have done a lot of tests under different versions of Python on Windows and I have not encountered any problem with the multiping function. Can you give me the IP addresses you are having trouble with?

However, I have located a bug with IPv6 under Python 3.7 and it is now fixed.

HardipinderS commented 3 years ago

Let me start by saying sorry for the delayed reply. Been busy with few things.

Answering your questions in order, happens with multiping only. happens both times with one ip address and multiple ip addresses. Happens only in 3.7.9.

I commend your effort, but the moment I run it with 3.7.9, the problem occurs. The ip addresses were as per your example in the first try. Second try was two ip address[192.168.38.163, 192.168.45.146]. Third try was these two with addition of ::1 and fourth, was deletion of ::1 and addition two ips 192.168.38.172 and 192.168.45.147. But all gave me save attributeerror. And once I changed the error part in your codebase, it worked.

I know this not what you were hoping to hear, but I hope this might have helped in bringing clarity on the issue.

You mentioned you got the bug fixed in 3.7. First of all, wow. Second, if you don't mind my asking, how?

ValentinBELYN commented 3 years ago

It's pretty reassuring that the problem isn't affecting other functions, even though I can't reproduce the behavior.

For the version 3.0 of icmplib which will be released this weekend, I notably integrated a brand new implementation of the multiping function which should also fix your problem.

If you want to test in the meantime, you can follow these instructions:

# Uninstall icmplib
pip3 uninstall icmplib

# Download and extract this repository:
# https://github.com/ValentinBELYN/icmplib/archive/main.tar.gz

# Go to the extracted directory

# Install the version under development
python3 setup.py install

Regarding the bug affecting IPv6, it has been fixed here: 6bfb44aa1a87dee11bf687a97e247909a89f10b3

If you have time, don't hesitate to give me a feedback! ... and sorry for all the questions I asked you ๐Ÿ˜„

HardipinderS commented 3 years ago

I would love to be a part of that. I will try that out and let you know how it prevails.

It was nice working with you, and happy to answer all your questions, it was the only way to get to the bottom of it. Just wanted to let you know that you codebase will be utilised in an automation and hopefully if things go as planned, in Django backend too that is part of Wipro . So, Wanted to say really thankful for your dedicated effort. If I can contribute to your work in any way let me know๐Ÿ˜Š

ValentinBELYN commented 3 years ago

Hi! Sorry for the delay. I am currently preparing the release of version 3 of icmplib.

I'm glad you enjoy this library! It is in this kind of remark that I draw my motivation โค๏ธ. Hope you enjoy the next version as much! It will be released on June 1st.

ValentinBELYN commented 3 years ago

icmplib 3.0 is available for download! :rocket:

Your problem should now be resolved!

See what's new