DataScript: avi.utils.get_geo_from_ip


Function avi.utils.get_geo_from_ip (ip_string, [flag])
Description For a given IP address, returns a geolocation value from the geolocation database:
  • the 2-character country code, or
  • starting in 18.2.5, the ASN code.
Events All HTTP events
Parameters ip_string is a string containing the IP address for which geodatabase information is queried.

flag is a string added in release 18.2.5. Starting in release 18.2.5 it can be set to "COUNTRY" or "ASN."
  1. Returns nil if:

    • _ip_string_ contains an IPv6, internal, or invalid address,
    • _ip_string_ is not in the geolocation database, or
    • the value of _flag_ is incorrect (i.e., is neither "COUNTRY" nor "ASN")

  2. Returns a string containing an ISO 3166-1 alpha-2 country code (e.g., "AU" for Australia) if _flag_ is set to "COUNTRY" or if the _flag_ parameter is absent.

  3. Returns a 32-bit ASN code (e.g., 1449 for AS1449 PayPal, Inc) if _flag_ is set to "ASN"


The database used for this function is equivalent to the default one maintained on the Avi Controller. It cannot be overridden or augmented with IP groups, as mentioned in the Geolocation Database article.

To use this function, at least 100 MB of extra shared memory must be configured on the Service Engine group using it. This can be set via the "extra_shared_config_memory" parameter in the SE group, as illustrated below.
[admin:example-ctrl]: > configure serviceenginegroup Default-Group extra_shared_config_memory 100
[admin:example-ctrl]: serviceenginegroup > save

A restart of the SEs in this group is required for the memory changes to take effect.
If this memory is not set, any virtual services configured with a DataScript using this function will be put into a fault state. In the Avi UI, the error message will resemble the below.

fault state error message
Example The below DataScript adds a geo header that looks like

  • "Geo: not found" if the IP address is not found in the geolocation database, or
  • "Geo: <ASN>" if it is found.
client_ip = avi.vs.client_ip()
asn = avi.utils.get_geo_from_ip(client_ip, "ASN")
geo_header = "not found"
if asn ~= nil then
geo_header = asn
avi.http.add_header("Geo", geo_header)