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.

Return
table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.
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

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.

Return
table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.
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

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

Send an HTTP POST request with json content.

Return
true on success, false otherwise.
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.

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.

Return
the symbolic IP address.
Parameters
  • numeric_ip: the numeric IP address to convert.

function networkPrefix(string address, int netmask)

Apply a netmask to the specified IP address.

Return
the masked IP address.
Parameters
  • address: the IP address.
  • netmask: the network mask to apply.

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.

Return
getservbyport result on success, the port value on failure.
Parameters
  • port: service port.
  • proto: service protocol, e.g. “tcp”.

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.

Return
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.

Return
true on success, false otherwise
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

function resolveHost(string ip)

Resolve the given IP into an host name.

Return
the resolved host on success, nil otherwise.
Note
this call is blocking. Use getResolvedAddress() for a non blocking approach.
Parameters
  • ip: the host IP

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

Perform an SNMP GET request.

Return
a table with the results on success, nil otherwise
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

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

Perform an SNMP GETNEXT request.

Return
a table with the results on success, nil otherwise
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

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

Send a TCP probe and get the returned banner string.

Return
the banner string on success, nil otherwise.
Parameters
  • server_ip: the server IP address
  • server_port: the TCP service port
  • timeout: maximum timeout for the operation

function isIPv6(string addr)

Check if the given address is an IPv6 address.

Return
true if the addtess is a valid IPv6 address, false otherwise.
Parameters
  • addr: the IP address to check