Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-15-2012, 11:50 AM
mikit mikit is offline
Registered User
 
Join Date: Mar 2012
Posts: 38
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?
Reply With Quote
  #2  
Old 03-15-2012, 12:43 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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)
There's a timeout of 3 in the code block above. The timeout lets the script proceed past the line regardless of whether or not the string is found. There's no attempt in the code to determine whether either string was detected. Nothing assures at this point that logon succeeded. The script continues, and writes some things, and that's what you're reporting as a problem, but I believe that's long after the real problem. The wheels fell off the vehicle long before the trail of unwanted garbage being left behind in the problem log file. I think the problem is that the script isn't handling the situation of logon failure.

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:
  1. Before connecting with the session, select "Raw Log Session" from SecureCRT's "File" menu.
  2. In the "Select Log File" dialog, choose a folder and filename in which you would like the log text to be stored and press the "Save" button. At this point, if you open the "File" menu, you will notice that the "Raw Log Session" menu item now has a check-mark next to it; this indicates that raw logging is activated.
  3. Now connect to the remote machine and perform the actions which cause the reported problem to occur.
  4. Once the problem occurs, disconnect the session and select "Raw Log Session" from the "File" menu to turn off the raw logging feature.
  5. Browse to the location of the raw log file and add the file as an attachment to your e-mail reply.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 03-20-2012, 11:41 AM
mikit mikit is offline
Registered User
 
Join Date: Mar 2012
Posts: 38
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?
Reply With Quote
  #4  
Old 03-20-2012, 12:47 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Quote:
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?
If you have a reasonably finite set of strings that would indicate failure then you could simplify the code to something like this:
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
In this case, it sounds like "SSCODE" is the only string that really matters. If you have an unreasonable number of possible strings to indicate failure and not good way to limit this, or you have only one string that matters, you could use WaitForString with a timeout.
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
Do either of these ideas help?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #5  
Old 03-21-2012, 02:14 PM
mikit mikit is offline
Registered User
 
Join Date: Mar 2012
Posts: 38
Hi Mike,

I've used first option as there are only those three cases (sscode, invalid, ssword).


Thank you very much for your help.
Reply With Quote
  #6  
Old 03-21-2012, 02:33 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
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]
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:45 AM.