วิธีบันทึกและเล่นซ้ำเซสชัน Linux Terminal โดยใช้คำสั่ง 'script' และ 'scriptreplay'
ในคู่มือนี้ เราจะดูวิธีใช้คำสั่ง สคริปต์ และ scriptreplay ใน Linux ที่สามารถช่วยคุณบันทึกคำสั่งและเอาต์พุตที่พิมพ์บนเทอร์มินัลของคุณในช่วงเวลาที่กำหนด การประชุม.
คำสั่ง history เป็นยูทิลิตีบรรทัดคำสั่งที่ยอดเยี่ยมที่ช่วยให้ผู้ใช้สามารถจัดเก็บคำสั่งก่อนหน้านี้ที่ใช้ แม้ว่าจะไม่เก็บผลลัพธ์ของคำสั่งก็ตาม
อย่าพลาด: Showterm.io – เครื่องมือบันทึกเทอร์มินัล Linux
อย่าพลาด: 8 โปรแกรมบันทึกหน้าจอเดสก์ท็อปที่ดีที่สุดสำหรับ Linux
ดังนั้นคำสั่ง script จึงมีประโยชน์ในการมอบฟังก์ชันการทำงานที่มีประสิทธิภาพซึ่งช่วยให้คุณบันทึกทุกสิ่งที่พิมพ์บนเทอร์มินัลของคุณลงใน log_file จากนั้น คุณสามารถอ้างถึงไฟล์นี้ในภายหลังได้ในกรณีที่คุณต้องการดูผลลัพธ์ของคำสั่งในประวัติศาสตร์จาก log_file
คุณยังสามารถเล่นซ้ำคำสั่งที่คุณบันทึกไว้โดยใช้คำสั่ง scriptreplay ได้โดยใช้ข้อมูลจังหวะ
วิธีบันทึก Linux Terminal โดยใช้คำสั่งสคริปต์
คำสั่ง สคริปต์ เก็บกิจกรรมเทอร์มินัลไว้ในไฟล์บันทึกที่ผู้ใช้สามารถตั้งชื่อได้ เมื่อผู้ใช้ไม่ได้ระบุชื่อ ชื่อไฟล์เริ่มต้นคือ 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' สำหรับคำสั่งที่บันทึกไว้ทั้งหมด ในขณะที่คุณดูบันทึก คุณจะพบว่าสคริปต์ยังเก็บการป้อนบรรทัดและ backspaces ด้วย
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 เพื่อต่อท้ายไฟล์บันทึกหรือ typescript โดยคงเนื้อหาก่อนหน้าไว้
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 มีสองคอลัมน์ คอลัมน์แรกแสดงระยะเวลาที่ผ่านไปนับตั้งแต่การแสดงครั้งล่าสุด และคอลัมน์ที่สองแสดงจำนวนอักขระที่ถูกแสดงในครั้งนี้
ใช้ man page และ –help เพื่อค้นหาตัวเลือกเพิ่มเติมและความช่วยเหลือในการใช้ยูทิลิตีบรรทัดคำสั่งของสคริปต์
การใช้ scriptreplay เพื่อเล่นสคริปต์ซ้ำโดยใช้ข้อมูลจังหวะ
คำสั่ง scriptreplay ช่วยในการเล่นซ้ำข้อมูลใน log_file ของคุณที่บันทึกโดยคำสั่ง script
ข้อมูลการกำหนดเวลาถูกกำหนดโดยตัวเลือก -timing=file ที่ใช้กับคำสั่ง script และ file ในกรณีนี้คือ file.txt ที่ใช้กับคำสั่งสคริปต์
โปรดจำไว้ว่าคุณต้องระบุ log_file ที่คุณใช้กับคำสั่งสคริปต์
ตอนนี้ให้เราเล่นซ้ำสามคำสั่งล่าสุด w, uptime และ cal ที่เราเรียกใช้ดังต่อไปนี้
tecmint@tecmint ~ $ scriptreplay --timing=time.txt script.log
เมื่อ log_file เล่นซ้ำโดยใช้ข้อมูลจังหวะเวลา คำสั่งที่บันทึกไว้จะถูกรันและเอาต์พุตจะแสดงพร้อมกันกับเอาต์พุตต้นฉบับที่แสดงขณะกำลังบันทึก
สรุป
คำสั่งทั้งสองนี้ script และ scriptreplay ใช้งานง่ายและช่วยได้มากเมื่อคุณต้องการรันคำสั่งชุดเดียวกันหลายครั้ง ช่วยได้มากในการจัดการเซิร์ฟเวอร์ที่มีอินเทอร์เฟซบรรทัดคำสั่งสำหรับการโต้ตอบกับระบบของคุณเท่านั้น หวังว่าคู่มือนี้จะเป็นประโยชน์ และหากคุณมีอะไรเพิ่มเติมหรือเผชิญกับความท้าทายขณะใช้งาน อย่าลังเลที่จะแสดงความคิดเห็น