7 sshd_exe_path
=/usr
/sbin
/sshd
12 command ps
"$@" || true
17 grep -aPo "\x00$2=[^\x00]+" "/proc/$1/environ" | cut
-f2- -d=
20 find_slave_sshd_pids
()
23 local current_master_sshd_pid
=`cat /var/run/sshd.pid`
25 for pid
in `find /proc -mindepth 1 -maxdepth 1 -regex '/proc/[0-9]+' -printf "%f "`
27 if [ $pid = $current_master_sshd_pid ]
31 if [ "$(readlink /proc/$pid/exe)" = "$sshd_exe_path" ]
33 if [ "$(stat -c %u /proc/$pid)" = 0 ]
35 if grep -q -m1 -E "^PPid:\s*(1|$current_master_sshd_pid)" /proc
/$pid/status
42 if [ ${#pids[@]} -gt 0 ]
50 while getopts hntd opt
52 Usage
="Usage: $0 [-n] [-t] [-d]
53 -n Do not resolve IP addresses
54 -t Display time as unix timestamp
55 -d Replace inherited details with '%' sign"
56 if [ .
"$1" = .
--help ]
72 echo "USER;TTY;FROM;LOGIN@;PID;WHAT"
74 for slave_sshd_pid
in `find_slave_sshd_pids`
76 user_sshd_pid
=`ps o pid= --ppid $slave_sshd_pid`
77 user_sshd_pid
=${user_sshd_pid//[!0-9]/}
78 if [ -z "$user_sshd_pid" ]
83 username
=`stat -c %U "/proc/$user_sshd_pid"`
84 start_time
=`stat -c %Y "/proc/$user_sshd_pid"`
85 if [ "${flag[t]}" != 1 ]
87 start_time
=`date -d@$start_time`
91 for shell_pid
in `ps o pid= --ppid "$user_sshd_pid"`
93 shell_pid
=${shell_pid//[!0-9]/}
94 test -n "$shell_pid" ||
continue
97 #tty=`readlink "/proc/$shell_pid/fd/0"`
98 tty
=`psgetenv "$shell_pid" SSH_TTY`
102 if [ "${flag[d]}" = 1 -a $shells -gt 1 ]
106 env_ssh_client
=`psgetenv "$shell_pid" SSH_CLIENT`
107 hostip
=`cut -f1 -d' ' <<<"$env_ssh_client"`
108 port
=`cut -f2 -d' ' <<<"$env_ssh_client"`
109 if [ "${flag[n]}" != 1 ]
111 if type resolveip
>/dev
/null
2>&1
114 host=`resolveip -s "$hostip"`
121 host=`dig +short -x "$hostip"`
129 exe
=`readlink "/proc/$shell_pid/exe"`
130 comm=`cat "/proc/$shell_pid/comm"`
131 argv
=(`tr "\0" "\n" <"/proc/$shell_pid/cmdline"`)
133 if [ "${argv[0]}" != "$exe" ]
135 exe_if_spoofed
=" ($exe)"
138 echo "$username;$tty;$from;$start_time;$shell_pid;${argv[@]}$exe_if_spoofed"
140 if [ "${flag[d]}" = 1 -a $shells = 1 ]
149 set -- `lastcsv -f /var/run/sshd/wtmp | awk -F';' '$9=="'$slave_sshd_pid'"{print $3, $2; exit}'`
152 port
=${port//[!0-9]/}
154 echo "$username;-;$from;$start_time;$user_sshd_pid;-"
157 } |
column -t -s';' -n