博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SecureCRT Python
阅读量:6256 次
发布时间:2019-06-22

本文共 3024 字,大约阅读时间需要 10 分钟。

  hot3.png

学完python后需要学以致用,但平时还真没有这个环境,也不搞自动化,但语言这玩意必需要用,就拿secureCRT来练手吧,也算是和工作擦点边
对话框加按扭
CONFIRM_REPLACE = """\                                       #为数不多的button判断Do you want to replace the contents of "%(FILE)s" with the selected text? 	Yes = overwrite	No = append	Cancel = end script"""message = CONFIRM_REPLACE % { 'FILE' : "filename" }		prompt = "Replace contents"		buttons = BUTTON_YESNOCANCEL		result = crt.Dialog.MessageBox(message, prompt, buttons)if result == IDYES:    fileMode = "w"elif result == IDNO:    fileMode = "a" 

crt.Dialog.Prompt(                                  #超级有用的在用户这获取数据的方式		"Save selected text to File:",		"SecureCRT - Save selected text to file",		"MyFile.txt") 

获取log文件名在SecureCRT

tab = crt.GetScriptTab()tab.Session.LogFileName                   #如果在CRT中设置了log文件名,这个函数可以把它读取出来#可以直接取到log的文件名
获取系统当前的前缀名
SCRIPT_TAB = crt.GetScriptTab()rowIndex = SCRIPT_TAB.Screen.CurrentRowcolIndex = SCRIPT_TAB.Screen.CurrentColumn - 1prompt = SCRIPT_TAB.Screen.Get(rowIndex, 0, rowIndex, colIndex)#获取当前行,当前列的数据,即该服务器的前缀,与waitfor配套prompt = prompt.strip()

获取打印值

def CaptureOutputOfCommand(command, prompt):	if not crt.Session.Connected:		return "[ERROR: Not Connected.]"		# First, send the command to the remote.	SCRIPT_TAB.Screen.Send(command + '\r')            #输入命令 cmmand 然后回车		# Second, wait for the carriage return to be echoed by the remote device. 	# This allows us to capture only the output of the command, not the line	# on which the command was issued (which would include the prompt + cmd).	# If you want to capture the command that was issued, simply comment out	# the following line of code.	SCRIPT_TAB.Screen.WaitForString('\r'#等待命令输完后,服务器给的回应,这样可以不是获取行,而是获取输出		# Now that the command has been sent, use Screen.ReadString to capture	# all the data that is received up to the point at which the shell	# prompt appears (the captured data does not include the shell prompt).	return SCRIPT_TAB.Screen.ReadString(prompt)     #获取直到prompt出现时的所有数据

屏幕取词

filename = os.path.join(os.path.expanduser('~'), 'output.txt')      #~/output.txtfp = open(filename, "wb+")screenrow = crt.Screen.CurrentRow - 1    readline = crt.Screen.Get(screenrow, 1, screenrow, 20)  #把当前行的1~20个字符取出来 fp.write(readline + os.linesep)                         #把取出的那一行加行尾符写到文件中

写到excel

import os import csv                                               #excel modulefileobj = open(filename, 'wb')worksheet = csv.writer(fileobj)                                                                worksheet.writerow(items[:2]) fileobj.close(

连接

SSH:cmd = "/SSH2 /L %s /PASSWORD %s /C 3DES /M MD5 %s" % (user, passwd, host)crt.Session.Connect(cmd)                                  #ssh -L user password hostTelnet:crt.Session.Connect("/TELNET 127.0.0.1 2034")            # telnet 127.0.0.1:2034crt.Session.Connect("/S \"" + session + "\"") #如果这个session在CRT里存在,无论是Telnet还是SSH都能连

调试工具 CRT中的print

crt.Dialog.MessageBox("Nothing to save!") 

参考:

http://www.vandyke.com/support/securecrt/python_examples.html

转载于:https://my.oschina.net/hding/blog/390158

你可能感兴趣的文章
JavaScript基础避免使用eval()(006)
查看>>
面向对象和面向过程的区别
查看>>
内置函数与匿名函数
查看>>
SSH实现登陆拦截器
查看>>
使用HttpWebRequest出错时获取详细的错误信息
查看>>
sql还原(.mdf文件还原)
查看>>
Mellanox infinoband RDMA SDP
查看>>
Nearest Common Ancestors(LCA)
查看>>
JS/atan2 pow
查看>>
Pythoh网络编程3:创建TCP服务器和客户端
查看>>
angularjs 出现 “Possibly unhandled rejection: cancel ”错误
查看>>
bzoj 2653 middle (主席树+二分)
查看>>
指导别人,弥补自己
查看>>
BZOJ3932: [CQOI2015]任务查询系统
查看>>
和make相关的一些命令
查看>>
Fiddler抓取https设置及其原理
查看>>
常用的一些模板
查看>>
WPF使用Expression Design设计图形
查看>>
Ubuntu 下Qt安装实用教程
查看>>
DNS 协议2
查看>>