VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Log into Cisco router, perform cmds and log to a file (https://forums.vandyke.com/showthread.php?t=10303)

stuartX 02-21-2012 08:19 AM

Log into Cisco router, perform cmds and log to a file
 
I'm having difficulty in creating a script to log into a cisco router, prompted with username and password, perform commands and log to a file.
I saw an example. I'm able to log into the router with username and password.
Is there an example I can use to refer to?
Thanks for help (in advance)

rtb 02-21-2012 08:31 AM

Hi stuartX,

Thanks for the question. We have examples as well as information explaining how the examples work in the scripting manual that we published to www.vandyke.com. You can find the downloadable PDF file at the following location:
http://www.vandyke.com/support/tips/...ing/index.html
Chapter 3 discusses connecting to remote machines, chapters 4 and 5 discuss interacting with the remote machine (performing commands) and chapter 7 discusses logging data to files.

Does this information help as you write your script?

Did you have any specific questions about the example you found?

If so, would you post them along with the example you are referencing?

stuartX 02-21-2012 08:40 AM

Yes, I have questions. I searched and found an example from this link:
http://forums.vandyke.com/showthread.php?p=14178

I copied the example and changed the path for the Devices.txt file and of course modified the text file contents (x.x.x.x;Telnet).
When I run the script, I get pass username and password. But it stops and nothing else happens.

rtb 02-21-2012 08:54 AM

Hi stuartX,

Thanks for the update. Would you post the script you are using in your tests in a code block?

Would you also cancel the script (Script / Cancel) when it is doing nothing, and tell me on what line the script is stopped?

stuartX 02-21-2012 09:02 AM

How do I create a code block on this forum?
Is the beginning
Code:

and after
?

stuartX 02-21-2012 09:16 AM

How do I create the code block? or should I just copy/paste?

stuartX 02-21-2012 09:21 AM

Here is the code from the link modified

Code:

# $language = "VBScript"
# $interface = "1.0"

Sub Main
       
        Dim fso
        Dim fil
        Dim Connected
        Dim UID
        Dim DeviceType
        Dim Password
        Dim Router
        Dim protocol
        Dim LogFileLocation
 
'******************************
'******************************
'  LOCATION OF LOG FILE
 
 LogFileLocation = "S:\Riggs\"
 
'******************************
'******************************
 
        Const DEVICE_FILE_PATH = "S:\Riggs\Devices.txt"
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)
        UID = GetUID()
        crt.Session.Disconnect
 
        While Not fil.AtEndOfStream
                line = fil.ReadLine
               
                Router = Split(line, ";")(0)
                protocol = Split(line, ";")(1)
                Password = GetPassword()
               
                Select Case protocol
                        Case "Telnet"
                                cnxnString = "/TELNET " & Router & " 23"
                        Case "SSH2"
                                cnxnString = "/SSH2 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
                        Case "SSH1"
                                cnxnString = "/SSH1 /L " & UID & " /PASSWORD " & password & " /C 3DES /M MD5 " & Router
                End Select       
               
                crt.Session.Connect cnxnString
                crt.Screen.Synchronous = True
                If protocol = "Telnet" then
                        crt.Screen.WaitForString "Username:"
                        crt.Screen.Send UID & chr(13)
                        crt.Screen.WaitForString "Password:"
                        crt.Screen.Send Password & chr(13)
                End If
                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                crt.Screen.Send "sh ver " + chr(124) + " i IOS" + chr(124) + "Sw" + chr(13)
                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                screenrow = crt.screen.CurrentRow
                screenrow = screenrow - 1
                tempDevice = crt.Screen.Get(screenrow, 1, screenrow, 80 )
 
                DeviceType = "CatOS"
 
                If instr(tempDevice,"IOS") > 0 then
                        DeviceType = "IOS"
                End If
                       
                Dim logfile
                logfile = (LogFileLocation + Router + "-confg.txt")
                crt.Session.LogFileName = logfile
                crt.Session.Log True
                       
                        If DeviceType = "IOS" then
                                crt.Screen.Send "term len 0" & chr(13)
                                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                                crt.Screen.Send "sh run" & chr(13)
                                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                        End If
                        If DeviceType = "CatOS" then
                                crt.Screen.Send "set length 0" & chr(13)
                                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                                crt.Screen.Send "sh conf" & chr(13)
                                crt.Screen.WaitForStrings Router + "#",  Router + "> (enable)"
                        End If
 
                        crt.Session.Log False
                        crt.Session.Disconnect
       
        Wend
                        fil.Close
End Sub
 
'**************************************************************************************
Function GetPassword()
 Dim TempPassword
 Do
  TempPassword = crt.Dialog.Prompt("Enter Password:","Password","",True)
  If TempPassword = "" Then
  result = crt.Dialog.MessageBox("Password cannot be blank. Retry?","No Password",4)
    If result = 7 then
    crt.quit
    End If
  End If
 Loop Until TempPassword<>""
 GetPassword=TempPassword
 Exit Function
End Function
 
'**************************************************************************************
Function GetUID()
  dim TempUID
  Do
  TempUID = crt.Dialog.Prompt("Enter UserID:","UserID","",False)
  If TempUID = "" Then
    result = crt.Dialog.MessageBox("UserID cannot be blank. Retry?","No Userid",4)
    If result = 7 then
    crt.quit
    End If
  End If
  Loop Until TempUID<>""
  GetUID=TempUID
  Exit Function
End Function

I really don't care about "IOS" or "CATOS".
It stops at line #56.

rtb 02-21-2012 09:26 AM

Hi stuartX,

Thanks for posting the code in a code block.

Here is line 56:
Code:

                crt.Screen.Send "sh ver " + chr(124) + " i IOS" + chr(124) + "Sw" + chr(13)
Are you saying that you never see "sh ver...", or you see that, but nothing happens after you see that sent to the remote device?

Is line 56 in your script editor the same as the line I pasted above?

I think that you may have better luck using the examples in the scripting manual that I referenced rather than trying to repurpose this example to your needs.

stuartX 02-21-2012 09:29 AM

Yes, your line 56 is the same as mine.
After the script logs in successfully, it just sits there at the prompt. no other commands are issued.

rtb 02-21-2012 10:06 AM

Hi stuartX,

I am not able to reproduce the issue you are seeing.

It seems odd that when you cancel the script using Script / Cancel it displays line 56 in the dialog. I have never seen a script hang on a send line. I have seen scripts hang on wait lines.

Would you post a screenshot of the dialog you see when cancelling the script?

stuartX 02-21-2012 10:34 AM

2 Attachment(s)
Below is what I see after I execute the script (Forum.jpg)
When I perform a cancel (Forum2.jpg)

Like I mention after execution, the prompt just sits there. I can hit Enter and the router responds with the next prompt, so its not hung.

rtb 02-21-2012 11:25 AM

Hi stuartX,

Thanks for the screenshots. Based on the screenshot, it appears as though the problem is a result of not waiting for the correct thing on line 55.

It is important to note that when interacting with a remote device, you must wait for strings that exactly match what you expect to see on the remote. This is discussed in detail in Chapter 4 of the scripting manual.

In your script, it looks like you might be connecting to a device using an IP address, and based on the way the code is written in this example, you are waiting for the IP address and a hash or the IP address and an angle bracket followed by (enable). In the screenshot, even though you redacted it, it seems like the prompt on the device has a device name followed by an angle bracket.

If you change line 55 (and subsequent wait lines) to be something similar to the following, do you get farther along in the script?
Code:

crt.Screen.WaitForString "THIS_IS_MY_DEVICE1>"

stuartX 02-21-2012 11:54 AM

I apologize for the delay.
I copied and pasted the router name prompt into line 55 and I get the same thing.

could you guide me on the following? (To less complicate matters)
-take out the prompt(dialog box requesting information) and include the username & password in the code.

It seems like it stops after username & password entry.

rtb 02-21-2012 12:08 PM

Hi stuartX,

To remove the functionality that prompts you for data, you can remove from line #92 to the end of the script. On line #28, you will need to define a username. On line #36, you will need to define a password.

I would also recommend replacing all "+" symbols with "&" symbols.

I must say, however, that I don't think that this will help resolve the issue. You may want to try adding a timeout to the wait lines. For example:
Code:

crt.Screen.WaitForString "THIS_IS_MY_DEVICE1>", 1
Once you have made the changes to the code, would you post your new code (please be sure to redact any sensitive data like usernames and passwords)?

Due to the sensitive nature of the data involved in this project, you may want to send the modified script and a screenshot to support@vandyke.com with a subject of Attn: Todd - Forum thread #10303.

stuartX 02-21-2012 12:15 PM

Yes, I'll try to modify a few things to make it simpler.
I'll post the results once I have sucess.
thanks for your help.


All times are GMT -6. The time now is 11:00 AM.