Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #16  
Old 02-21-2012, 01:28 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the update. I still think that you would be better off using an example from and the information contained in the scripting manual.

I already mentioned the chapters that I think would be most beneficial as you write your script.

There is a solution/example that may also be of assistance. It is the solution in chapter 7.2 called Read Data from Separate Files: Hosts, Commands. You could also modify this example to log data using the information in chapter 7.1

Feel free to post any additional questions you might have as you work on your script.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 02-21-2012 at 01:31 PM.
Reply With Quote
  #17  
Old 02-22-2012, 07:08 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Ok,
I'm back...
I revamped the script by viewing the link you sent me (thanks). It works as far as logging into the device and performing the three commands I have (after I get it fix there may be more commands). And it logs into the file.
My "Test-router-1.txt" contains three entries for three routers....shown below:
Nexus1;x.x.x.1;Telnet 'router#1
Nexus2;x.x.x.2;Telnet 'router#2
Nexus3;x.x.x.3;Telnet 'router#3

My problem is that the script does the following:
login into router#1 skips commands
goes to router#2 skips commands
goes to router#3 shows first command only, cuts to second command then stops.

I think that I don't have enough delay time in for it to perform and complete the commands.

Below is the entire script. I have a few lines commented out to get me to this point.



Below is my full code:
Code:
# $language = "VBScript"
# $interface = "1.0"

Sub Main
	Const username = "userx" ' Username to use for login
	Const password = "passx" ' Password for corresponding user
	Const loginPass = "admin" ' Password to use for password only - no TACACSnew-model
	Const DEVICE_FILE_PATH = "s:\riggs\Test-router-1.txt"
	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")
	Dim fil
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)
	Dim ip
	Dim name
	Dim protocol
	Dim line
	Dim cnxnString
	'File Reads @ stored location
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		name = Split(line, ";")(0)
		ip = Split(line, ";")(1)
		protocol = Split(line, ";")(2)	
	'Protocol Selection
		Select Case protocol
			Case "Telnet"
				cnxnString = "/TELNET " & ip & " 23"
			Case "SSH2"
				cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
			Case "SSH1"
				cnxnString = "/SSH1 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
		End Select	
		' Connect 
		crt.Screen.Synchronous = True
		'Error
		on error resume next
		crt.Session.Connect cnxnString
		If protocol = "Telnet" Then
			Dim index
			index = crt.Screen.WaitForStrings("login:", "Password:")
					
			If index = 1 Then 
				crt.Screen.Send username & vbCr'crt.Screen.Send username & vbCr	
				crt.Screen.WaitForString "Password:"
 				crt.Screen.Send password & vbCr
 	 			crt.Screen.WaitForString "#"
		'Take about page pauses
		crt.Screen.Send "term length 0"& vbCr	
		'Send CPU command
		crt.Screen.Send "show proc cpu | inc CPU"& vbCr
		'Wait for unique character
		crt.Screen.WaitForString "#"
 		'send show version information						 				 
		crt.Screen.Send "show version"& vbCr 
			Else If index = 2 then
				crt.Screen.Send vbCr
				'crt.Screen.WaitForString "Password:"
 				crt.Screen.Send vbCr
 							
 		crt.Screen.WaitForString "#"
		crt.Screen.Send "show users"& vbCr 

		'crt.Session.Log False
			End If
		End If
'Start to collect information in log-file
		'crt.Screen.WaitForString "linux$", 1
 			Dim logfile
  logfile = "S:\Riggs\" & ip &"-Nexus-config" & ".log"
  crt.Session.LogFileName = logfile
  crt.Session.Log True
		' Enable
		'crt.Screen.WaitForString ">"
		'crt.Screen.Send "show inventory"& vbCr 
		'crt.Screen.WaitForString ">"
		'crt.Screen.Send "show snmp status"& vbCr 
    'crt.Screen.WaitForString ">"
    'crt.Screen.Send "show snmp accexx"& vbCr 
		' Save
		'Close log file
		crt.Session.Log False
		crt.Screen.Synchronous = False
		'Close out session
		crt.Session.Disconnect 			

		end if
wend
	fil.Close 
End Sub
Reply With Quote
  #18  
Old 02-22-2012, 09:55 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for posting your script that works, but doesn't. I have found a few issues that may be the cause of the issue you are currently seeing.

Here is a list:
  • The Synchronous property should always be set to True after the connection is established. This should be done between lines 37 and 38 rather than on line 34.
  • By enabling error handling on line 36, but not terminating it, you are likely masking errors. It is a best practice to limit the scope of the error handling to the specific line or lines of code that may be problematic. Here is an example of how you could use error handling on a connection attempt:
    Code:
    ' Start error handling
    On Error Resume Next
        ' Connect to device
        crt.Session.Connect cnxnString
        ' Capture errors in variables for future use if necessary
        strErr = Err.Description
        nError = Err.Number
    ' Stop error handling
    On Error Goto 0
  • As a general rule, you should never send a command without waiting on the remote to see if it is ready for the next command. For devices that don't present a prompt, it may be sufficient to wait for the command to be echoed from the remote. In your case, you should wait for the prompt after line 48. Additionally, it is possible that a hash is not a unique character. If you are still seeing odd behavior after ensuring that each Send() command is followed by a WaitForString() command, then you may want to confirm that the output of the command being issued does not contain your unique character.
  • There are a few problems in your Telnet If...Then...Else statement. It would be best to handle authentication prior to sending any commands. Here is an example:
    Code:
    index = crt.Screen.WaitForStrings("login:", "Password:")
    If index = 1 Then 
        crt.Screen.Send strUsername & vbCr
        crt.Screen.WaitForString "Password:"
        crt.Screen.Send strPassword & vbCr
    
    Else
        crt.Screen.WaitForString "Password:"
        crt.Screen.Send strPassword & vbCr
    End If
    crt.Screen.WaitForString "#"
    ' Enable logging
    strLogFile = "S:\Riggs\" & strIP & "-Nexus-config.log"
    crt.Session.LogFileName = strLogFile
    crt.Session.Log True
    ' Send commands
    crt.Screen.Send "term length 0" & vbCr
    crt.Screen.WaitForString "#"
    .
    .
    .
  • It is also advisable to enabling logging prior to sending any commands as in the example above.
After making the changes listed above, are you able to successfully run your script?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730

Last edited by rtb; 02-22-2012 at 09:57 AM.
Reply With Quote
  #19  
Old 02-22-2012, 01:40 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Mr. Todd,
It works!! Thanks for you help. But I can not make the recommended "IF, Else" changes. But I did make the error exception change. Below is the working code. I tried to clean it up as much as possible. I may have gotten some indentions wrong.
Thank you Sir, much appreciated.

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

Sub Main
	Const username = "userX" ' Username to use for login
	Const password = "passX" ' Password for corresponding user
	'Const enablePass = "admin" ' Password to use for enable password  -  TACACSnew-model
	Const DEVICE_FILE_PATH = "s:\riggs\Test-router-1.txt"
	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")
	Dim fil
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)
	Dim ip
	Dim name
	Dim protocol
	Dim line
	Dim cnxnString
	'File Reads @ stored location
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		name = Split(line, ";")(0)
		ip = Split(line, ";")(1)
		protocol = Split(line, ";")(2)	
	'Protocol Selection
		Select Case protocol
			Case "Telnet"
				cnxnString = "/TELNET " & ip & " 23"
			Case "SSH2"
				cnxnString = "/SSH2 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
			Case "SSH1"
				cnxnString = "/SSH1 /L " & username & " /PASSWORD " & password & " /C 3DES /M MD5 " & ip
		End Select	
		' Connect 
		crt.Screen.Synchronous = True
		'Error Exception Information
		on error resume next
		crt.Session.Connect cnxnString
		strErr = Err.Description
    nError = Err.Number
On Error Goto 0
		If protocol = "Telnet" Then
			Dim index
			index = crt.Screen.WaitForStrings("login:", "Password:")
			If index = 1 Then 
				crt.Screen.Send username & vbCr'crt.Screen.Send username & vbCr	
				crt.Screen.WaitForString "Password:"
 				crt.Screen.Send password & vbCr
 	 	 		crt.Screen.WaitForString "#"		
'Start to collect information in log-file		
Dim logfile
    logfile = "S:\Riggs\" & ip &"-Nexus-config" & ".txt"
    crt.Session.LogFileName = logfile
    crt.Session.Log True
		'Take about page pauses
		crt.Screen.Send "term length 0"& vbCr	
		crt.Screen.WaitForString "linux$", 3
		'Send CPU command
		crt.Screen.Send "show proc cpu | inc CPU"& vbCr
		crt.Screen.WaitForString "linux$", 1
		crt.Screen.WaitForString "#"
 		'send show version information						 				 
		crt.Screen.Send "show version"& vbCr 
		crt.Screen.WaitForString "linux$", 3
			Else If index = 2 then
				crt.Screen.Send vbCr
				'crt.Screen.WaitForString "Password:"
 				crt.Screen.Send vbCr
 		crt.Screen.WaitForString "#"
			End If
		End If
		crt.Screen.WaitForString "linux$", 1
		'Close log file
		crt.Session.Log False
		crt.Screen.Synchronous = False
		'Close out session
		crt.Session.Disconnect 			
		end if
wend
	fil.Close 
End Sub
Reply With Quote
  #20  
Old 02-22-2012, 02:52 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the update. I am glad to hear that you have the script working. I do still see a couple of potential problem areas. They are associated with how you have the script performing authentication.

If you get to the point that you see problems, or you want to revisit trying to get the authentication code removed from the command code, please post the error and the current iteration of your script. I am happy to help.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #21  
Old 02-23-2012, 06:24 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Thanks Todd. I appreciate all your help. This has been a marathon (for me) to get to the working point.
By the way, I would like to streamline the authentication as you mention. But when I changed the IF Else statements, I got errors. So, I would welcome your expertise.
Also, I did find the option to append to a file with the same name with a script writing to the same file. The second "true" turns on append. That is done by the following line command:

Code:
    crt.Session.Log True, true
In addition, if someone does'nt want to go the append route. the timestamp command is performed by the following modication:
Code:
logfile = "S:\Riggs\" & HosName &"%H-%Y-%M-%D--%h-%m-%s.%t.txt"
So, I hope these notes/post will help others trying to do the same function.
Reply With Quote
  #22  
Old 02-23-2012, 02:08 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Thanks for posting the additional information stuartX.

I have gone through the script that you posted, and made some changes to it. Here it is:
Code:
Sub Main
    strUsername = "user" 
    strPassword = "password" 
    strPrompt = "#"
    Set fso = CreateObject("Scripting.FileSystemObject")
    strDeviceList = "C:\Switches\devices.txt"
    Set fil = fso.OpenTextFile(strDeviceList)
    ' Start loop to iterate over all devices defined in file
    While Not fil.AtEndOfStream
        ' Process a line at a time
        vLine = Split(fil.ReadLine, ";")
        strHostname = vLine(0)
        strIP = vLine(1)
        strProtocol = vLine(2)
        ' Generate a connection string
        Select Case strProtocol
            Case "Telnet"
                cnxnString = "/TELNET " & strIP & " 23"
                ' Don't wait for connection to complete before moving on because
                ' we are going to handle authentication after connecting.                
                bWait = False
            Case "SSH2"
                cnxnString = "/SSH2 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP
                ' Wait for connection to complete before moving on.
                bWait = True
            Case "SSH1"
                cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP
                ' Wait for connection to complete before moving on.
                bWait = True
            End Select	
        'Handle possible errors when connecting
        On Error Resume Next
            crt.Session.Connect cnxnString, bWait
            strErr = Err.Description
            nError = Err.Number
        On Error Goto 0
        
        ' Enable Synchronous which is necessary for not missing data from remote
        crt.Screen.Synchronous = True
        
        ' Handle Telnet authentication
        If strProtocol = "Telnet" Then
            nIndex = crt.Screen.WaitForStrings("login:", "Password:")
            
            ' Handle authentication starting at username
            If nIndex = 1 Then 
                crt.Screen.Send strUsername & vbcr
                crt.Screen.WaitForString "Password:"
                crt.Screen.Send strPassword & vbcr
                
            ' Handle authentication starting at password
            Else
                crt.Screen.Send strPassword & vbcr
            End If
        End If
        ' Confirm that remote is ready
        crt.Screen.WaitForString strPrompt
        ' Start logging to new file
        strLogFile = "C:\Switches\" & strHostname & "-%Y-%M-%D--%h-%m-%s.%t.txt"
        crt.Session.LogFileName = strLogFile
        crt.Session.Log True
        ' Turn off paging
        crt.Screen.Send "term length 0" & vbcr	
        crt.Screen.WaitForString strPrompt
        ' Send commands
        crt.Screen.Send "show proc cpu | inc CPU" & vbcr
        crt.Screen.WaitForString strPrompt
        crt.Screen.Send "show version" & vbcr 
        crt.Screen.WaitForString strPrompt
        ' Stop logging
        crt.Session.Log False
        ' Disconnect from device
        If crt.Session.Connected Then
            crt.Session.Disconnect
        End If
    Wend
    ' Close device file
    fil.Close 
End Sub
Without having a Cisco Nexus device against which to test, I can't say that it will work, but I would expect this script to work.

If you decide to try it, and it fails, let me know what error and line number you see. If the script is hanging, then I would need to know on what line the script cancels.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #23  
Old 02-24-2012, 06:34 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Todd,
I loaded the script and ran. It hung, and when I hit Cancel, it reported error script cancelled @ line 46.
Reply With Quote
  #24  
Old 02-24-2012, 07:59 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the information. What do you see in the SecureCRT terminal when the script is hanging?

Would you be able to post or send a screenshot, or copy the data out of the terminal and post or send it?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #25  
Old 02-24-2012, 09:01 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
well, when I started the script, the CRT window was blank. it did not prompt or log into a device at all. There was nothing in the window. When I cancel the script, thats when it came up with the line 46. But it did not initiate a session.
Reply With Quote
  #26  
Old 02-24-2012, 10:12 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
the screeshot is attached
Attached Images
File Type: jpg Error-line46.JPG (19.2 KB, 510 views)
Reply With Quote
  #27  
Old 02-24-2012, 10:17 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the information. If you are establishing a Telnet connection, then it may be necessary to send a carriage return to get the authentication process started.

What happens if you modify the following code:
Code:
        If strProtocol = "Telnet" Then
            nIndex = crt.Screen.WaitForStrings("login:", "Password:")
to be
Code:
        If strProtocol = "Telnet" Then
            crt.Screen.Send vbcr
            nIndex = crt.Screen.WaitForStrings("login:", "Password:")
If that doesn't work, would you run the script, cancel it when it hangs, enable File / Trace Options and then run the script again and send me a copy of the data that appears in the terminal window?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #28  
Old 02-24-2012, 10:51 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
That just it, there is nothing that pops up on the screen (lol).
When I start the script, before (with working/non-efficient script) I would get the login prompt. But that doesn't come up, the window is blank when the script starts. When I turned on Trace, it does not copy anything to the window. I attached the error dialog box (line 46).when I cancelled.
Attached Images
File Type: jpg Error-line46-2.JPG (21.8 KB, 508 views)
Reply With Quote
  #29  
Old 02-24-2012, 01:56 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

It seems like we need to know what SecureCRT is trying to use for the connection. Would you make the following modification and let me know what the message box says (this is only if you have the time, of course)?
Code:
        crt.Dialog.MessageBox "Connect string:" & vbcrlf & vbtab & cnxcString &_
            vbcrlf & "Should I wait:" & vbcrlf & vbtab & bWait
        On Error Resume Next
            crt.Session.Connect cnxnString, bWait
            strErr = Err.Description
            nError = Err.Number
        On Error Goto 0
As a note, this is a way to troubleshoot issues. You have indicated that nothing is appearing in the terminal window. This means to me that there is something wrong with the connection string or there is something wrong with the remote device responding. I can pop up the connection string in a message box to see if it is legitimate.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #30  
Old 02-27-2012, 04:59 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I'm sorry for the delay, had to deal with a network issue on Friday.
I inserted the additional script and I attached errors. The first was the attached ErrorMod-1.jpg and the second was the attached ErrorMod-2.jpg.
Attached Images
File Type: jpg ErrorMod-1.JPG (12.9 KB, 511 views)
File Type: jpg ErrorMod-2.JPG (21.9 KB, 475 views)
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 01:29 PM.