Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

how to automatically grab openvz vps ip
New on LowEndTalk? Please Register and read our Community Rules.

how to automatically grab openvz vps ip

BellaBella Member
edited July 2014 in Help

Hi.

I am writing an auto install script for a vpn and I was wondering how I would automatically grab my openvz vps IP and put it into a variable so I can use it to set ip tables.

Comments

  • RalliasRallias Member, Provider

    IPv4: ip -4 addr show venet0 | grep inet | awk '{print $2}' | awk -F/ '{print $1}'

    IPv6: ip -6 addr show venet0 | grep inet6 | awk '{print $2}' | awk -F/ '{print $1}'

    Thanked by 2netomx Boxode
  • blackblack Member

    @Bella said:
    Hi.

    I am writing an auto install script for a vpn and I was wondering how I would automatically grab my openvz vps IP and put it into a variable so I can use it to set ip tables.

     curl getipaddr.net -s | head -n 1 
  • RalliasRallias Member, Provider

    @black said:

     curl getipaddr.net -s | head -n 1 

    If he's using IPTables, that might very well be useless in a NAT situation.

    Thanked by 1netomx
  • blackblack Member

    @Rallias said:
    If he's using IPTables, that might very well be useless in a NAT situation.

    True, assuming he's using NAT.

    Thanked by 1netomx
  • amhoabamhoab Member

    Also try hostname -i.

  • BellaBella Member
    edited July 2014

    @Rallias said:
    IPv4: ip -4 addr show venet0 | grep inet | awk '{print $2}' | awk -F/ '{print $1}'

    The output of that shows two IP's

    [[email protected] ~]# ip -4 addr show venet0 | grep inet | awk '{print $2}' | awk -F/ '{print $1}'
    127.0.0.1
    192.XXX.XXX.XX

    @black said:

     curl getipaddr.net -s | head -n 1 

    [[email protected] ~]# curl getipaddr.net -s | head -n 1
    192.XXX.XXX.XX

    That works perfectly, thanks!

  • RalliasRallias Member, Provider

    Bella said: The output of that shows two IP's

    Then pipe it into grep -v ' 127' (watch the space)

  • 5n1p5n1p Member

    Some time ago I found the way with python (cant remember where):


    import socket
    print([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])

    for ipv6 i changed it like this:


    import socket
    print([(s.connect(('ipv6.google.com', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)]][0][1])

  • agentsmithagentsmith Member
    edited July 2014
    curl
    

    needs a network connection to setup rules so I use this:

    EXTIP=$(/sbin/ifconfig venet0:0 |sed -rn 's/.*inet addr:(.*)  P-t-P.*/\1/p')
    
Sign In or Register to comment.