Thursday, April 11, 2024

การใช้งาน Secure crt เพื่อรัน Python-Script และ VBScript

   สำหรับใครที่ใช้งาน Secure CRT อยู่เป็นประจำและมีงานที่ต้องดึงข้อมูลโดยใช้ command line อยู่เป็นประจำหรือต้องดึงข้อมูลปริมาณมาก การใช้งาน Script เพื่อช่วยงานจะเป็นเครื่องมือที่ช่วยทุ่นแรงเราอย่างมาก การใช้งานก็ไม่ได้ยากเท่าใดนัก ซึ่งในบทความนี้ผมจะยกตัวอย่างที่ผมคิดว่าใช้งานบ่อยๆ ครับ


Script ที่ผมใช้รัน จะ simulate ในโน๊ตบุ๊คโดยใช้ EVE-NG ซิมอุปกรณ์ Huawei และ Nokia Tology ก็จะตามรูปด้านล่างนี้ครับ


บทที่ 1 เริ่มต้นใช้งาน ส่งคำสั่ง รอผลลัพธ์ และการแสดงผล
  • crt.Session.Send(String) คือ ส่ง String
  • crt.Screen.WaitForString(String) คือ รอการแสดงผลจนถึง String ที่กำหนด
  • crt.Screen.ReadString(String) คล้ายกับ WaitForString แต่จะเป็นการอ่านจนถึง String ที่กำหนดแล้ว Return data ออกมา
  • crt.Dialog.MessageBox(String) แสดงหน้าต่าง Dialog พร้อมข้อความ
  • vbcr คือ กด Enter ของภาษา vbs. ส่วน python ใช้ "\n"
ในตัวอย่างนี้เราจะส่งคำสั่ง display ip interface brief ให้ router huawei จากนั้นก็จะรอจนกระทั่ง output จะมีคำว่า <HUAWEI>  จากนั้นก็จะส่งคำสั่ง display ip routing-table เก็บ output ใส่ตัวแปร data จนถึงคำว่า <HUAWEI>  แสดงผล output ในหน้าต่าง Diaglog

Chapter_1.vbs

crt.Screen.Send("display ip interface brief" & vbcr)
crt.Screen.WaitForString("<HUAWEI>")
crt.Screen.Send("display ip routing-table" & vbcr)
data = crt.Screen.ReadString("<HUAWEI>")
crt.Dialog.MessageBox(data)

Chapter_1.py

crt.Screen.Send("display ip interface brief\n")
crt.Screen.WaitForString("<HUAWEI>")
crt.Screen.Send("display ip routing-table\n")
data = crt.Screen.ReadString("<HUAWEI>")
crt.Dialog.MessageBox(data)

Chapter_1 ตัวอย่างผลลัพธ์




บทที่ 2 การดู Help เพื่อดูรายละเอียดการใช้งานและคำอธิบายของฟังก์ชั่น มีตัวอย่างทั้ง VBScript และ Python

คลิกที่ Menu Help -> Help Topics -> ในหน้าต่าง Help เลือก Contents -> Scripting -> Script Objects Reference 

ส่วนมากจะใช้งานในส่วนของ Screen Object กับ Session Object

ตัวอย่าง Help ของ Readstring


บทที่ 3 การเก็บ log

Chapter_3.vbs

Set FSO = CreateObject("Scripting.FileSystemObject")       ' create FileSystemObject
scriptPath = FSO.GetAbsolutePathName(".")                  ' get script path  = D:\Desktop\script
crt.session.log(False)		                           ' release log session
logpath = FSO.BuildPath(scriptPath, "log_huawei.txt" )     ' log path = D:\Desktop\script\log_huawei.txt
crt.Session.LogFileName = logpath                          ' set log filename
crt.Session.Log(True, False)                               ' start log session and overwrite log file
crt.Screen.Send("display ip interface brief" & vbCr)       ' send command and [Enter]
crt.Sleep 1000                                             ' delay 1 second
crt.session.log(False)                                     ' release log session
crt.Dialog.MessageBox(logpath)                             ' message logpath after finish

Chapter_3.py

import os                                           
import time                                               
scriptPath = os.path.dirname(os.path.realpath(__file__))   # get script path  = D:\Desktop\script
crt.Session.Log(False)                                     # release log session
logpath = os.path.join(scriptPath, "log_huawei.txt")       # log path = D:\Desktop\script\log_huawei.txt
crt.Session.LogFileName = logpath                          # set log filename
crt.Session.Log(True, True)                                # start log session and append log file
time.sleep(1)                                              # delay 1 second
crt.Session.Log(False)                                     # release log session
crt.Dialog.MessageBox(logpath)                             # message logpath after finish

บทที่ 4 การใช้ Loop

ส่วนที่ 1 จะเก็บคำสั่งต่างๆ ไว้ในตัวแปรประเภท array และ list ตามลำดับ จากนั้นใช้ For loop เพื่อส่งสั่งไปยัง Router

Chapter 4.1_loop_command.vbs

lines = Array("show isis adjacency process-id 11",_  
	      "show isis adjacency process-id 15",_
	      "show isis adjacency process-id 19" )

For each aline in lines
    crt.Screen.Send(aline & vbCr)
    crt.Screen.WaitForString("#")
    crt.Sleep 1000	
Next

Chapter 4.1_loop_command.py

lines = ["show isis adjacency process-id 11",
	 "show isis adjacency process-id 15",
	 "show isis adjacency process-id 19" ]

for aline in lines:
    crt.Screen.Send(aline + "\n")
    crt.Screen.WaitForString("#")
    time.sleep(1)

ส่วนที่ 2 จะส่งคำสั่ง "display ip interface brief" เก็บ output ไว้ในตัวแปล info จากนั้นแสดงหน้า output ในหน้าต่าง Diaglog เพื่อตรวจสอบ output. ใช้ฟังก์ชั่น split เพื่อแยก output ในแต่ละบรรทัดให้อยู่ในตัวแปลประเภท Array และ List ตามลำดับ ใช้ For loop เพื่อแสดงผลลัพท์

Chapter 4.2_loop_split.vbs

crt.Screen.Send("display ip interface brief"& vbcr)       ' send command
info = crt.Screen.ReadString(">")                         ' read string until found >
crt.Dialog.MessageBox(info)                               ' show all 
lines = Split(info, vbCrLf)                               ' split by new line
For each aline in lines                                   ' loop
    crt.Dialog.MessageBox(aline)                          ' show each line in dialog
Next

Chapter 4.2_loop_split.py

crt.Screen.Send("display ip interface brief\n")           # send command 
info = crt.Screen.ReadString(">")                         # read string until found >
crt.Dialog.MessageBox(info)                               # show all 
lines = info.split("\n")                                  # split by new line
for aline in lines:                                       # loop
    crt.Dialog.MessageBox(aline) 	                  # show each line in dialog

บทที่ 5 Telnet


บทที่ 6 SSH

Chapter 6_ssh.py

hostip = "192.168.44.102"
username = "admin"
password = "admin"
crt.Session.ConnectInTab('/SSH2 /ACCEPTHOSTKEYS /PASSWORD %s %s@%s '%(password, username, hostip), True)

บทที่ 7 VBScript เขียนและอ่านข้อมูลลง Excel

บทที่ 8 Python อ่านและเขียนลงใน csv

บทที่ 9 Python การใช้ Subprocess เพื่อเขียนข้อมูลลง Excel

การใช้งาน Secure crt เพื่อรัน Python-Script และ VBScript

   สำหรับใครที่ใช้งาน Secure CRT อยู่เป็นประจำและมีงานที่ต้องดึงข้อมูลโดยใช้ command line อยู่เป็นประจำหรือต้องดึงข้อมูลปริมาณมาก การใช้งาน ...