How to extract IPv6 address from string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract IPv6 address from string?
# 1  
Old 05-01-2014
Question How to extract IPv6 address from string?

Hi All,

Would anyone know how to modify the below, so only the IPv6 address (red) is printed, please?
(in other words, what's between inet6 and the / sign)
Code:
ipv6=`/sbin/ifconfig lo0:5 inet6 | grep 'inet6'`
print $ipv6

Currently the output of the above script is:
Code:
inet6 2620:42::192:168:159:10/64

Thanks so much!
cg
# 2  
Old 05-01-2014
Code:
ifconfig lo0:5 inet6 | awk '/inet6/{gsub(/inet6 |\/.*/,x)}1'

# 3  
Old 05-01-2014
Question

Thanks, but no luck, unfortunately Smilie
Code:
# ifconfig lo0:5 inet6 | awk 'gsub(/inet6 |\/.*/,x)'
awk: syntax error near line 1
awk: bailing out near line 1

# 4  
Old 05-01-2014
Use nawk or /usr/xpg4/bin/awk instead on Solaris or SunOS:
Code:
ifconfig lo0:5 inet6 | nawk '/inet6/{gsub(/inet6 |\/.*/,x)}1'

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-01-2014
Question

Thanks, Yoda! That did the trick!

One other Q though... if possible, would you know offhand how to remove the leading space?
Code:
        2620:42::192:168:159:10

Thanks again
cg
# 6  
Old 05-01-2014
Code:
ifconfig lo0:5 inet6 | nawk '/inet6/{gsub(/ *inet6 |\/.*/,x)}1'

# 7  
Old 05-01-2014
Hmm... no luck there either. I tried doing a search online, since I thought removing whitespace in a variable would be a common request. I've tried the above and all these with no luck though: (ps, thanks so much for your help so far!!)
Code:
# ifconfig lo0:5 inet6 | /usr/xpg4/bin/awk 'gsub(/inet6 |\/.*/,x)' | sed 's/[[:space:]]//g'
        2620:42::192:168:159:10
# ifconfig lo0:5 inet6 | /usr/xpg4/bin/awk 'gsub(/inet6 |\/.*/,x)' | sed 's/[[:tab:]]//g'
        2620:42::192:168:159:10
# ifconfig lo0:5 inet6 | /usr/xpg4/bin/awk 'gsub(/inet6 |\/.*/,x)'
        2620:42::192:168:159:10
# ifconfig lo0:5 inet6 | /usr/xpg4/bin/awk 'gsub(/inet6 |\/.*/,x)' |  sed 's/ //g'
        2620:42::192:168:159:10
# ifconfig lo0:5 inet6 | /usr/xpg4/bin/awk 'gsub(/inet6 |\/.*/,x)' |  sed 's/\t//g'
        2620:42::192:168:159:10

---------- Post updated at 10:12 PM ---------- Previous update was at 07:32 PM ----------

Got the answer! I've been doing a lot of scouring the Internet for an answer to this one. It seems that every solution I've tried using awk and sed didn't seem to remove the leading whitespace. The following line using perl does work though:
Code:
/sbin/ifconfig lo0:1 inet6 | nawk 'gsub(/inet6 |\/.*/,x)' | perl -lapi -e 's/\s+|^\n//sg'

Thanks so much for the input, guys!
cg
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What determines DNS will query IPV6 address?

When I do simple nslookup it does not generate any query for IPV6 (AAAA). But sometimes I see DNS query for both A and AAAA are generated. What decides this? The reason I do not want AAAA query is in most of the cases AAAA records are absent. Hence, if one DNS server fails, the clients keeps... (1 Reply)
Discussion started by: broy32000
1 Replies

2. Red Hat

Find DHCPv6 server's IPv6 address

I want to find out the DHCPv6 server's ip address in the network. I went through the lease files but could find only duid/server-id and not the IPv6 address of the dhcp server. And I couldn't find any commands to get that information. Is there a way to get the DHCPv6 server's IPv6 address? ... (0 Replies)
Discussion started by: bshalini
0 Replies

3. IP Networking

IPv6 address block assign/associate with an interface

Hello all, I am trying to receive (tcp/udp/sctp) traffic from all IPs and, eventually, all ports of an IPv6 address block using as few sockets as possible short of implementing my own network stack. One possible solution was to associate an IP block to an interface then bind to that... (6 Replies)
Discussion started by: redwil
6 Replies

4. Solaris

Assiging "static" IPv6 address for a jumpstart target.

I provision Solaris10 using jumpstart. I know we can enable IPv6 by setting protocol_ipv6=yes in the sysidcfg file. Is there a way to assign Static IPv6 address using sysidcfg/jumpstart method (1 Reply)
Discussion started by: hemalsid
1 Replies

5. BSD

Link Local IPv6 Address

Hi, Am using FreeBSD7.4/i386 During IPv6 configuration, I added the following in rc.conf as Restarted IPv6 network using /etc/rc.d/network_ipv6 restart.. My problem is I need to set link local IPv6 address auto-configured.. Is my proceeding right?? I feel something missing to make... (0 Replies)
Discussion started by: Priya Amaresh
0 Replies

6. HP-UX

configuring site-local IPv6 address

How do I configure site-local IPv6 address in HP-UX box? I can get link local IPv6 address automatically when I put IPv6 up. aps39-88-root# ifconfig lan0 inet6 up (0 Replies)
Discussion started by: kirtikjr
0 Replies

7. Shell Programming and Scripting

Regular expression to extract ipv6 address

Hi all , I have a string in my weblog xheader v6-day-2011:xx:yy:zz:qq:qq:ww:ee:rr My requirement is to lookup the sting v6-day-2011 in this header and if found would like to extract the V6 ip part . v6-day-2011 is always constant for a ipv6 entry so i would like to extract every thing... (4 Replies)
Discussion started by: jambesh
4 Replies

8. Solaris

IPMP + IPv6 test address

Hi, inspired by this article, I decided to implement IPMP + IPv6 in Solaris 10. It worked for me only this way: 1. Setup # cat /etc/hostname* 10.23.10.113/24 broadcast + group data failover up <- hostname.e1000g0 0.0.0.0/24 broadcast + group data -failover deprecated up standby... (3 Replies)
Discussion started by: masloff
3 Replies

9. Programming

validate IPV6 address in windows using c++

I know there is a function inet_tpon for unix platforms to validate ipv6 addresses.But i need an equivalent of windows.When i use this function with the header file <winsock2.h> the visual studio 2005 on win2003 issues an error saying identifier not found :confused: (3 Replies)
Discussion started by: guru13
3 Replies

10. IP Networking

IPv6 UDP server not accepting packets with global address

Hi All, I am running a IPv6 UDP server, which is bound to in6addr_any. When I send a packet from a client to the link-local address of any interface on the linux box, the server accepts the packets. But when I send the packets to the global address the server doesnt pick the packets. On... (0 Replies)
Discussion started by: muralia
0 Replies
Login or Register to Ask a Question