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-20-2007, 12:44 AM
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, 02: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, 03: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, 09: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, 03: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 04:27 AM.
Reply With Quote
  #6  
Old 01-21-2007, 05: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
  #7  
Old 01-21-2007, 09:50 AM
Grunt11B's Avatar
Grunt11B Grunt11B is offline
Registered User
 
Join Date: Aug 2006
Posts: 22
Here's a script I use to figure out where I am in the config when I login. It may be of some use to you...

Cheers

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

Const BUTTON_OK = 0 ' OK button only
Dim strPassword, strResult, strHostname, strPrompt, strVar1, strVar2, intTest, dlgError, intTab, intResult, strUsername, strWait, appExcel, appExcelwb, appExcelws, intDialog

Sub main
Crt.Screen.Synchronous = True
On Error Resume Next
strUsername = Crt.Dialog.Prompt("Enter Tacacs Username:", "Device Hostname", "", True)
strPassword = Crt.Dialog.Prompt("Enter Tacacs Password:", "Device Hostname", "", True)
strHostname = Crt.Dialog.Prompt("Enter Device Hostname:", "Device Hostname", "", False)
If strHostname = "" Then
Exit Sub
End If
intTest = 0
intResult = 0
strVar1 = Left(strHostname, 6) '
strWait = Array(Chr(10), strVar1, "#", ">")
If Crt.Session.Connected = True Then
Set Tab = Crt.Session.ConnectInTab("/telnet " & strHostname)
Set Caption = Tab
Caption.Caption = strHostname & " - !"
Else
Set Tab = Crt
Tab.Session.Connect("/telnet " & strHostname)
Set Caption = Tab.Window
Caption.Caption = strHostname & " - !"
End If
If err.number <> 0 Then
intDialog = Crt.Dialog.MessageBox("Failed to connect: " & strHostname, Error.description, BUTTONOK)
Exit Sub
End If
Tab.Screen.Synchronous = True
Tab.Screen.WaitForString "sername:"
Tab.Screen.Send strUsername & VbCr
Tab.Screen.WaitForString "assword:"
Tab.Screen.Send strPassword & VbCr
Tab.Screen.WaitForCursor
Do While intTest = 0
intResult = Tab.Screen.WaitForStrings(strWait)
If intResult = 4 Then
strResult = Tab.Screen.WaitForString(" (enable)", 1)
End If
If intResult > 2 Then
strResult = Tab.Screen.Get(Tab.Screen.CurrentRow, 1, Tab.Screen.CurrentRow, Tab.Screen.CurrentColumn)
strResult = Trim(strResult)
strPrompt = Right(strResult, 1)
intTest = 1
Exit Do
End If
Loop
Select Case strPrompt
Case "#"
Tab.Screen.Send "term len 0" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh startup" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh running" & VbCr
Tab.Screen.WaitForString strResult
Case ")"
Tab.Screen.Send "set len 0" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh running" & VbCr
Tab.Screen.WaitForString strResult
Case ">"
MsgBox "Failed to enter privileged mode!"
End Select
Caption.Caption = strHostname
Tab.Screen.Synchronous = False
Crt.Screen.Synchronous = False
End Sub
__________________
-Christopher
Reply With Quote
  #8  
Old 01-21-2007, 10:45 AM
eazy2001x eazy2001x is offline
Registered User
 
Join Date: Jan 2007
Posts: 17
Chris,

This seems like the bulk of your code and maybe its a lil advanced for me but i'll try..

Code:
Tab.Screen.Synchronous = True
Tab.Screen.WaitForString "sername:"
Tab.Screen.Send strUsername & VbCr
Tab.Screen.WaitForString "assword:"
Tab.Screen.Send strPassword & VbCr
Tab.Screen.WaitForCursor
Do While intTest = 0
intResult = Tab.Screen.WaitForStrings(strWait)
If intResult = 4 Then
strResult = Tab.Screen.WaitForString(" (enable)", 1)
End If
If intResult > 2 Then
strResult = Tab.Screen.Get(Tab.Screen.CurrentRow, 1, Tab.Screen.CurrentRow, Tab.Screen.CurrentColumn)
strResult = Trim(strResult)
strPrompt = Right(strResult, 1)
intTest = 1
Exit Do
End If
Loop
Select Case strPrompt
Case "#"
Tab.Screen.Send "term len 0" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh startup" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh running" & VbCr
Tab.Screen.WaitForString strResult
Case ")"
Tab.Screen.Send "set len 0" & VbCr
Tab.Screen.WaitForString strResult
Tab.Screen.Send "sh running" & VbCr
Tab.Screen.WaitForString strResult
Case ">"
MsgBox "Failed to enter privileged mode!"
End Select
Caption.Caption = strHostname
Tab.Screen.Synchronous = False
Crt.Screen.Synchronous = False
End Sub
Is seems like your using strWait as a variable which contains (# & >). Then you set the intResult to equal any of the listed variables. Now if it finds the # it sets the WaitForString to enable. If it doesn’t see the error level 4 then ends if and jumps to the next if statement.

You set the strResult to equal any of the lister varaibles. Now if it finds error number > 2 what does it do next? and how does it go about detecting which case prompt to use based on what it finds?

This is something that I could also use.. Sorry I got a little lost at the end..
Reply With Quote
  #9  
Old 01-22-2007, 06:55 AM
wirelessmonk01 wirelessmonk01 is offline
Registered User
 
Join Date: Nov 2006
Posts: 16
Hey all. Thanks so much for your help. I finally got it to where I need it to be. Here's the code if anyone would like to offer constructive criticism for the next script I write.

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

Const USERNAME = "xxxxx" 
Const PASSWORD = "xxxxxxx"
Const DEVICE_FILE_PATH = "C:\Common\hostlists\xxxxx_hostlist.txt"	

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fil
Set fil = fso.OpenTextFile(DEVICE_FILE_PATH)

On Error Resume Next

Sub Main

	Dim LogFileName
	Dim ip
	Dim name
	Dim line
	
	On Error Resume Next

	While Not fil.AtEndOfStream
	
		line = fil.ReadLine
		
		name = Split(line, ",")(0)
		ip = Split(line, ",")(1)

		crt.Session.Connect("/s xxxBACKUPSCRIPTxxx")
		crt.Screen.Synchronous = True
		
		crt.Screen.Send "telnet " & ip & vbCr
		crt.Screen.WaitForString "Username:"
		crt.Screen.Send USERNAME & vbCr
		crt.Screen.WaitForString "Password:"
		crt.Screen.Send PASSWORD & vbCr
			If crt.Screen.WaitForString(">",1)  = True Then
				crt.Screen.Send "en" & vbCr
				crt.Screen.WaitForString "Password:"
				crt.Screen.Send PASSWORD & vbCr
			End If

		
		crt.Screen.WaitForString "#", 3
		crt.Screen.Send "term length 0" & vbCr
		crt.Screen.WaitForString "#", 3
		crt.Screen.Send "sh run" & vbCr
		crt.Session.logfilename = "C:\Common\backups_go_here\"&name&"."&"%M%D%Y"&".log"
		crt.Session.Log True
		crt.Screen.WaitForString "#", 3
		crt.Screen.Send "no term length 0" & vbCr
		crt.Screen.Send "exit" & vbCr
		crt.Session.Log False
		crt.Session.disconnect
	Wend

	fil.close
End Sub
Reply With Quote
  #10  
Old 01-22-2007, 07:42 AM
Grunt11B's Avatar
Grunt11B Grunt11B is offline
Registered User
 
Join Date: Aug 2006
Posts: 22
Sorry for the late reply, I see you already got it working. But, I wanted to answer your questions anyway.

Basically, I posted my script as an example to use for looking for an IOS based switch versus a CatOS based switch and checking for enable mode.

Here's some pseudo code:

1. Did we get a new line and hostname?
no: loop
yes: goto 2.
2. Did we get a # or >? (intResult will be 3 or 4)
2.a If we got a 4, wait for enable mode " (enable)" timeout after 1 sec.
2.b Got 3 or 4: Get hostname, update caption, set intTest to 1 and exit loop
3. Get last character of strPrompt, should be #, >, or ), nothing else.
5. Select case and apply appropriate commands, update caption.
6. End script.

That's pretty much it. It just logs in and waits for the specified characters and then checks the last one to see where the prompt is. If it's one of the three, it fits the case. If it isn't, it errors out.

Let me know if you have any other questions. You can message me directly or whatever...

Cheers,

-Chris
__________________
-Christopher
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 10:37 PM.