Get IPv6 address from IPv4?

13,229

There is no direct mapping between IPv4 and IPv6 addresses. The most reliable way I can think of (which requires you to be on the same network as the IPv4 node) is to get the MAC address for the IP, and construct a link-local EUI-64-based address from it. That is, IPv6 nodes will almost always automatically configure a MAC-based link-local address, so this should work in most cases.

The following quick-and-dirty example script works on Ubuntu Xenial (16.04) as long as the python3-netaddr package is installed. It doesn't seem to work on Trusty (14.04) since the netaddr library is missing some of the required functionality.

#!/bin/bash -e

IP=$1
ping -c 1 $1 > /dev/null 2> /dev/null
MAC=$(arp -an $1 | awk '{ print $4 }')
IFACE=$(arp -an $1 | awk '{ print $7 }')

python3 -c "
from netaddr import IPAddress
from netaddr.eui import EUI
mac = EUI(\"$MAC\")
ip = mac.ipv6(IPAddress('fe80::'))
print('{ip}%{iface}'.format(ip=ip, iface=\"$IFACE\"))"

Example usage (assuming you pasted the contents into ipv4-to-ipv6.sh and did a chmod +x ipv4-to-ipv6.sh):

./ipv4-to-ipv6.sh 192.168.0.1
fe80::224:b4ff:fe9c:1329%eth0

Note that this is a link-local address must be scoped to a particular link in order to be useful, so requires the %<interface> to be usable in most application. If your hosts have a global unicast prefix on this network as well, they would be probably be reachable using the MAC-based address there, too. (you could, for example, change the ip = mac.ipv6(...) line to use your global prefix, and that should work.)

Share:
13,229

Related videos on Youtube

Roboman1723
Author by

Roboman1723

Updated on September 18, 2022

Comments

  • Roboman1723
    Roboman1723 over 1 year

    Is it possible to do this? I don't care what tool, ping, nmap, nslookup. Hell, i'll even write a bash script. I just need to figure this out.

    Edit: All I know are IPv4 addresses and I want to see what their IPv6 addresses are. I realistically cannot scan a whole /32 block of IPv6 and that's terribly inefficient anyways. TL;DR

    IPv4 Input > IPv6 Output

    Pseudo-code:

    ping 1.2.3.4

    Result:

    1.2.3.4: IPv6 Address

    • Roboman1723
      Roboman1723 about 8 years
      Yes but ping6 cannot take in an IPv4 address. I want to input a IPv4 address and get an IPv6 address as a result.
    • John Orion
      John Orion about 8 years
      not sure that is possible since they are two different things ... the IPv4 address is not linked with the IPv6 address.
    • Admin
      Admin about 8 years
      Can't you use the ipv4 representation in ipv6, '::ffff:192.168.0.1' ?
    • Ron
      Ron about 8 years
      write a script ;)
    • John Orion
      John Orion about 8 years
      I have to be honest .. I'm not real familiar with IPv6 but I have never seen an IPv4 address in an IPv6. Not saying that it can't be done .. I just have never seen an IPv6 address like that ... Hopefully someone who knows more about IPv6 will answer the question for you or better explain it .. lol .. that would interest me too :D
    • Anders
      Anders about 8 years
      Reliable? No, there are no such mapping. Not even with the Link Local EUI-64 address, as that isn't always used.
  • Roboman1723
    Roboman1723 about 8 years
    Hey man thanks for the reply I'll give this a shot.
  • Roboman1723
    Roboman1723 about 8 years
    It's throwing an error on line 7 python3 -c ". Does there need to be a second "?
  • mpontillo
    mpontillo about 8 years
    @Roboman1723 the ending " is at the end of the script, on the last line. Did you sudo apt-get install python3-netaddr? (If not, the script will fail.) Are you running Ubuntu Xenial (16.04)? (If not, the script will fail.)
  • Roboman1723
    Roboman1723 about 8 years
    I didn't install netaddr. Confirms it works. Thanks a ton for the help!
  • kasperd
    kasperd almost 6 years
    Rather than assuming the address you are looking for was constructed from the MAC address you can instead ping ff02::1. That will give you a response from every IPv6 capable device on the segment. The command could look like this ping6 -nc2 ff02::1%eth0.