Project

General

Profile

Connect ipv6 » History » Version 3

Pau Escrich, 05/29/2013 10:40 AM

1 1 Pau Escrich
h1. Connect ipv6 link-local
2 1 Pau Escrich
3 1 Pau Escrich
Each working network interface in your Linux system have a special IPv6 address configured automatically by the Kernel. These are named IPv6 link-local and are inside the special prefix fe80::/10
4 1 Pau Escrich
The scope of these IPs is to communicate computers which are in the same colision domain (for example our LAN network). But, how to know the IPv6 link-local address of the host we want to achieve?
5 1 Pau Escrich
Using ICMPv6 we can discover machines in our network thanks to the special Multicast address "ff02::". So to discover routers (group 2) we can use the next command:
6 1 Pau Escrich
7 1 Pau Escrich
<pre>
8 1 Pau Escrich
ping6 ff02::2%eth0
9 1 Pau Escrich
</pre>
10 1 Pau Escrich
11 2 Pau Escrich
Then each router connected to our colision domain, will reply the ICMP request with its own IPv6 link-local address.
12 2 Pau Escrich
<pre>
13 2 Pau Escrich
p4u@nomada:~$ $ ping6 ff02::2%eth1 -c2
14 2 Pau Escrich
PING ff02::2%eth1(ff02::2) 56 data bytes
15 3 Pau Escrich
64 bytes from fe80::a2f3:c1ff:fe39:1cea: icmp_seq=1 ttl=64 time=0.956 ms
16 3 Pau Escrich
64 bytes from fe80::a2f3:c1ff:fe39:1cea: icmp_seq=2 ttl=64 time=0.949 ms
17 2 Pau Escrich
18 2 Pau Escrich
--- ff02::2%eth1 ping statistics ---
19 2 Pau Escrich
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
20 2 Pau Escrich
rtt min/avg/max/mdev = 0.949/0.952/0.956/0.031 ms
21 2 Pau Escrich
</pre>
22 2 Pau Escrich
23 2 Pau Escrich
We can use connect to it using ssh:
24 1 Pau Escrich
25 1 Pau Escrich
<pre>
26 1 Pau Escrich
ssh fe80::a2f3:c1ff:fe39:1cea%eth1
27 1 Pau Escrich
</pre>
28 1 Pau Escrich
29 1 Pau Escrich
Or copy files using scp:
30 1 Pau Escrich
31 1 Pau Escrich
<pre>
32 1 Pau Escrich
scp QMP.bin [fe80::a2f3:c1ff:fe39:1cea%eth1]:/tmp/
33 1 Pau Escrich
</pre>
34 1 Pau Escrich
35 1 Pau Escrich
This is a small script which check if there is some router attached to your network device and in case, try to connect to it. If there are not routers it waits until some appears.
36 1 Pau Escrich
37 1 Pau Escrich
<pre>
38 1 Pau Escrich
#!/bin/sh
39 1 Pau Escrich
INT=${1:-eth0}
40 1 Pau Escrich
MYIP="$(ip addr show dev $INT | grep -e "inet6.*scope link" | awk '{print $2}' | cut -d/ -f1)"
41 1 Pau Escrich
NEIGH="$(ping6 ff02::2%$INT -n -c3 2>/dev/null | grep fe80 2>/dev/null | awk '{print $4}' | sed 's/\(.*\)./\1/' | sort -u)"
42 1 Pau Escrich
RNEIGH="$(echo -e "$NEIGH" | grep -v $MYIP | grep -v "^$" | sort -u)"
43 1 Pau Escrich
if [ $(echo -e "$RNEIGH" | wc -l) -eq 1 -a $(echo -e "$RNEIGH" | wc -c) -gt 4 ]; then
44 1 Pau Escrich
	echo "Host found, connecting with it!"
45 1 Pau Escrich
	ssh $RNEIGH%$INT
46 1 Pau Escrich
elif [ -z "$RNEIGH" ]; then
47 1 Pau Escrich
	echo -n "."
48 1 Pau Escrich
	exec $0 $@
49 1 Pau Escrich
else
50 1 Pau Escrich
	echo "Several Hosts found:"
51 1 Pau Escrich
	echo -e "$RNEIGH"
52 1 Pau Escrich
fi
53 1 Pau Escrich
</pre>
54 1 Pau Escrich
55 1 Pau Escrich
56 1 Pau Escrich
An example of execution:
57 1 Pau Escrich
58 1 Pau Escrich
<pre>
59 1 Pau Escrich
p4u@nomada:~$ $ disc6 eth1
60 1 Pau Escrich
..................................Host found, connecting with it!
61 1 Pau Escrich
62 1 Pau Escrich
Warning: Permanently added 'fe80::a2f3:c1ff:fe39:7cea%eth1' (RSA) to the list of known hosts.
63 1 Pau Escrich
root@fe80::a2f3:c1ff:fe39:7cea%eth1's password: 
64 1 Pau Escrich
</pre>