1.4. Network API

The Network API provides utility functions to deal with IP addresses and send HTTP requests.

Functions

function httpGet(string url, string username = nil, string password = nil, int timeout = nil, bool return_content = false, bool cookie_auth = false)

Performs an HTTP GET request to the specified URL.

Parameters
  • url – the URL to fetch.

  • username – for HTTP authentication.

  • password – the password for HTTP authentication.

  • timeout – maximum connection timeout in seconds.

  • return_content – enable sending response content back to the caller.

  • cookie_auth – Use basic (default) or cookie (used by ntopng) authentication

Returns

table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.

function httpPost(string url, string data, string username = nil, string password = nil, int timeout = nil, bool return_content = false, bool cookie_auth = false)

Send an HTTP POST request with url encoded data.

Parameters
  • url – the target URL.

  • data – the url encoded data to send.

  • username – for HTTP authentication.

  • password – for HTTP authentication.

  • timeout – maximum connection timeout in seconds.

  • return_content – enable sending response content back to the caller.

  • cookie_auth – Use basic (default) or cookie (used by ntopng) authentication

Returns

table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.

function postHTTPJsonData(string username, string password, string url, string json)

Send an HTTP POST request with json content.

Note

HTTP header “Content-Type: application/json” is sent.

Parameters
  • username – for HTTP authentication. Pass empty string to disable authentication.

  • password – for HTTP authentication. Pass empty string to disable authentication.

  • url – the target URL.

  • json – the data to post.

Returns

true on success, false otherwise.

function send_udp_data(string host, int port, string data)

Send raw UDP data to a given host and port.

Parameters
  • host – the host IP address.

  • port – the host port.

  • data – the data to send.

function inet_ntoa(int numeric_ip)

This is the equivalent C inet_ntoa for Lua.

Parameters

numeric_ip – the numeric IP address to convert.

Returns

the symbolic IP address.

function networkPrefix(string address, int netmask)

Apply a netmask to the specified IP address.

Parameters
  • address – the IP address.

  • netmask – the network mask to apply.

Returns

the masked IP address.

function httpRedirect(string url)

Send an HTTP redirection header to the specified URL.

Note

this must be called before sending any other HTTP data.

Parameters

url – the URL to redirect to.

function httpPurifyParam(string str)

Purify a string from the HTTP standpoint. Used to purify HTTP params.

Note

The ourigied inout string with _ that replaced chars not allowed

Parameters

str – the string to purify

function getservbyport(int port, string proto)

A wrapper for C getservbyport.

Parameters
  • port – service port.

  • proto – service protocol, e.g. “tcp”.

Returns

getservbyport result on success, the port value on failure.

function pingHost(string host, bool is_v6)

Send an ICMP request to the given host.

Note

this can be called multiple times on different hosts and then ntop.collectPingResults() can be used to collect the results.

Parameters
  • host – the host name/IP address.

  • is_v6 – true for IPv6 connections, false for IPv4.

function collectPingResults()

Collect the ICMP replies after ntop.pingHost() calles.

Returns

a table with IP address -> RTT mappings

function sendMail(string from, string to, string msg, string smtp_server, string username = nil, string password = nil)

Send an email to the specified address.

Parameters
  • from – sender email and name

  • to – recipient email and name

  • msg – the message to send

  • smtp_server – the SMTP server address (e.g. smtp://myserver.com)

  • username – an optional username for the SMTP authentication

  • password – an optional password for the SMTP authentication

Returns

true on success, false otherwise

function resolveHost(string ip)

Resolve the given IP into an host name.

Note

this call is blocking. Use getResolvedAddress() for a non blocking approach.

Parameters

ip – the host IP

Returns

the resolved host on success, nil otherwise.

function snmpget(string agent_host, string community, string oid, int timeout = 5, int version = 1, string oids)

Perform an SNMP GET request.

Parameters
  • agent_host – the target SNMP host

  • community – the SNMP community

  • oid – the OID to query

  • timeout – maximum seconds before aborting the request

  • version – the SNMP version to use

  • oids – additional OIDs to query

Returns

a table with the results on success, nil otherwise

function snmpgetnext(string agent_host, string community, string oid, int timeout = 5, int version = 1, string oids)

Perform an SNMP GETNEXT request.

Parameters
  • agent_host – the target SNMP host

  • community – the SNMP community

  • oid – the OID to query

  • timeout – maximum seconds before aborting the request

  • version – the SNMP version to use

  • oids – additional OIDs to query

Returns

a table with the results on success, nil otherwise

function tcpProbe(string server_ip, int server_port = 5, int timeout = 3)

Send a TCP probe and get the returned banner string.

Parameters
  • server_ip – the server IP address

  • server_port – the TCP service port

  • timeout – maximum timeout for the operation

Returns

the banner string on success, nil otherwise.

function isIPv6(string addr)

Check if the given address is an IPv6 address.

Parameters

addr – the IP address to check

Returns

true if the addtess is a valid IPv6 address, false otherwise.