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 05-15-2017, 09:20 PM
Sal Sal is offline
Registered User
 
Join Date: May 2017
Posts: 1
Post Running show commands on a device

Hi

I am new to scripting, what I want to do is just get in to a device and run multiple show commands using script.

Say my router name is abcd

and I want to run show commands like this
Show ver
show inter


It can be VBScript or python, want to learn how those are made and than learn myself

Sal
Reply With Quote
  #2  
Old 05-16-2017, 10:02 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 654
Hi Sal,

To connect to a saved session you could use:

Code:
crt.session.Connect("/s SessionName")
Sending commands would look like:

Code:
crt.Screen.Send("Show Version" & vbcrlf
We do have a more in depth example available here.
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 05-17-2017, 04:50 AM
thera34's Avatar
thera34 thera34 is offline
Registered User
 
Join Date: May 2015
Location: Romania
Posts: 8
Hi Sal,

I have a script like that, modified it a bit and will post it here.
I also added comments to ease up reading it

In order to run it, you need a Linux based server from which you connect to your Cisco device. The script also prompts you for the Cisco IP address, username and password.

Let me know if this helps you.

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

' Send out some commands to a Cisco device
' It will also test via ICMP if the device is reachable or not
Dim g_objTab
Set g_objTab = crt.GetScriptTab
Dim strPrompt																	' Will use this variable to store server's prompt
Dim nResult																		' Will use this variable for temporary storing the screen output
Dim SHtest
Dim devPrompt																	' Variable to store device's prompt
Dim vDevDown																	' Array to determine the outcome of the ping IP
vDevDown = Array ("100% packet loss",_
					"min/avg/max/mdev")

Dim vWaitFors																	' Array to determine if RSA key is known, also to determine if SSH or Telnet is enabled
	vWaitFors = Array("(yes/no)?",_							
					"port 22: Connection refused",_			
					"assword:")
Sub Main
		crt.Screen.Synchronous = True
		If crt.Session.Logging Then 											' Turn off session logging before setting up our script's
		crt.Session.Log False													' logging... if needed
		End If
		strAddress = "127.0.0.1"
		Set re = New RegExp
			' Set the pattern to match only an IPv4 address
		re.Pattern = _
		"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}" & _
		"(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"
		    If strIP = "" Then													' Sub-routine to prompt for SteelHead's IP
        strIP = Trim( _
            GetPlainTextInput(_
                "Specify the IP address to connect to :", _
                "IP address?", _
                strAddress))
        ' Bail if user cancelled
        If strIP = "" Then Exit Sub
		End If
		If re.Test(strIP) <> True Then
		crt.Dialog.MessageBox "Invalid IPv4 address entered!" & _
								vbcrlf & vbcrlf & _
								"Please restart the script and enter a valid IPv4 address."
		crt.Quit																' As we did not enter a valid IP address, exit the Secure CRT
		Exit Sub
		End If
	    If strUser = "" Then												' Sub-routine to prompt for username
        strUser = Trim( _
            GetPlainTextInput(_
                "Specify username to connect with :", _
                "Username?", _
                strUser))
        ' Bail if user cancelled
        If strUser = "" Then Exit Sub
		End If
		If strPwd = "" Then															' Sub-routine to get password for the specified username
        strPwd = Trim(_
            GetPasswordInput(_
                "Specify password for " & strUser & ":", _
                "Password?", _
                strPwd))
        ' Bail if user cancelled
        If strPwd = "" Then Exit Sub
		End If
	Do 
	bCursorMoved = crt.Screen.WaitForCursor(1)								' I instruct SecureCRT to wait until cursor stops moving for 1 second (Can be increased for slower machines)
	Loop Until bCursorMoved = False
	strPrompt = GetShellPrompt()												' Determine shell's prompt
	crt.Screen.Send "ping -c 2 " & strIP & chr(13)							' Send 2 ICMP packets to the IP
	Dim pingResult
	pingResult = crt.Screen.WaitForStrings (vDevDown)				' And start checking the result
	Select Case pingResult
		Case 1													' Bad Luck !!! Non-responsive machine
		crt.Dialog.MessageBox "Device is not Responding to ICMP"
		Exit Sub
		Case 2													' In case of an responsive machine
		crt.Screen.Send chr(13)										' Another control Enter
		crt.Screen.WaitForString strPrompt								' wait for server's prompt
		crt.Screen.Send "ssh " + strUser + "@" + strIP & chr(13)	' First try to connect via SSH to device
			Dim nResult	
			nResult = crt.Screen.WaitForStrings(vWaitFors)			' Start analysing  the output
			Select Case nResult										' Now determine if RSA key has to be added or not in SSH case or try via Telnet
				Case 1												' RSA key for /.ssh/known_hosts must be added
				crt.Screen.Send "yes" & chr(13)							' so send and "yes"
				crt.Screen.WaitForString "assword:"						' then wait for password prompt
				crt.Screen.Send strPwd & chr(13)						' and send pass
				Case 2												' SSH not enabled on device, then go for telnet
				crt.Screen.Send "telnet " + strIP & chr(13)				' In this case only Telnet is permitted on device
				crt.Screen.WaitForString "sername:"						' wait for prompt
				crt.Screen.Send strUser & chr(13)						' send username
				crt.Screen.WaitForString "assword:"						' wait for password prompt
				crt.Screen.Send strPwd & chr(13)						' and send the pass
				Case 3												' RSA key already learned & SSH is permitted on the target device
				crt.Screen.Send strPwd & chr(13)						' just send pass from credentials file
			End Select				
	End Select
	devPrompt = GetShellPrompt()										' Now that we are connected, we determine device hostname
	crt.Screen.Send "terminal length 0" & chr(13)						' Execute "terminal length 0"
	crt.Screen.WaitForString devPrompt									' Waits for device's hostname
	crt.Screen.Send "show ver" & chr(13)								' Sends out the "show ver" command
	crt.Screen.WaitForString devPrompt									' Waits for device's hostname . Now these two pairs (Screen.Send amd Screen.WaitForString can be modified for other commands)
	SendExpect "show interface", devPrompt								' Now we exemplify the SendExpect function
																		' Depending on the needs, you can also start logging to a file the output of those commands
	crt.Screen.Send "exit" & chr(13)									' Disconnect from the Cisco device
	crt.Screen.WaitForString strPrompt
	crt.Screen.Synchronous = False	
End Sub

Function SendExpect(szSend, szExpect)							' Made this function to replace the Screen.Send/Screen.WaitForString pairs
																' Returns true if the text in 'szSend' was successfully sent and
																' the text in 'szExpect' was successfully found as a result.
																' If we're not connected, we can't possibly return true, or even send/recv text
    if g_objTab.Session.Connected <> True then exit function
    g_objTab.Screen.Send szSend & vbcr
    g_objTab.Screen.WaitForString szExpect
    SendExpect = True
End Function
'-------------------------------------------------------------------------------
Function GetPlainTextInput(strPrompt, strTitle, strDefaultValue)
    GetPlainTextInput = _
        crt.Dialog.Prompt(strPrompt, strTitle, strDefaultValue)
End Function				
'-------------------------------------------------------------------------------
Function GetPasswordInput(strPrompt, strTitle, strDefaultValue)
    GetPasswordInput = _
        crt.Dialog.Prompt(strPrompt, strTitle, strDefaultValue, True)
End Function
' -----------------------------------------------------------------------------
' GetShellPrompt - determine the shell's prompt
' -----------------------------------------------------------------------------
Function GetShellPrompt()
																				' Heuristically determine the shell's prompt.  Crt.Screen.Synchronous must
																				' already have been set to True.  In general, Crt.Screen.Synchronous should
																				' be set to True immediately after a successful crt.Session.Connect().  In
																				' This script, SecureCRT should already be connected -- otherwise, a script
																				' error will occur.
    Do																			' Simulate pressing "Enter" so the prompt appears again...
        crt.Screen.Send vbcr
																				' Attempt to detect the command prompt heuristically by waiting for the
																				' cursor to stop moving... (the timeout for WaitForCursor above might
																				' not be enough for slower- responding hosts, so you will need to adjust
																				' the timeout value above to meet your system's specific timing
																				' requirements).
        Do
            bCursorMoved = crt.Screen.WaitForCursor(1)
        Loop Until bCursorMoved = False
																				' Once the cursor has stopped moving for about a second, it's assumed
																				' it's safe to start interacting with the remote system. Get the shell
																				' prompt so that it's known what to look for when determining if the
																				' command is completed. Won't work if the prompt is dynamic (e.g.,
																				' changes according to current working folder, etc.)
        nRow = crt.Screen.CurrentRow
        strPrompt = crt.Screen.Get(nRow, 0, nRow, crt.Screen.CurrentColumn - 1)
																				' Loop until a line of non-whitespace text actually appears:
        strPrompt = Trim(strPrompt)
        If strPrompt <> "" Then Exit Do
    Loop
    
    GetShellPrompt = strPrompt
    
End Function
Reply With Quote
Reply

Tags
script , secure crt , vba


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 02:58 PM.