Network configuration

Commands related to network configuration

SYNOPSIS

package require twapi
address_to_hostname IPADDRESS ?-async SCRIPT? ?-flushcache?
flush_arp_table INTERFACEINDEX
get_arp_table ?options?
get_ip_addresses
get_netif_count
get_netif_indices
get_netif_info NETWORKINTERFACE ?options?
get_network_info ?options?
get_outgoing_interface DESTADDR
get_route ?options?
get_routing_table ?-sort?
get_tcp_connections ?options?
get_udp_connections ?options?
hostname_to_address HOSTNAME ?-async SCRIPT? ?-flushcache?
hwaddr_to_ipaddr HWADDR ?VARNAME?
ipaddr_to_hwaddr IPADDR ?VARNAME?
port_to_service PORTNUMBER
service_to_port SERVICENAME
terminate_tcp_connections ?options?

DESCRIPTION

This module provides procedures to retrieve network information

Overview

This module provides commands to retrieve various network-related data for the local system including DNS and DHCP information, type and configuration of network interfaces, traffic statistics and various other information. The module also provides some client side name resolution lookup functions including nonblocking name and address lookups. Many of these commands and some options are not available on all platforms as noted in each command description.

Commands

address_to_hostname IPADDRESS ?-async SCRIPT? ?-flushcache?
Attempts to map the specified Internet address IPADDRESS to its fully qualified DNS name (FQDN).

If the -async option is not specified, the command returns the FQDN if available and the empty string otherwise. Note that this is a blocking call and the Tcl event loop will not be run until the command returns.

If the -async option is specified, the command returns right away. The address resolution is done in the background and when completed, SCRIPT is called at the global scope. Three additional arguments are appended to the script before it is executed - IPADDRESS, status success or fail and either the FQDN (when the status is success) or the Winsock error code (when the status is fail). The Winsock error code may be translated to an error string using the map_windows_error command. If no names are found, the status is success but the third argument will be an empty string.

Internally, the command maintains a cache of address to hostname mappings so as to avoid unnecessary lookups. The result is normally returned from here if available. However, if the -flushcache option is specified, the cache entry is deleted and an explicit lookup up is done.
flush_arp_table INTERFACEINDEX
Flushes the ARP table associated with the specified network interface.
get_arp_table ?options?
Returns the entries in the system's ARP table. Each entry is a sublist consisting of four elements - the interface index, the hardware address, the IP address and the type. The type is one of dynamic, static, other or invalid.

The following options control the returned data:
-ifindex INTERFACEINDEX Limits the returned entries to those in the ARP table for the specified network interface with index INTERFACEINDEX.
-sort Returns the entries sorted by IP address.
-validonly Skips entries that are marked invalid.
get_ip_addresses
Returns a list of IP addresses for the system.
get_netif_count
Returns the number of network interfaces in the system.
get_netif_indices
Network interfaces in the system are identified by integer indices. This command retrieves the list of network interfaces in the system.

Example: Show network interface information
get_netif_info NETWORKINTERFACE ?options?
Returns various information about the configuration of a network interface. NETWORKINTERFACE must be the index of a network interface or a name identifying the interface.

The information returned depends on the options specified and is in the form of a flat list of the form "option1 value1 ...". The following options may be specified:
-adapterdescription Returns a description of the adapter associated with the interface. This option is supported on Windows 2000 and higher.
-adapterindex Returns the index of the adapter associated with the interface. This option is supported on Windows 2000 and higher.
-adaptername Returns the name of the adapter associated with the interface. This option is supported on Windows 2000 and higher.
-adminstatus Returns 1 or 0 depending on whether the interface has been administratively enabled or not.
-all Returns all option values.
-autoconfigactive Returns 1 if the interface has been autoconfigured with an address, and 0 otherwise. This option is supported on Windows 2000 and higher.
-autoconfigenabled Returns 1 if autoconfiguration is enabled on the interface if a DHCP server cannot be found, and 0 otherwise. This option is supported on Windows 2000 and higher.
-defaultgateway Returns the address of the default gateway on the interface. This option is supported on Windows 2000 and higher.
-description Returns a description of the interface.
-dhcpenabled Returns 1 if DHCP is enabled on the interface, else 0. This option is supported on Windows 2000 and higher.
-dhcpleaseend Returns the time, in seconds since Jan 1, 1970, that the DHCP lease will expire. This option is supported on Windows 2000 and higher.
-dhcpleasestart Returns the time, in seconds since Jan 1, 1970, that the DHCP lease was obtained. This option is supported on Windows 2000 and higher.
-dhcpserver Returns the address of the DHCP server on the network interface. This option is supported on Windows 2000 and higher.
-dnsservers Returns a list of DNS servers on that interface. This option is supported on Windows 2000 and higher.
-havewins Returns 1 if the interface uses WINS, else 0. This option is supported on Windows 2000 and higher.
-ifindex Returns the interface index. This is the same as INTERFACEINDEX unless the latter is specified as a name.
-ifname Returns the name of the network interface.
-inbytes Returns the number of bytes received through this interface.
-indiscards Returns the number of incoming packets on this interface that were discarded even though they had no errors.
-inerrors Returns the number of incoming packets on this interface that were discarded because of errors.
-innonunicastpkts Returns the number of broadcast or multicast packets received through this interface.
-inunicastpkts Returns the number of unicast packets received through this interface.
-inunknownprotocols Returns the number of incoming packets on this interface that were discarded because of the protocol was unknown.
-laststatuschange Returns the last time the operational status changed.
-ipaddresses Returns a list sublists containing the IP addresses and masks for the interface.
-mtu Returns the maximum transmission unit of the interface.
-operstatus Returns the operational status of the interface. This is one of the values nonoperational, wanunreachable, disconnected, wanconnecting, wanconnected, operational.
-outbytes Returns the number of bytes transmitted through this interface.
-outdiscards Returns the number of outgoing packets on this interface that were discarded even though they had no errors.
-outerrors Returns the number of outgoing packets on this interface that were discarded because of errors.
-outnonunicastpkts Returns the number of broadcast or multicast packets transmitted through this interface.
-outqlen Returns the output queue length on this interface.
-outunicastpkts Returns the number of unicast packets transmitted through this interface.
-physicaladdress Returns the physical address of the network interface.
-primarywins Returns the address of the primary WINS server, if any.
-reassemblysize Returns the maximum size of a reassembled packet that is supported by the interface.
-secondarywins Returns the address of the secondary WINS server, if any.
-speed Returns the speed of the network interface in bits per second.
-type Returns the network interface type. This is one of ethernet, tokenring, fddi, ppp, loopback, slip or other.


Example: Show network interface information

Example: Show network statistics

get_network_info ?options?
Returns various information about network configuration. The information returned depends on the options specified and is in the form of a flat list of the form "option1 value1 ...". The following options may be specified:
-all Returns all values.
-arpproxyenabled Returns 1 if ARP proxying is enabled and 0 otherwise.
-dhcpscopeid Returns the DHCP scope id.
-dnsenabled Returns 1 if DNS name resolution is enabled on the system and 0 otherwise.
-dnsservers Returns a list of DNS server addresses used for name resolution.
-domain Returns the system domain.
-hostname Returns the host name of the system.
-routingenabled Returns 1 if routing is enabled on the system and 0 otherwise.
get_outgoing_interface DESTADDR
Returns the index of the network interface that will be used to send a packet to the IP address specified by DESTADDR
get_route ?options?
Returns the best route to a specified destination. The following options may be specified:
-dest DESTADDR Specifies the destination address for which the route is to be returned. If unspecified, defaults to 0.0.0.0 which corresponds to the default route.
-source SRCADDR Specifies the source address to be used. This can affect the route selected. Defaults to 0.0.0.0 which indicates the source address should not affect which route is selected.
The returned information is a keyed list with the following fields:
-addr The destination address that together with the -mask field specifies the address range to which the route applies.
-age The age of the route in seconds. This is valid only if the Routing and Remote Access service is running and the protocol is netmgmt.
-ifindex The interface index for this route.
-mask The network mask that together with -dest specifies the address range to which the route applies.
-metric1 A routing protocol specific metric value.
-metric2 A routing protocol specific metric value.
-metric3 A routing protocol specific metric value.
-metric4 A routing protocol specific metric value.
-metric5 A routing protocol specific metric value.
-nexthop The IP address of next hop in the route to the destination.
-nexthopas The autonomous system number of the next hop.
-policy Multi-path route selector condition, typically in IP TOS format as defined in RFC 1354.
-protocol Protocol that generated the route.
-type The type of the route. This is one of local, remote, other or invalid.
get_routing_table ?-sort?
Returns the network routing table as a list of routes. Each element of the route is in the format described in the get_route command description.
get_tcp_connections ?options?
Retrieves information about currently active TCP connections similar to the Windows netstat command. The return value is a list of sublists, each corresponding to a single connection.

The information returned for each connection is a flat list of the form "option1 value1 ...". The information returned for each connection is controlled through the following options:
-all Returns all fields for a connection. This option is assumed if no other field option is specified.
-bindtime Returns the time at which the last bind operation was done on the socket, or 0 if no bind has been done. On some platforms, this may not be available in which case, an empty string is returned for the value.
-localaddr Returns the local address being used for the connection.
-localport Returns the local port number being used for the connection.
-modulename Returns the name of the process module that owns the socket. On some platforms, this may not be available in which case, an empty string is returned for the value.
-modulepath Returns the path to the process module that owns the socket. On some platforms, this may not be available in which case, an empty string is returned for the value.
-pid Returns the PID of the process communicating on the port. On some platforms, this may not be available in which case, an empty string is returned for the value.
-remoteaddr Returns the address of the remote end of the connection.
-remoteport Returns the port number of the remote end of the connection.
-state Returns the connection state. This is one of closed, listen, syn_sent, syn_rcvd, estab, fin_wait1, fin_wait2, close_wait, closing, last_ack, time_wait or delete_tcb.
In addition, the connections for which information is returned may be filtered through the following options. Only those connections that match the specified criteria are returned.
-matchlocaladdr HOSTLIST Only returns those connections whose local address is one of those specified in HOSTLIST. Each element of HOSTLIST may be either an IP address or a host name.
-matchlocalport PORTNUMBER Only returns those connections whose local port is the specified number.
-matchpid PID Only returns those connections owned by the process with the specified PID. On platforms where the PID is not available, specifying this option will generate an error.
-matchremoteaddr HOSTLIST Only returns those connections whose remote address is one of those specified in HOSTLIST. Each element of HOSTLIST may be either an IP address or a host name.
-matchremoteport PORTNUMBER Only returns those connections whose remote port is PORTNUMBER.
matchstate STATELIST Only returns those connections whose state is one those specified in STATELIST which should be a list of connection state values (see above).
get_udp_connections ?options?
Retrieves information about currently active UDP sockets similar to the Windows netstat command. The return value is a list of sublists, each corresponding to a single open socket.

The information returned for each connection is a flat list of the form "option1 value1 ...". The fields returned for each connection is controlled through the following options:
-all Returns all fields for a connection. This option is assumed if no other field option is specified.
-bindtime Returns the time at which the last bind operation was done on the socket, or 0 if no bind has been done. On some platforms, this may not be available in which case, an empty string is returned for the value.
-localaddr Returns the local address being used for the connection.
-localport Returns the local port number being used for the connection.
-modulename Returns the name of the process module that owns the socket. On some platforms, this may not be available in which case, an empty string is returned for the value.
-modulepath Returns the path to the process module that owns the socket. On some platforms, this may not be available in which case, an empty string is returned for the value.
-pid Returns the PID of the process communicating on the port. On some platforms, this may not be available in which case, an empty string is returned for the value.
In addition, the connections for which information is returned may be filtered through the following options. Only those connections that match the specified criteria are returned.
-matchlocaladdr HOSTLIST Only returns those connections whose local address is one of those specified in HOSTLIST. Each element of HOSTLIST may be either an IP address or a host name.
-matchlocalport PORTNUMBER Only returns those connections whose local port is the specified number.
-matchpid PID Only returns those connections owned by the process with the specified PID. On platforms where the PID is not available, specifying this option will generate an error.
hostname_to_address HOSTNAME ?-async SCRIPT? ?-flushcache?
Attempts to map the specified host name to its IP addresses. Note there may be more than one address associated with a host name.

If the -async option is not specified, the command returns the list of IP addresses corresponding to the host if available and an empty list otherwise. Note that this is a blocking call and the Tcl event loop will not be run until the command returns.

If the -async option is specified, the command returns right away. The name resolution is done in the background and when completed, SCRIPT is called at the global scope. Three additional arguments are appended to the script before it is executed - HOSTNAME, status success or fail and either the list of IP addresses (when the status is success) or the Winsock error code (when the status is fail). The Winsock error code may be translated to an error string using the map_windows_error command. If no addresses are found, the status is success but the third argument will be an empty list.

Internally, the command maintains a cache of address to hostname mappings so as to avoid unnecessary lookups. The result is normally returned from here if available. However, if the -flushcache option is specified, the cache entry is deleted and an explicit lookup up is done.
hwaddr_to_ipaddr HWADDR ?VARNAME?
Returns the IP address corresponding to the given hardware address. If VARNAME is specified, the command stores the result in a variable of that name in the caller's context if a mapping can be found and returns 1. Otherwise the command returns 0 without affecting the variable. If VARNAME is not specified, the command returns the IP address if a mapping is found and generates a Tcl exception otherwise.
ipaddr_to_hwaddr IPADDR ?VARNAME?
Returns the hardware address corresponding to the given IP address. If VARNAME is specified, the command stores the result in a variable of that name in the caller's context if a mapping can be found and returns 1. Otherwise the command returns 0 without affecting the variable. If VARNAME is not specified, the command returns the hardware address if a mapping is found and generates a Tcl exception otherwise.

Example: Map an IP address to a physical address

port_to_service PORTNUMBER
Returns the service name (e.g. http) corresponding to the specified port number. If no corresponding service name exists, an empty string is returned.
service_to_port SERVICENAME
Returns the port number corresponding to the specified service name (e.g. http). If the service name is not defined, an empty string is returned. If SERVICENAME is a port number, it is returned.
terminate_tcp_connections ?options?
Terminates all connections that match all the options specified. Any options that are not specified are assumed to match. The valid matching options are:
-matchlocaladdr IPADDR Only matches those connections whose local address is bound to IPADDR.
-matchlocalport PORTNUMBER Only matches those connections whose local port is the specified number.
-matchpid PID Only matches those connections owned by the process with the specified PID. On platforms where the PID is not available, specifying this option will generate an error.
-matchremoteaddr IPADDR Only matches those connections whose remote address is bound to IPADDR.
-matchremoteport PORTNUMBER Only matches those connections whose remote port is PORTNUMBER.
matchstate STATELIST Only matches those connections whose state is one those specified in STATELIST which should be a list of connection state values. See get_tcp_connections for a list of valid values.

COPYRIGHT

Copyright © 2004-2006, Ashok P. Nadkarni

Tcl Windows API 2.1.6 Privacy policy