Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-21-2012, 08:19 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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)
Reply With Quote
  #2  
Old 02-21-2012, 08:31 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 02-21-2012, 08:40 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Reply With Quote
  #4  
Old 02-21-2012, 08:54 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 02-21-2012, 09:02 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
How do I create a code block on this forum?
Is the beginning
Code:
 and after
?
Reply With Quote
  #6  
Old 02-21-2012, 09:16 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
How do I create the code block? or should I just copy/paste?
Reply With Quote
  #7  
Old 02-21-2012, 09:21 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Reply With Quote
  #8  
Old 02-21-2012, 09:26 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 02-21-2012, 09:29 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Reply With Quote
  #10  
Old 02-21-2012, 10:06 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #11  
Old 02-21-2012, 10:34 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Attached Images
File Type: jpg Forum.JPG (53.2 KB, 906 views)
File Type: jpg Forum2.JPG (19.0 KB, 709 views)
Reply With Quote
  #12  
Old 02-21-2012, 11:25 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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>"
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #13  
Old 02-21-2012, 11:54 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Reply With Quote
  #14  
Old 02-21-2012, 12:08 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
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.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #15  
Old 02-21-2012, 12:15 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
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.
Reply With Quote
Reply

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 11:07 AM.