「script」および「scriptreplay」コマンドを使用して Linux ターミナル セッションを記録および再生する方法
このガイドでは、Linux でスクリプトとスクリプトリプレイコマンドを使用する方法を説明します。これらのコマンドは、特定の実行中に端末に出力されるコマンドとその出力を記録するのに役立ちます。セッション。
History コマンドは、コマンドの出力を保存しませんが、ユーザーが以前に使用したコマンドを保存するのに役立つ優れたコマンド ライン ユーティリティです。
お見逃しなく: Showterm.io – Linux ターミナル記録ツール
お見逃しなく: Linux 用のベスト デスクトップ スクリーン レコーダー 8 選
したがって、スクリプト コマンドは、端末に出力されるすべてのものをログファイルに記録するのに役立つ強力な機能を提供するのに役立ちます。後でログファイルの履歴にあるコマンドの出力を表示したい場合に、このファイルを参照できます。
また、scriptreplay コマンドを使用して記録したコマンドを、タイミング情報を使用して再生することもできます。
スクリプトコマンドを使用して Linux ターミナルを記録する方法
script コマンドは、ユーザーが名前を付けることができるログ ファイルに端末のアクティビティを保存します。ユーザーが名前を指定しない場合は、デフォルトのファイル名である typescript が使用されます。 。
スクリプトコマンドの基本構文
script [options] - -timing=timing_file log_filename
Linux ターミナルの記録を開始するには、 図のようにスクリプトを入力し、 ログのファイル名を追加します。
tecmint@tecmint ~ $ script history_log.txt
Script started, file is history_log.txt
スクリプトを停止するには、「exit」と入力して[Enter]を押します。
tecmint@tecmint ~ $ exit
Script done, file is history_log.txt
スクリプトが指定されたログ ファイルに書き込めない場合は、エラーが表示されます。
たとえば、以下の出力では、 ファイルtypescript の権限により、ユーザーまたはグループ以外のファイルの読み取り、書き込み、実行が許可されていません。ログ ファイル名を指定せずにスクリプト コマンドを実行すると、デフォルト ファイルであるtypescriptに書き込もうとするため、エラーが表示されます。
tecmint@tecmint ~ $ ls -l typescript
--------- 1 ubuntu ubuntu 144 Sep 15 00:00 typescript
tecmint@tecmint ~ $ script
script: open failed: typescript: Permission denied
Terminated
スクリプトコマンドの使用例
以下の例では、ログ ファイルに script.log という名前を付けていますが、ファイルに別の名前を付けることもできます。
tecmint@tecmint ~ $ script script.log
次に、スクリプトが端末上で実行されたコマンドを記録できるようにするために、いくつかのコマンドを実行してみます。
tecmint@tecmint ~ $ cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
tecmint@tecmint ~ $ w
14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager
tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log
tecmint@tecmint ~ $ uptime
14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62
tecmint@tecmint ~ $ whoami
tecmint
tecmint@tecmint ~ $ echo 'using script'
using script
tecmint@tecmint ~ $ exit
exit
Script done, file is script.log
次に、記録されたすべてのコマンドのログ ファイル「script.log」を表示してみます。ログを表示すると、スクリプトには改行とバックスペースも保存されていることがわかります。
tecmint@tecmint ~ $ vi script.log
サンプル出力
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M
September 2015 ^M
Su Mo Tu We Th Fr Sa ^M
1 2 3 4 5 ^M
6 7 8 9 10 11 12 ^M
13 14 15 ^[[7m16^[[27m 17 18 19 ^M
20 21 22 23 24 25 26 ^M
27 28 29 30 ^M
^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M
14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT^M
tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager^M
tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M
14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whoami^M
tecmint^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m echo ''^Hu'^Hs'^Hi'^Hn'^Hg'^H '^Hs'^Hc'^Hr'^Hi'^Hp'^Ht'^H^M
using script^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m exit^M
exit^M
Script done on Wednesday 16 September 2015 02:49:59 PM IST
~
-a オプションを使用すると、以前の内容を保持したままログ ファイルまたはタイプスクリプトを追加できます。
tecmint@tecmint ~ $ script -a script.log
Script started, file is script.log
tecmint@tecmint ~ $ date
Wed Sep 16 14:59:36 IST 2015
tecmint@tecmint ~ $ pwd
/home/tecmint
tecmint@tecmint ~ $ whereis script
script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz
tecmint@tecmint ~ $ whatis script
script (1) - make typescript of terminal session
スクリプトの内容を表示し、 -a オプションを使用して追加した後にログを記録します。
tecmint@tecmint ~ $ vi script.log
サンプル出力
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m date^M
Wed Sep 16 14:59:36 IST 2015^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m pwd^M
/home/tecmint^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whre^H^[[K^H^[[Kereis script^M
script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whatis script^M
script (1) - make typescript of terminal session^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m vi s^H^[[K^H^[[K^H^[[K^H^[[Kexit^M
exit^M
対話型シェル セッション以外の単一コマンドの結果をログに記録するには、-c オプションを使用します。
tecmint@tecmint ~ $ script -c 'hostname' script.log
Script started, file is script.log
linux-console.net
Script done, file is script.log
スクリプトをサイレント モードで実行したい場合は、-q オプションを使用できます。スクリプトが開始中または終了中であることを示すメッセージは表示されません。
tecmint@tecmint ~ $ script -c 'who' -q script.log
tecmint tty8 2015-09-16 10:45 (:0)
tecmint pts/5 2015-09-16 13:42 (:0)
タイミング情報を標準エラーまたはファイルに設定するには、–timing オプションを使用します。タイミング情報は、log_file に保存されている出力を再表示する場合に役立ちます。
スクリプトを開始し、次のコマンド w、uptime、および cal を実行して記録します。
tecmint@tecmint ~ $ script --timing=time.txt script.log
Script started, file is script.log
tecmint@tecmint ~ $ w
15:09:31 up 4:26, 2 users, load average: 1.38, 1.39, 1.47
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint tty8 :0 10:45 4:26m 8:15 0.38s x-session-manager
tecmint pts/5 :0 13:42 3.00s 0.09s 0.00s script --timing=time.txt script.log
tecmint@tecmint ~ $ uptime
15:09:36 up 4:26, 2 users, load average: 1.43, 1.40, 1.48
tecmint@tecmint ~ $ cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
上記のタイミング コマンドの script.log ファイルと time.txt ファイルを表示できます。
tecmint@tecmint ~ $ vi script.log
サンプル出力
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M
15:12:05 up 4:28, 2 users, load average: 1.31, 1.37, 1.45^M
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT^M
tecmint tty8 :0 10:45 4:28m 8:20 0.38s x-session-manager^M
tecmint pts/5 :0 13:42 5.00s 0.09s 0.00s script --timing=time.txt script.log^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M
15:12:07 up 4:28, 2 users, load average: 1.29, 1.36, 1.45^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M
September 2015 ^M
Su Mo Tu We Th Fr Sa ^M
1 2 3 4 5 ^M
6 7 8 9 10 11 12 ^M
13 14 15 ^[[7m16^[[27m 17 18 19 ^M
20 21 22 23 24 25 26 ^M
27 28 29 30 ^M
^M
次に、time.txt ファイルを表示します。
tecmint@tecmint ~ $ vi time.txt
サンプル出力
0.259669 306
0.037680 829
0.000006 2
0.000002 100
0.000002 2
0.000002 102
0.000019 202
0.000004 2
0.000002 102
0.000015 100
0.000002 2
0.000003 2
0.000002 99
0.000011 2
0.000003 82
...
time.txt ファイルには 2 つの列があり、最初の列は前回の表示からの経過時間を示し、2 番目の列は今回表示された文字数を示します。
マニュアル ページと -ヘルプ を使用して、スクリプト コマンド ライン ユーティリティの使用に関するその他のオプションとヘルプを探します。
scriptreplay を使用して、タイミング情報を使用してスクリプトを再生する
scriptreplay コマンドは、script コマンドによって記録された log_file 内の情報を再生するのに役立ちます。
タイミング情報は、script コマンドで使用される -timing=file オプションによって定義されます。この場合の file は file.txt です。スクリプト コマンドで使用されるものです。
スクリプト コマンドで使用したログファイルを指定する必要があることに注意してください。
ここで、次のように実行した最後の 3 つのコマンド w、uptime、および cal を再実行してみましょう。
tecmint@tecmint ~ $ scriptreplay --timing=time.txt script.log
タイミング情報を使用してログファイルが再生されると、記録されたコマンドが実行され、記録中に元の出力が表示されるのと同時にその出力が表示されます。
まとめ
これら 2 つのコマンド、script と scriptreplay は使いやすく、同じコマンドのバッチを複数回実行する必要がある場合に非常に役立ちます。これらは、システムと対話するためのコマンドライン インターフェイスしか持たないサーバーの管理に非常に役立ちます。このガイドがお役に立てば幸いです。何か追加したいことや使用中に問題に直面した場合は、遠慮なくコメントを投稿してください。