1

If I run /sbin/ifconfig eth0 I get the following output:

Link encap:Ethernet  HWaddr <address hidden for privacy>                                                 
          inet addr:<address hidden for privacy>  Bcast:<address hidden for privacy>  Mask:255.255.255.0                          
          inet6 addr: <address hidden for privacy> Scope:Link                                           
          inet6 addr: <address hidden for privacy> Scope:Global                                    
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1                                            
          RX packets:5891 errors:0 dropped:0 overruns:0 frame:0                                         
          TX packets:4933 errors:0 dropped:0 overruns:0 carrier:0                                       
          collisions:0 txqueuelen:1000                                                                  
          RX bytes:23922030 (22.8 MiB)  TX bytes:455152 (444.4 KiB)                                     

lo        Link encap:Local Loopback                                                                     
          inet addr:127.0.0.1  Mask:255.0.0.0                                                           
          inet6 addr: ::1/128 Scope:Host                                                                
          UP LOOPBACK RUNNING  MTU:65536  Metric:1                                                      
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0                                            
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0                                          
          collisions:0 txqueuelen:0                                                                     
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B) 

How do I get the ipv6 address where the scope is global ?

So far, If I do:

/sbin/ifconfig eth0 | awk '/inet6 / { print $3 }'

I get both the addresses (from scopes "link" AND "global"), but I want only the scope for "Global"

Also, this seems like a very messy way to get it, since I am relying on ifconfig outputting the data in a certain order / format. I would rather use commands to get exactly what I want.

2

If you want to be 100% independent of output formats you have to parse the /proc/net files. The interesting part is that file: /proc/net/if_inet6.

So you can extract the ip with:

awk '$6=="eth0"&&$4==00{print $1}' /proc/net/if_inet6

If the 4th value is 00 then the scope is global And if the 6th field is eth0 (or how your interfaces name is) print the first field (the ip address in hex).

3
  • But effectively, I'm still parsing something whose format can change right? XD – Kaizer Sozay Mar 18 '15 at 8:04
  • 1
    This is a stable published interface: tldp.org/HOWTO/Linux+IPv6-HOWTO/proc-net.html – Sander Steffann Mar 18 '15 at 8:05
  • @John effectifly yes =) but there is no other option than parsing a file or a command output. You can also get the ip via phyton, perl or php. And as Sander said, the format should really not change. If think ipv8 will come first then... – chaos Mar 18 '15 at 8:10
4

The classic Unix ifconfig is deprecated on Linux now. The ip command is the new hotness.

I'm not near a Linux box to test this right now, but the ip(8) man page seems to indicate that this should work:

ip addr show dev eth0 scope global
2
  • You're correct in that it displays the global ip's, but it still displays it like "inet6 addr: <address hidden for privacy> Scope:Global ", which means I still need to parse it. Parsing isn't difficult, but if the format changes in future my code will break. Any way to get just the address like "2600:abcd:asdl...." ? – Kaizer Sozay Mar 18 '15 at 3:46
  • @John Not possible to get it straight out of ip. But the -o option makes it easer to filter out the address: ip -6 -o addr show dev eth0 scope global | awk '{print $4}' – Torkel Bjørnson-Langen Sep 8 '15 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.