站长资讯网
最全最丰富的资讯网站

SecureCRT脚本环境类(python)

重构看的七七八八了,感觉应该练练手,自己打包一个SecureCRT的环境类。不过暂时没让我想到如何将这个类放在文件中,然后每次写脚本的时候import进来就好了,等有空再想想怎么解决。

所以现在还是只能复制粘贴后用。

其实再写个设备类,然后从设备类中调用crt环境类,就可以达到设备相关的东西和环境分开的效果。以后如果没有crt了,只要把crt环境类换掉就行了。

没有打包log功能,这个类是我用来直接在内存中处理回显字符串用的,所以不需要log。

# $language = "python"

# $interface = "1.0"

class CrtEnv():

@staticmethod

def promptBox(szMessage, szTitle):

return crt.Dialog.Prompt(szMessage, szTitle, "", True)

@staticmethod

def inputBox(szMessage, szTitle):

return crt.Dialog.Prompt(szMessage, szTitle, "", False)

@staticmethod

def messageBox(szMessage, szTitle):

crt.Dialog.MessageBox(szMessage, szTitle)

def __init__(self):

self.__oTab = None

self.__szPrompt = ""

self.__szHostname = ""

def login(self, szHostname, szUsername, szPassword):

self.__szHostname = szHostname

if self.__ssh(szUsername,szPassword):

return True

if self.__telnet(szUsername,szPassword):

return True

return False

def logout(self):

self.__oTab.Session.Disconnect()

def send(self, szCMD):

self.__checkEnv()

self.__send(szCMD)

self.__oTab.Screen.WaitForString(szCMD)

return str(self.__getResult())

def changePrompt(self, szCMD):

self.__send(szCMD)

self.__oTab.Screen.WaitForString(szCMD)

self.__oTab.Screen.WaitForString("Some String will not echo.", 1)

self.__updatePrompt()

def getTabName(self):

if self.__isTabEmpty():

self.__getCurrentTab()

return str(self.__oTab.Caption)

def __ssh(self, szUsername, szPassword):

szConnectString = "/SSH2 /L "%s" /PASSWORD "%s" %s" %

(szUsername, szPassword, self.__szHostname)

if self.__connect(szConnectString) == False:

return False

self.__oTab.Screen.Synchronous = True

self.__waitDeviceTabInit()

return True

def __telnet(self, szUsername, szPassword):

szConnectString = "/Telnet %s" % (self.__szHostname)

if self.__connect(szConnectString) == False:

return False

self.__oTab.Screen.Synchronous = True

self.__oTab.Screen.WaitForString("ame")

crt.Sleep(200)

self.__send(szUsername)

self.__oTab.Screen.WaitForString("assword")

crt.Sleep(200)

self.__send(szPassword)

self.__waitDeviceTabInit()

return True

def __connect(self, szConnectString):

try:

self.__oTab = crt.Session.ConnectInTab(szConnectString)

return True

except:

return False

def __waitDeviceTabInit(self, szString = "#"):

crt.Sleep(300)

self.__oTab.Screen.WaitForString(szString)

def __send(self, szCMD):

szCMD = szCMD + 'r'

self.__oTab.Screen.Send(szCMD)

def __checkEnv(self):

if self.__isTabEmpty():

self.__getCurrentTab()

self.__oTab.Screen.Synchronous = True

if self.__isPromptEmpty():

self.__updatePrompt()

def __isPromptEmpty(self):

if self.__szPrompt == "":

return True

return False

def __isTabEmpty(self):

if self.__oTab == None:

return True

return False

def __getCurrentTab(self):

self.__oTab = crt.GetActiveTab()

def __getResult(self):

return self.__oTab.Screen.ReadString(self.__szPrompt)

def __updatePrompt(self):

crt.Sleep(300)

rowIndex = self.__oTab.Screen.CurrentRow

colIndex = self.__oTab.Screen.CurrentColumn – 1

self.__szPrompt = self.__oTab.Screen.Get(rowIndex, 0, rowIndex, colIndex)

self.__szPrompt = str(self.__szPrompt.strip())

def main():

# 测试用。获取当前tab标签,并直接向设备发送命令

test1 = CrtEnv()

test1.send("terminal length 0")

CrtEnv.messageBox(test1.send("sh int status"),"run")

CrtEnv.messageBox(test1.getTabName(), "getTabName")

test1.changePrompt("conf t")

CrtEnv.messageBox(test1.send("do sh ip int br"),"run")

test1.changePrompt("end")

CrtEnv.messageBox(test1.send("sh int status"),"run")

# 新登录一台设备

test2 = CrtEnv()

test2.login("192.168.1.1","qw er","wqerqwer")

test1.logout()

test2.logout()

main()

赞(0)
分享到: 更多 (0)
网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号