웹사이트 검색

'script' 및 'scriptreplay' 명령을 사용하여 Linux 터미널 세션을 기록하고 재생하는 방법


이 가이드에서는 Linux에서 scriptscriptreplay 명령을 사용하여 주어진 시간 동안 터미널에 인쇄된 명령과 출력을 기록하는 데 도움이 되는 방법을 살펴보겠습니다. 세션.

History 명령은 명령의 출력을 저장하지는 않지만 사용자가 이전에 사용한 명령을 저장하는 데 도움이 되는 훌륭한 명령줄 유틸리티입니다.

놓치지 마세요: Showterm.io - Linux 터미널 녹음 도구

놓치지 마세요: Linux용 최고의 데스크톱 화면 녹화기 8가지

따라서 script 명령은 터미널에 인쇄된 모든 내용을 log_file에 기록하는 데 도움이 되는 강력한 기능을 제공하는 데 유용합니다. 그러면 나중에 log_file의 기록에서 명령 출력을 보려는 경우 이 파일을 참조할 수 있습니다.

타이밍 정보를 사용하여 scriptreplay 명령을 사용하여 기록한 명령을 재생할 수도 있습니다.

스크립트 명령을 사용하여 Linux 터미널을 기록하는 방법

script 명령은 사용자가 이름을 지정할 수 있는 로그 파일에 터미널 활동을 저장합니다. 사용자가 이름을 제공하지 않으면 기본 파일 이름인 typescript가 사용됩니다. .

스크립트 명령의 기본 구문

script [options] - -timing=timing_file log_filename

Linux 터미널 기록을 시작하려면 script를 입력하고 표시된 대로 로그 파일 이름을 추가하세요.


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, uptimecal 명령을 실행하여 기록해 보겠습니다.


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.logtime.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 파일에는 두 개의 열이 있습니다. 첫 번째 열은 마지막 표시 이후 경과된 시간을 표시하고 두 번째 열은 이번에 표시된 문자 수를 표시합니다.

스크립트 명령줄 유틸리티 사용에 대한 추가 옵션과 도움말을 보려면 매뉴얼 페이지와 –help를 사용하세요.

scriptreplay를 사용하여 타이밍 정보를 사용하여 스크립트 재생

scriptreplay 명령은 script 명령으로 기록된 log_file의 정보를 재생하는 데 도움이 됩니다.

타이밍 정보는 script 명령과 함께 사용되는 -timing=file 옵션으로 정의되며 이 경우 filefile.txt입니다. 스크립트 명령과 함께 사용되었습니다.

스크립트 명령어와 함께 사용한 log_file을 지정해야 한다는 점을 기억하세요.

이제 다음과 같이 실행했던 마지막 세 가지 명령 w, uptimecal을 재생해 보겠습니다.


tecmint@tecmint ~ $ scriptreplay --timing=time.txt script.log

타이밍 정보를 사용하여 log_file이 재생되면 기록된 명령이 실행되고 해당 출력이 기록되는 동안 원래 출력이 표시되는 것과 동시에 표시됩니다.

요약

scriptscriptreplay라는 두 명령은 사용하기 쉽고 동일한 명령 배치를 여러 번 실행해야 할 때 많은 도움이 됩니다. 시스템과의 상호 작용을 위한 명령줄 인터페이스만 있는 서버를 관리하는 데 많은 도움이 됩니다. 이 가이드가 도움이 되었기를 바라며, 사용 중에 추가할 사항이나 문제가 있는 경우 주저하지 말고 댓글을 게시해 주세요.