Brendan Gregg | 6049d3f | 2016-10-16 12:33:50 -0700 | [diff] [blame] | 1 | Demonstrations of ttysnoop, the Linux eBPF/bcc version. |
| 2 | |
| 3 | |
| 4 | ttysnoop watches a tty or pts device, and prints the same output that is |
| 5 | appearing on that device. It can be used to mirror the output from a shell |
| 6 | session, or the system console. |
| 7 | |
| 8 | Let's snoop /dev/pts/2: |
| 9 | |
| 10 | # ./ttysnoop 2 |
| 11 | <screen clears> |
| 12 | date |
| 13 | Sun Oct 16 01:28:47 UTC 2016 |
| 14 | # uname -a |
| 15 | Linux bgregg-xenial-bpf-i-xxx 4.8.0-rc4-virtual #1 SMP Wed Aug 31 22:54:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux |
| 16 | # df -h |
| 17 | Filesystem Size Used Avail Use% Mounted on |
| 18 | udev 7.4G 0 7.4G 0% /dev |
| 19 | tmpfs 1.5G 89M 1.4G 6% /run |
| 20 | /dev/xvda1 7.8G 4.5G 3.3G 59% / |
| 21 | tmpfs 7.4G 0 7.4G 0% /dev/shm |
| 22 | tmpfs 5.0M 0 5.0M 0% /run/lock |
| 23 | tmpfs 7.4G 0 7.4G 0% /sys/fs/cgroup |
| 24 | tmpfs 250M 0 250M 0% /run/shm |
| 25 | /dev/md0 160G 20G 141G 13% /mnt |
| 26 | tmpfs 1.5G 0 1.5G 0% /run/user/0 |
| 27 | # ^C |
| 28 | |
| 29 | What we're seeing is another shell session. The first line was "date" without |
| 30 | the shell prompt ("#") because we began tracing after the prompt was printed. |
| 31 | The other commands appeared, keystroke by keystroke, as the user was typing |
| 32 | them. Spooky! |
| 33 | |
| 34 | Remember to Ctrl-C to exit ttysnoop. |
| 35 | |
| 36 | |
| 37 | To figure out which pts device number to use, you can check your own with "ps" |
| 38 | and other's with "w". For example: |
| 39 | |
| 40 | # ps -p $$ |
| 41 | PID TTY TIME CMD |
| 42 | 9605 pts/1 00:00:00 bash |
| 43 | # w |
| 44 | 01:26:37 up 9 days, 35 min, 2 users, load average: 0.22, 0.22, 0.15 |
| 45 | USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT |
| 46 | root pts/1 100.127.65.241 00:39 2.00s 0.33s 0.33s -bash |
| 47 | root pts/2 100.127.65.241 00:40 16.00s 1.06s 1.06s -bash |
| 48 | |
| 49 | So I'm pts/1, and there's another session that's pts/2. |
| 50 | |
| 51 | |
| 52 | This can also snoop tty devices using their full path. Eg, snooping the system |
| 53 | console: |
| 54 | |
| 55 | # ./ttysnoop /dev/console |
| 56 | Oct 16 01:32:06 bgregg-xenial-bpf-i-xxx kernel: [780087.407428] bash (9888): drop_caches: 1 |
| 57 | Oct 16 01:32:38 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied |
| 58 | Oct 16 01:33:32 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied |
| 59 | Oct 16 01:34:26 bgregg-xenial-bpf-i-xxx snmpd[2708]: Cannot statfs /sys/kernel/debug/tracing: Permission denied |
| 60 | ^C |
| 61 | |
| 62 | Neat! |
| 63 | |
| 64 | |
| 65 | USAGE: |
| 66 | |
| 67 | # ./ttysnoop.py -h |
| 68 | usage: ttysnoop.py [-h] [-C] device |
| 69 | |
| 70 | Snoop output from a pts or tty device, eg, a shell |
| 71 | |
| 72 | positional arguments: |
| 73 | device path to a tty device (eg, /dev/tty0) or pts number |
| 74 | |
| 75 | optional arguments: |
| 76 | -h, --help show this help message and exit |
| 77 | -C, --noclear don't clear the screen |
| 78 | |
| 79 | examples: |
| 80 | ./ttysnoop /dev/pts/2 # snoop output from /dev/pts/2 |
| 81 | ./ttysnoop 2 # snoop output from /dev/pts/2 (shortcut) |
| 82 | ./ttysnoop /dev/console # snoop output from the system console |
| 83 | ./ttysnoop /dev/tty0 # snoop output from /dev/tty0 |