View Single Post
  #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