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 01-19-2007, 11:44 PM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
enabling/disabling logging

Ok, forgive the complete noob question...

I am trying to write a script that will, one at a time, telnet to a Cisco router, enable logging on SecureCRT, do a "show run" command, disable logging on SecureCRT and save the log file into a folder.

I literally just started trying to figure out VB scripting about, oh...2 hours ago

My immediate question is this, how do I enable/disable SecureCRT logging through vbscript? Or can I?
Reply With Quote
  #2  
Old 01-20-2007, 01:49 AM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
sorry to double post, but

crt.Session.Log True

Is this correct? This didn't show up when I searched earlier. I guess I should have been more broad in my search.
Reply With Quote
  #3  
Old 01-20-2007, 02:10 AM
eazy2001x eazy2001x is offline
Registered User
 
Join Date: Jan 2007
Posts: 17
You might be able to get away with this code.. i also added the commands to do a show run. it will also send the output fo the screen to a text file on your HD.

Code:
#$language = "VBScript"
#$interface = "1.0"
' ConnectToMultipleHostsAndSendCommands.vbs
'
Option Explicit

Dim g_shell, g_fso
Set g_shell = CreateObject("WScript.Shell")
Set g_fso   = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

' array of target hosts to which we will be connecting
dim g_vHosts(100)
g_vHosts(0) = "192.168.204.129"
g_vHosts(1) = "192.168.0.2"
g_vHosts(2) = "192.168.0.3"
g_vHosts(3) = "192.168.0.123"
g_vHosts(4) = "192.168.0.131"
' g_vHosts(5) = ""
' g_vHosts(6) = ""
' g_vHosts(7) = ""
' g_vHosts(8) = ""
' ...

' List of commands that we will issue on each host after
' we are connected
dim g_vCommands(2)
g_vCommands(0) = "show run"
g_vCommands(1) = "exit"

' What we'll be waiting for each time we run a command so that
' we know the command has finished (since our shell prompt will
' appear once again)...
Dim g_szPrompt
g_szPrompt = "#"

' We'll log to a separate file for each connection/host, and
' we'll place the log files in the current user's "My Documents"
' folder...
Dim g_szLogNameTemplate
g_szLogNameTemplate = g_shell.SpecialFolders("MyDocuments") & _
"\MM-DD-YYYY - HOST.log"

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Main()
   crt.screen.synchronous = true

   Dim nIndex, szHost
   For nIndex = 0 to uBound(g_vHosts) - 1
       szHost = Trim(g_vHosts(nIndex))
 
' If the szHost is empty, it means we've gone through all
       ' populated elements of our g_vHosts array, and we should
       ' quit now.
       if szHost = "" then exit For

       ' Reset logging to 'off'
       crt.session.Log False

       ' Disconnect from any previous connection we have made
       If crt.Session.Connected = True then
           crt.session.Disconnect
       end if

       On Error Resume Next

       ' Choose your connect method
       '
       ' the use of the /Telnet connection method will require additional logic for
       ' detecting the username and password prompts (using crt.screen.WaitForString)
       ' and entering these values in order to authenticate successfully before running
       ' any commands.        '
       '      crt.session.connect "/Telnet " & szHost
       '

       crt.session.connect "/SSH2 /L user /PASSWORD password /Z 6 " & szHost

       '
       ' The following connection method requires that the
       ' values in g_vHosts are session names, rather than
       ' hostnames or IP addresses
       '      crt.session.connect "/S " & szHost


       ' Set a log file name for this next connection and enable logging.
       Dim szLogFilename
       szLogFilename = g_szLogNameTemplate
       szLogFilename = Replace(szLogFilename, "HOST", szHost)
       szLogFilename = Replace(szLogFilename, "MM", NN(Month(Now)))
       szLogFilename = Replace(szLogFilename, "DD", NN(Day(Now)))
       szLogFilename = Replace(szLogFilename, "YYYY", Year(Now))

       If Err.Number = 0 then
           ' We've connected successfully.

           ' Now that we have connected, turn on logging
           ' for this connection
           crt.session.logfilename = szLogFilename
           crt.session.log True

           ' Let's issue commands
           Dim nCommandIndex
           For nCommandIndex = 0 to UBound(g_vCommands) - 1
               crt.Screen.Send g_vCommands(nCommandIndex) & vbcr

               ' Wait for the command to complete. If you want this to time out
               ' after a number of seconds, provide a timeout value to the WaitForString
               ' command as in:
               '     nResult = crt.Screen.WaitForString(g_szPrompt, 100)
               '
               ' This type of construct will wait for the value of g_szPrompt for 100 seconds
               ' and return false if the timeout occurs before the prompt string is detected.
               '
               ' For this example, we'll just wait forever.
               crt.Screen.WaitForString g_szPrompt
           Next
       Else
           ' We didn't connect successfully.  We should
           ' record this information in the log file for
           ' this connection.
           Dim objFile
           Set objFile = g_fso.OpenTextFile(szLogFileName, 2, True)
           objFile.Write "Unable to connect to host: " & szHost & vbcrlf & _
                         "  Error (" & Err.Number & "): " & Err.Description
           objFile.Close
       End if

       ' Disconnect from the current connection and turn off logging
       If crt.Session.Connected = True then
           crt.session.Log False
           crt.session.Disconnect
       end if

   ' Now that we've disconnected and turned off logging, let's move
   ' on to the next host...
   Next

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function NN(nNumber)
' Normalizes a single digit number to have a 0 in front of it
   NN = nNumber
   If nNumber < 10 Then NN = "0" & nNumber
End Function
you also need to replace the name "user" and "password" with your username and password. also if you add the GV commands make sure you add them in order. Good luck
Reply With Quote
  #4  
Old 01-20-2007, 08:05 PM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
awesome, thanks easy2001x!! I'll see if I can modify this script a bit (to use a csv instead of an array for target hosts) and I'll be in business.

Thanks again!

p.s. what is the difference in using g_vCommands as opposed to crt.Screen.Send?
Reply With Quote
  #5  
Old 01-21-2007, 02:28 AM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
Hey all, I'm still struggling with this script a bit. I decided to work with a different script that was originally intended for changing router passwords. I took out some parts that didn't apply to my situation (i.e. the original offered a telnet, ssh1 or ssh2 case statement, while I only need telnet), and it certainly isn't finished, but I'm running into a bit of a speedbump. We don't log directly on the the remote routers, but rather to a local *nix box that is on a vpn with the routers. The connection with this *nix box is saved as a "session" in Secure CRT. I would like to be able to load this "session" manually, and then run the script rather than write the connection to the *nix box into the script. When I load the "session" and then run the script I get an "already connected" error (specificaly at line 41). I assume this is because SecureCRT already sees a connection open, but how do I get past this? Here's the script so far (*note, I censored some of the info*)


I'm sure I've butchered some stuff in here, but be kind...


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


Sub Main
	Const username = "xxxxxx" ' Username to use for login
	Const password = "xxxxxxxx" ' Password for corresponding user
	Const loginPass = "xxxxxxxx" ' Password to use for password only login (aka no aaa new-model)
	
'10	
	Const DEVICE_FILE_PATH = "C:\Documents and Settings\xxxxx\Desktop\devicestest.txt"
	
	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")
	
	Dim fil
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

	crt.session.Connect("/s xxxxxxxxxx")
'20
	
	Dim ip
	Dim name
	Dim line
	Dim cnxnString
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		
		name = Split(line, ",")(0)
		ip = Split(line, ",")(1)
'31				
		
		cnxnString = ("/TELNET " & ip & " 23")
		
		
		' Connect 
		crt.Screen.Synchronous = True
		crt.Session.Connect cnxnString
		' Enable
		crt.Screen.WaitForString "Username:"
		crt.Screen.Send username & vbCr
		crt.Screen.WaitForString "Password"
		crt.Screen.Send password & vbCr
'44
	Wend
	
	fil.Close 
End Sub

Ok, I edited this post a bit. I can now start my session within the script, but I'm still getting the same error. "Already connected". Any ideas?

Last edited by wirelessmonk01; 01-21-2007 at 03:27 AM.
Reply With Quote
  #6  
Old 01-21-2007, 04:53 AM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
OK, as I am prone to do, I am doing all my own thread answering. I'm not completely out of the woods yet, but I'm getting closer. I can now start a script which loads a session, telnets into the first router on the list, and enters the username and password. At this point in my script I have it waiting for the ">" that indicates global config mode. From there it takes the steps to enter enable mode. The problem is: some of the routers are in enable mode by default. So instead of a ">" when I log in, there is a "#" waiting for me. This is only some of the routers so I definitely need to keep the crt.Screen.WaitForString ">" in there, but how do I tell it to skip ahead if it doesn't find it in say...15 seconds??

I fully expect I'll find the answer somewhere and post my own reply (nobody else seems to be up at this time of...morning, and I don't blame you!)

Here's the script so far...feel free to constructively criticize.

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


Sub Main
	Const username = "xxxxxx" ' Username to use for login
	Const password = "xxxxxxx" ' Password for corresponding user
	Const loginPass = "xxxxxxxx" ' Password to use for password only login (aka no aaa new-model)
	
'10	
	Const DEVICE_FILE_PATH = "C:\Documents and Settings\xxxxxx\Desktop\devicestest.txt"
	
	Dim fso
	Set fso = CreateObject("Scripting.FileSystemObject")
	
	Dim fil
	Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

	crt.session.Connect("/s xxxxxxxxxxxx")
'20
	
	Dim ip
	Dim name
	Dim line
	
	While Not fil.AtEndOfStream
		line = fil.ReadLine
		
		name = Split(line, ",")(0)
		ip = Split(line, ",")(1)
'31				
		
		
		crt.session.Log True
		crt.session.Logfilename = "C:\Documents and Settings\xxxxxx\Desktop\backup_configs\"&name&".log"
		
		' Connect 
		crt.Screen.Synchronous = True		
		crt.Screen.Send "telnet " & ip &  vbCr
'40
		' Enable
		crt.Screen.WaitForString "sername:"
		crt.Screen.Send username & vbCr
		crt.Screen.WaitForString "assword"
		crt.Screen.Send password & vbCr
		crt.Screen.WaitForString ">"
		crt.Screen.Send "en" & vbCr
		crt.Screen.WaitForString "assword"
		crt.Screen.Send loginPass
		crt.Screen.WaitForString "#"
		crt.Screen.Send "sh run"
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Screen.Send " "
		crt.Session.Log False
		crt.Session.Disconnect



	Wend
	
	fil.Close 
End Sub
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:37 AM.