歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> SecureCRT中Python腳本編寫學習指南

SecureCRT中Python腳本編寫學習指南

日期:2017/3/1 9:19:54   编辑:Linux編程

引言

在測試網絡設備中,通常使用腳本對設備端進行配置和測試以及維護;對於PE設備的測試維護人員來說使用較多是SecureCRT工具;SecureCRT支持VB、JavaScript、Python等多種腳本語言,為了實現腳本在CRT中更加豐富穩定地執行,掌握CRT的常用函數是非常有用的。接下來的時間我將對SecureCRT腳本編寫的常用函數展開學習應用。

內容

(1)使用python語言實現SecureCRT中的Dialog功能

# $language = "Python"
# $interface = "1.0"

#crt.Dialog.FileOpenDialog([title,[buttonLabel,[defaultFilename,[filter]]]])
#彈出一個對話框,用於選擇單個文件;如果選擇了具體文件則返回該文件的絕對路徑,如果選擇了彈窗的“取消”,則返回空。
filePath = crt.Dialog.FileOpenDialog("please open a file","open","a.log","(*.log)|*.log")
#filePath = crt.Dialog.FileOpenDialog("","","a.log","")
#crt.Dialog.MessageBox(message, [title, [icon|buttons]]) 警告、按鈕類型彈出一個消息框,可以定義按鈕,使用按鈕和文本消息來實現和用戶的簡單對話;
crt.Dialog.MessageBox(filePath,"",64|0)
crt.Dialog.MessageBox("會話已斷開","session",64|2)
crt.Dialog.MessageBox("確認是否退出","session",32|1)
crt.Dialog.MessageBox("確認是否退出","session",32|3)
crt.Dialog.MessageBox("是否繼續安裝","session",32|4)
crt.Dialog.MessageBox("此會話已打開","session",48|5)
crt.Dialog.MessageBox("無法連接此窗口","session",16|6)

#crt.Dialog.Prompt(message [, title [,default [,isPassword ]]])
#彈出一個輸入框,用戶可以填寫文字,比如填寫文件名,填寫路徑,填寫IP地址等,運行結果如果點擊'ok',返回輸入的字符串,否則返回""
password = crt.Dialog.Prompt("password","session","admin",False)
crt.Dialog.MessageBox(password,"password",64|0)
password = crt.Dialog.Prompt("password","session","",True)
crt.Dialog.MessageBox(password,"password",64|0)

(2)使用python語言實現SecureCRT中的Screen功能

# $language = "Python"
# $interface = "1.0"

# CurrentColumn返回當前光標的列坐標。
curCol = crt.Screen.CurrentColumn
crt.Dialog.MessageBox(str(curCol))

# CurrentRow返回當前光標的行坐標。
curRow = crt.Screen.CurrentRow
crt.Dialog.MessageBox(str(curRow))

# Columns 返回當前屏幕的最大列寬
cols = crt.Screen.Columns
crt.Dialog.MessageBox(str(cols))

# Rows 返回當前屏幕的最大行寬
rows = crt.Screen.Rows
crt.Dialog.MessageBox(str(rows))

#IgnoreEscape 定義當使用WaitForString、WaitForStrings和ReadString這三個方法時是否獲取Escape字符(特殊字符如回車)默認是會獲取的
crt.Screen.IgnoreEscape = False
crt.Dialog.MessageBox(crt.Screen.ReadString(["\03"],5)) #獲取ctrl+c

crt.Screen.IgnoreEscape = True
crt.Dialog.MessageBox(crt.Screen.ReadString(["\03"],2)) #不獲取ctrl+c

# MatchIndex 定義當使用WaitForStrings和ReadString這三個方法時會根據參數的位置 獲取返回值,從1開始計算,如果沒有一個匹配則返回0.
outPut = crt.Screen.ReadString(["error","warning","#"],10)
index = crt.Screen.MatchIndex
if (index == 0):
crt.Dialog.MessageBox("Timed out!")
elif (index == 1):
crt.Dialog.MessageBox("Found 'error'")
elif (index == 2):
crt.Dialog.MessageBox("Found 'warning'")
elif (index == 3):
crt.Dialog.MessageBox("Found '#'")

# Synchronous 設置屏幕的同步屬性。若設置為false,則在腳本中使用WaitForString、WaitForStrings、ReadString函數時可能存在丟失一部分數據的現象,設置為true後可能會存在屏幕卡頓的情況,默認為false
crt.Screen.Synchronous = True
crt.Screen.Send("\r\n")
crt.Screen.ReadString("#")
crt.Screen.Send("\r\n")
crt.Screen.WaitForString("#")
crt.Screen.Send("\r\n")
crt.Screen.WaitForStrings(["#",">"])
crt.Screen.Send("conf t\r\n")

# 方法
# Clear()清屏功能
# crt.Screen.Clear()

# get()按照坐標取出一個矩形框內的屏幕上的字符(即從某行某列開始到其它行其它列),不包含字符串中的回車換行符,所以這個多用於獲取無格式的光標處字符串或某小段特定區域字符串。
out = crt.Screen.Get(row1, col1, row2, col2)
crt.Dialog.MessageBox(out)

# get2()解釋按照坐標取出一個矩形框內的屏幕上的字符(即從某行某列開始到其它行其它列),包含字符串中的回車換行符,所以這個多用於獲取大段帶格式的字符串。
crt.Screen.Get2(row1, col1, row2, col2)

# IgnoreCase 使用全局參數設置控制在使用WaitForString、WaitForStrings和ReadString這三個函數時是否對大小寫敏感,默認為false大小寫字符串都會檢查,設置為true時則不會檢測大小寫。
crt.Screen.IgnoreCase = True
crt.Screen.Send("show memory\r\n")
crt.Screen.WaitForString("more")
crt.Screen.Send("\r\n")
crt.Screen.WaitForStrings("more","#")
crt.Screen.Send("\r\n")
crt.Screen.ReadString("more","#")

# Send() 向遠端設備或者屏幕發送字符串,當向屏幕發送字符串時需要指定第二個參數為Ture
crt.Screen.Send("show version\r\n")
crt.Screen.Send("\r\nhello,world!\r\n",True)
crt.Screen.IgnoreCase = True
while (crt.Screen.WaitForString("more",10)):
crt.Screen.Send("\r\n")

# SendKeys()向當前窗口發送按鍵,包含組合按鍵,比如可以發送類似"CTRL+ALT+C"等這樣的組合鍵,這樣寫即可:crt.screen.sendkeys("^%c");這個功能需要語言本身支持,目前只有VBS和JS腳本可以使用。

# SendSpecial()可以發送特殊控制碼,這個控制碼是Crt內置的功能,具體可以包含的有Menu、Telnet、VT functions功能列表中提供的所有功能,
crt.Screen.SendSpecial("vT_HOLD_SCREEN")

# WaitForCursor()等待光標移動,當移動時返回值為true,當有超時時間參數且超時時返回false,否則會一直等待光標移動。利用這個功能可以用來判斷一個命令的輸出是否結束,
crt.Screen.WaitForCursor(5)
crt.Screen.Send("\r\nhello,world!\r\n",True)
if ( crt.Screen.WaitForCursor(5)):
crt.Screen.Send("show version\r\n")

# WaitForKey()檢測有鍵盤按鍵時返回true,當有超時時間參數且超時時返回false,否則會一直等待按鍵
if (crt.Screen.WaitForKey(5)):
crt.Screen.Send("show version\r\n")

# WaitForString()一般用於發送命令後等待某字符串
# crt.Screen.WaitForString(string,[timeout],[bCaseInsensitive])
crt.Screen.WaitForString("#",10)

# WaitForStrings()與WaitForString是同樣的功能,可以等待多個字符串
outPut = crt.Screen.WaitForStrings(["error","warning","#"],10)
index = crt.Screen.MatchIndex
if (index == 0):
crt.Dialog.MessageBox("Timed out!")
elif (index == 1):
crt.Dialog.MessageBox("Found 'error'")
elif (index == 2):
crt.Dialog.MessageBox("Found 'warning'")
elif (index == 3):
crt.Dialog.MessageBox("Found '#'")

# ReadString()與WaitForStrings功能類似,都是等待某幾個字符出現,不同的是它還會讀取字符串之前出現的所有字符。
crt.Screen.ReadString([string1,string2..],[timeout],[bCaseInsensitive])
1、string,必選參數,等待的字符串,最少有一個,可以是特殊字符比如:\r\n;
2、timeout,可選參數,超時時間,當檢測不到對應字符串時會返回false,沒有此參數時會一直等待;
3、bCaseInsensitive,可選參數,大小寫不敏感,默認值是false,表示將檢測字符串的大小寫,當為true時不檢測大小寫。

下面關於Python的文章您也可能喜歡,不妨看看:

Linux下Python的安裝以及注意事項 http://www.linuxidc.com/Linux/2015-11/124861.htm

Ubuntu 14.04 下安裝使用Python rq模塊 http://www.linuxidc.com/Linux/2015-08/122441.htm

無需操作系統直接運行 Python 代碼 http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved