在SecureCRT屏幕上获取数据并存至文件中

CRT屏幕上获取某个具体位置的文本.

可以使用Screen 对象的Get() or Get2()方法去获取屏幕上的数据.

 

1 Get() 函数包含四个参数.文本第一个字符所在的行号,列号.最后个字符所在的行号与列号.

可以通过选择相应的文本来查看文字所处的行号与列号.

 

以下就可获取相应的值.

nFreeMem = crt.Screen.Get (5,36,5,41)

 

把数据写放到文本中

 

Writing data to a file using the VBScript FileSystemObject  can typically be accomplished using

the following general steps:

 1)  Get a reference to the  FileSystemObject :

 Set  objFSO  =   CreateObject( "Scripting.FileSystemObject" ) 

 2) Use the FileSystemObject  reference to retrieve a reference to a  TextStream object

associated with the text file to which the data is to be written.

 Const ForWriting =  2

Const ForAppending =  8

 ' Create a new file (or overwrite an existing one)

Set  objStream  =  objFSO.OpenTextFile(  _

    "C:\Temp\MyDataFile.txt",  _

    ForWriting,  _

    True)

 ' Append to an existing file (create if it doesn't exist)

Set  objStream  =  objFSO.OpenTextFile(  _

    "C:\Temp\MyDataFile.txt",  _

    ForAppending ,  _

True)

3)  Write the data to the file using the  TextStream object's  Write  or  WriteLine methods. 

The WriteLine method automatically appends EOL characters to the end of the data

supplied to the method.

objStream.Write  "This data is written " 

objStream.Write  "as one single line." 

 Or:

 objStream.WriteLine "This data is written " 

objStream.WriteLine "as two separate lines."

 Note that you can write multiple lines using either method by manually inserting CRLF

sequences as needed, for example:

bjStream.Write  "This is line one."  &   vbcrlf   &  _

    "This is line two."  &   vbcrlf   &  _

    "This is line three."   &   vbcrlf   &  _

    "This is line four."  &   vbcrlf 

 

Or:

 

objStream.WriteLine "This is line one."  &   vbcrlf   &  _

    "This is line two."  &   vbcrlf   &  _

    "This is line three."   &   vbcrlf   &  _

    "This is line four."

 

4)  When you have completed writing data to the file, close the file:

 objStream.Close

 

EXAMPLE:获取CPU数据并将其写入到文件中

# $language = "VBScript"

# $interface = "1.0"

Sub  Main ()

For x = 1 To 10000

crt.sleep 300000

usrCpu = crt.Screen.Get(2,7,2,9)

sysCpu = crt.Screen.Get (2,15,2,18)

idleCpu =crt.Screen.get(2,35,2,37)

 

Dim fso, f1

Set fso = CreateObject("Scripting.FileSystemObject")

Set f1 = fso.OpenTextFile("E:\TestFile.txt", 8,True)

f1.writeline("usr:" & usrCpu & " sysCpu:" & sysCpu & " idleCpu" & idleCpu)

f1.close

 

Next

 

end Sub

# $language = "VBScript" # $interface = "1.0" '作者;小怪兽怪瘦 Sub Main Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso,file1,line,str1,params Set fso = CreateObject("Scripting.FileSystemObject") Set file1 = fso.OpenTextFile("D:\backup\list.txt",Forreading, False) crt.window.show 3 crt.Screen.Synchronous = True do while file1.AtEndOfStream True '读取每一行 line = file1.ReadLine '分割IP,用户名,密码 特权密码 型号简写(z m h b) 位置 params = Split (line) ip = params(0) user = params(1) password = params(2) su = params(3) mode = params(4) whereis = params(5) crt.session.LogFileName = "D:\backup\log\"&ip&"_"&whereis&"_20190620.txt" '提供日志,修改后面日期即可 crt.session.Log(true) crt.Session.Connect ( "/SSH2 /L "&user&" /PASSWORD "&password&" "&ip) '建立SSH连接 if mode = "m" then 'm 代表迈普型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "more off" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-mp.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "more on" & chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "exit" & chr(13) crt.Screen.WaitForString ">" crt.Session.Disconnect end if if mode = "c" then 'c 代表思科型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-cisco.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "r" then 'r 代表锐捷型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-rj.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "z" then 'z 代表兴型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-zx.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) if (crt.Screen.WaitForString (" --More--",1)False) then crt.Screen.Send " " & chr(13) else crt.Screen.Send chr(13) end if crt.Screen.waitForString "#" loop 'crt.Screen.Send "show running-config" & chr(13) 'crt.Screen.WaitForString " --More--" 'crt.Screen.WaitForString "#" crt.Session.Disconnect
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值