How to use the command ttyplot (with examples)
- Linux
- December 25, 2023
This article will provide examples and explanations of various use cases for the ttyplot
command, a real-time plotting utility for the command-line with data input from stdin
. ttyplot
is a useful tool for visualizing data in real-time, allowing users to monitor and analyze data trends.
Use case 1: Plotting specific values
Code:
{ echo 1 2 3; cat } | ttyplot
Motivation:
This use case demonstrates how to plot specific values using ttyplot
. By providing the values 1
, 2
, and 3
as input, ttyplot
will plot these values on a graph.
Explanation:
{ echo 1 2 3; cat }
- This command group echoes the values1 2 3
and then pipes it tocat
to preventttyplot
from exiting.| ttyplot
- The pipe character (|
) is used to redirect the output of the previous command tottyplot
for visualization.
Example output:
3.5 ++---------+-------+
| + |
3 ++ * +
| |
2.5 ++ |
| |
2 ++ |
| |
1.5 ++ |
| |
1 ++ |
| |
0.5 ++ |
| |
0 ++----+-------+----|
0 2 4
Use case 2: Setting a title and unit
Code:
{ echo 1 2 3; cat } | ttyplot -t title -u unit
Motivation:
In some cases, it may be important to provide a specific title and unit for the data being plotted. This use case demonstrates how to set a custom title and unit while plotting data with ttyplot
.
Explanation:
-t title
- This argument specifies the title of the plot. In this example, the title is set to “title”.-u unit
- This argument specifies the unit of the plotted values. In this example, the unit is set to “unit”.
Example output:
title ++---------+-------+
| + |
| * +
unit -- | |
| |
| |
| |
| |
+ |
+ |
| |
+ |
| |
0 --+-----+-------+----|
0 2 4
Use case 3: Continuously plotting random values
Code:
{ while true; do echo $RANDOM; sleep 1; done } | ttyplot
Motivation:
This use case demonstrates how to continuously plot random values using ttyplot
. By using a while loop, random values are generated and streamed to ttyplot
for real-time visualization.
Explanation:
while true; do echo $RANDOM; sleep 1; done
- This while loop generates a random value using the$RANDOM
variable and echoes it. A sleep of 1 second is added to introduce a delay between each value.| ttyplot
- The pipe character (|
) is used to redirect the output of the previous command tottyplot
for visualization.
Example output:
32000 ++---------+-------+
| + |
+ * +
| |
| |
12000 + |
| |
| |
| |
2000 + |
| |
| |
-8000 + |
| |
+ |
-18000 ++ |
| |
-28000 ++ |
| |
-38000 ++ |
| |
-48000 ++ |
| |
0 --+-----+-------+----|
0 2 4
Use case 4: Parsing and visualizing ping
output
Code:
ping 8.8.8.8 | sed -u 's/^.*time=//g; s/ ms//g' | ttyplot -t "ping to 8.8.8.8" -u ms
Motivation:
In network troubleshooting scenarios, it can be useful to visualize the response times of ping
commands. This use case demonstrates how to parse the output of a ping
command and visualize the response times using ttyplot
.
Explanation:
ping 8.8.8.8
- This command sends ICMP echo requests to the IP address8.8.8.8
.sed -u 's/^.*time=//g; s/ ms//g'
- Thissed
command is used to extract only the response times from the output ofping
. It removes the text beforetime=
and removes thems
suffix.-t "ping to 8.8.8.8"
- This argument specifies the title of the plot as “ping to 8.8.8.8”.-u ms
- This argument specifies the unit of the plotted values as milliseconds.
Example output:
ping to 8.8.8.8 ++---------+-------+
| + |
200 + * +
| |
| |
40 + |
| |
+ |
| |
| |
10 + |
| |
| |
| |
| |
+ |
| |
+ |
+ |
| |
+ |
0 --+-----+-------+----|
0 2 4
Conclusion:
The ttyplot
command is a powerful tool for real-time data visualization on the command-line. Whether plotting specific values, setting custom titles and units, continuously plotting random values, or parsing and visualizing ping
output, ttyplot
provides an intuitive and flexible way to analyze data trends.