#!/usr/bin/ksh # # connections - print inbound TCP connections by process. # Written in DTrace (Solaris 10 3/05). # # This displays the PID and command name of the processes accepting # connections, along with the source IP address and destination port number. # # $Id: connections 3 2007-08-01 10:50:08Z brendan $ # # USAGE: connections [-htvZ] # # -t # print timestamps, us # -v # print timestamps, string # -Z # print zonename # eg, # connections -v # snoop connections with times # # FIELDS: # ZONE_ID zone ID # PID process ID for the server # IP_SOURCE source IP of the client # PORT server port # TIME timestamp, us # TIMESTR timestamp, string # ZONE zonename # # SEE ALSO: snoop 'tcp[13:1] = 0x02' # snoop new connections # # Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. # # COPYRIGHT: Copyright (c) 2005 Brendan Gregg. # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License, Version 1.0 only # (the "License"). You may not use this file except in compliance # with the License. # # You can obtain a copy of the license at Docs/cddl1.txt # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # CDDL HEADER END # # TODO: IPv6 # # 10-Apr-2004 Brendan Gregg Created this. # 23-May-2004 " " Fixed issues on SPARC. # 08-May-2005 " " Updated for newer Solaris 10. # 17-Jun-2005 " " Rewrote, changed probes, wrapped in sh. # 04-Dec-2005 " " Changed tcp_accept_finish -> sotpi_accept # 20-Apr-2006 " " Fixed SS_TCP_FAST_ACCEPT bug in build 31+. # 20-Apr-2006 " " Last update. # # 26-Jun-2014 Melvin Gong Rewrote, using tcp provider ### Default variables opt_time=0; opt_timestr=0; opt_zone=0 ### Process options while getopts htvZ name do case $name in t) opt_time=1 ;; v) opt_timestr=1 ;; Z) opt_zone=1 ;; h|?) cat <<-END >&2 USAGE: connections [-htvZ] -t # print timestamps, us -v # print timestamps, string -Z # print zonename eg, connections -v # snoop connections with times END exit 1 esac done /usr/sbin/dtrace -C -s <( print -r ' #pragma D option quiet #pragma D option switchrate=10hz inline int OPT_time = '$opt_time'; inline int OPT_timestr = '$opt_timestr'; inline int OPT_zone = '$opt_zone'; /* * Print header */ dtrace:::BEGIN { /* print optional headers */ OPT_time ? printf("%-14s ", "TIME") : 1; OPT_timestr ? printf("%-20s ", "TIMESTR") : 1; OPT_zone ? printf("%-10s ", "ZONE") : 1; printf("%8s %7s %15s %7s\n", "ZONE_ID", "PID", "IP_SOURCE", "PORT"); } /* * Probe new TCP accepted connections */ tcp:::accept-established { /* print optional fields */ OPT_time ? printf("%-14d ", timestamp/1000) : 1; OPT_timestr ? printf("%-20Y ", walltimestamp) : 1; OPT_zone ? printf("%-10s ", zonename) : 1; printf("%8d %7d %15s %7d\n", args[1]->cs_zoneid, args[1]->cs_pid, args[2]->ip_saddr, args[4]->tcp_dport); } ')