#1
|
|||
|
|||
problem log file
Hi there,
I got a little problem. I have a script that logs in to device and gets some info from it. Everything is logged into a file. Problem is when script hits the device to which it cannot log in. Here is the script: #$language = "VBScript" #$interface = "1.0" Sub main Dim ip, line, cnxString, folder, index, Path, fso, fil, objFSO, objLogFile, strCurrentLine 'read file with IP and set path Const DEVICE_FILE_PATH = "c:\direcotry\ips.txt" Path = "c:\direcotry\" Set fso = CreateObject("Scripting.FileSystemObject") Set fil = fso.OpenTextFile(DEVICE_FILE_PATH) Set objFSO = CreateObject("Scripting.FileSystemObject") ' Telneting using IPs from txt file While Not fil.AtEndOfStream line = fil.ReadLine ip = Split(line, ";")(0) cnxnString = "/TELNET " & ip & " 23" crt.Screen.Synchronous = True crt.session.Log(false) 'Connect crt.Session.Connect cnxnString 'create and open log file Set lfile = fso.CreateTextFile(Path & "log.txt") Set lfile = nothing Set objFolder = nothing Set lfile = fso.OpenTextFile(Path & "log.txt", 8, True) 'if any error during connection, will continue with next IP On Error Resume Next 'get required info and check if it is OK crt.Screen.WaitForString ("sername:") crt.Screen.Send "user" & vbCr index = Crt.Screen.WaitForString("CODE:") index = crt.Screen.WaitForString("#") timeout = 3 index = crt.Screen.WaitForString("<<<", timeout) index = crt.Screen.WaitForString("#", timeout) 'read hostname hostname = crt.Screen.Get(crt.Screen.CurrentRow, 1, crt.Screen.CurrentRow, 50) hostname = Left(hostname, Instr(1, hostname, ">", 1) - 1) lfile.WriteLine(hostname) 'write actual date and time crt.Screen.Send "sh clock" + vbCr crt.Screen.WaitForString "#" file = crt.Screen.Get(crt.Screen.CurrentRow-1, 1,crt.Screen.CurrentRow-1, 150 ) lfile.Write (file) & vbCrLf 'will check if required string is present crt.Screen.Send "sh run | i no ip source" + vbCr crt.Screen.WaitForString "#" lfile.Write ("sh run | i no ip source") & vbCrLf file = crt.Screen.Get(crt.Screen.CurrentRow-1, 1,crt.Screen.CurrentRow-1, 150 ) If Instr(1, file, "sh run", 1) <> 0 Then lfile.Write "..." & vbCrLf Else lfile.Write (file) & vbCrLf End if If Instr(1, file, "no ip source-route", 1) <> 0 Then lfile.Write "NISR OK" & vbCrLf ElseIf Instr(1, file, "no ip source route", 1) <> 0 Then Else lfile.Write "NISR FALSE" & vbCrLf End If crt.Sleep 1000 crt.Screen.Send "sh run | i ervice password" + vbCr crt.Screen.WaitForString "#" lfile.Write ("sh run | i ervice password") & vbCrLf file = crt.Screen.Get(crt.Screen.CurrentRow-1, 1,crt.Screen.CurrentRow-1, 150 ) If Instr(1, file, "sh run", 1) <> 0 Then lfile.Write "..." & vbCrLf Else lfile.Write (file) & vbCrLf End if If Instr(1, file, "service password", 1) <> 0 Then lfile.Write "SPE OK" & vbCrLf Else lfile.Write "SPE FALSE" & vbCrLf End If crt.Sleep 1000 'add line at the end of each check lfile.WriteLine("-------------------------") & vbCrLf 'Close log file 'lfile.Close crt.Screen.Synchronous = true 'Disconnect crt.Session.Disconnect Wend End Sub And here is the extract from log: devicename1# 16:18:28.155 UTC Thu Mar 15 2012 sh run | i no ip source no ip source-route NISR OK sh run | i ervice password service password-encryption SPE OK sh run | i ogging IP logging IP LOG OK ------------------------- Basically it logs in and checks if certain commands are present in configuration. Problem occurs when script hits the device to which it cannot log in (password is not working). What happens is, that each time it hits the nonaccessible device it adds lines to the log which shouldn't be added. Then the log looks like this: devicename1# 16:18:28.155 UTC Thu Mar 15 2012 sh run | i no ip source no ip source-route NISR OK sh run | i ervice password service password-encryption SPE OK sh run | i ogging IP logging IP LOG OK ------------------------- devicename1# logging IP sh run | i no ip source logging IP NISR FALSE sh run | i ervice password logging IP SPE FALSE sh run | i ogging 3.10.203.48 logging IP LOG OK ------------------------- I am not sure what is going on, but it looks like that something got stucked in a buffer. Any ideas where can be a problem? ![]() |
#2
|
||||
|
||||
The script should verify that logon was successful. The following block of your code has a comment that says "check if it is OK" but nothing in the code actually checked to see if logon or anything else was okay. Error checking will help.
Code:
'get required info and check if it is OK crt.Screen.WaitForString ("sername:") crt.Screen.Send "user" & vbCr index = Crt.Screen.WaitForString("CODE:") index = crt.Screen.WaitForString("#") timeout = 3 index = crt.Screen.WaitForString("<<<", timeout) index = crt.Screen.WaitForString("#", timeout) You'll need to go back and look at a SecureCRT log file, probably raw log (raw log captures all data including escape characters), and see what you notice being sent from the server in the failure cases. Hopefully you see something like "logon failed" or "password incorrect", so that you can write logon failure situation handling into the script. Some slightly more specific comments that might help get things back on track. There are two WaitForString statements, back to back. That looks like a possible mistake. Do you really want to wait for "<<<" or "#", or do you actually want to wait for <<< and then # to appear sequentially? Are there any other strings that you would watch for to tell you that authentication failed or succeeded (this is when/why the SecureCRT log file is important). If you would like assistance figuring out the correct strings to wait for, please send a raw log to support@vandyke.com with subject Mike Forum Thread 10351. Please do not post a raw log to the forums as it may contain sensitive data. To create a raw log:
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#3
|
|||
|
|||
Hi Mike,
thank you very much for response. First: Explanation why I have a sequence of #, >>>, # there: Problem is, that the script is logging to different devices with different "welcome screen". E.g. some of them have no banner and when you log in all you will get is devicename#, but some of them have banner which have "#" in it and that is causing a problem with a line where script is trying to read a hostname. That's why I put that sequence there #, >>>, #, because when first # occurs in a banner then there is a set of > (>>>) and then devicename# and with this I do not have problem with capturing of the hostname. I put a timeout there for devices that do not have this sequence, so basically first two (#, >>>) will timeout and then script will continue as it should with other commands. Second: Anyway, I have figured it out. I've just used simple index = crt.Screen.WaitForStrings("SSCODE", "invalid", "ssword") If index = 1 Then "rest of the script" ElseIf index = 2 Then crt.Session.Disconnect ElseIf index = 3 Then crt.Session.Disconnect End If so if the expected string after username is not SSCODE, script will disconnect from the device and move to another device. This has fixed that issue with log file. Maybe one more question. Is there any way how not to put all other strings (invalid, ssword, etc...) to index but something that will say, if there is anything else than SSCODE then disconnect from the device? |
#4
|
||||
|
||||
Quote:
Code:
index = crt.Screen.WaitForStrings("SSCODE", "invalid", "ssword", "Any", "other", "strings", "that", "indicate", "failure") If index = 1 Then "rest of the script" Else crt.Session.Disconnect End If Maybe you want to timeout after 20 seconds: WaitForString("SSCODE", 20) Code:
If crt.screen.WaitForString("SSCODE", 20) <> True Then ' Timed out waiting for SSCODE, assume the worst, disconnect crt.Session.Disconnect Else ' We found SSCODE, carry on... "rest of the script" End If
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
#5
|
|||
|
|||
Hi Mike,
I've used first option as there are only those three cases (sscode, invalid, ssword). Thank you very much for your help. |
#6
|
||||
|
||||
Sounds good - the SecureCRT Scripting Manual has some examples that would also be applicable, if you or others revisit this at some point (WaitForStrings() section around page 37).
__________________
Mike VanDyke Software Technical Support [http://www.vandyke.com/support] |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|