Search code examples
vbscripttelnetcisco

Automatic Telnet to cisco switches


I have many switches that we backup every night. I need to create a job that will automatically backup the running config.

I am currently using this, but how can I use a list of server ip addresses instead of having to repeat the code.

Option Explicit

On Error Resume Next

Dim WshShell

set WshShell=CreateObject("WScript.Shell")

WshShell.run "cmd.exe"

WScript.Sleep 1000

'Send commands to the window as needed - IP and commands need to be customized

'Step 1 - Telnet to remote IP'

WshShell.SendKeys "telnet 10.1.130.91 23"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000


'Step 2 - Issue Commands with pauses'

WshShell.SendKeys ("password")

WScript.Sleep 1000

WshShell.SendKeys ("{Enter}")

WScript.Sleep 500
WshShell.SendKeys ("Enable")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500

WshShell.SendKeys ("password")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500

WshShell.SendKeys ("terminal length 0")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500

WshShell.SendKeys ("show running-config")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500

wshShell.SendKeys ("copy run tftp://10.1.211.53/file1.xls")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500


wshShell.SendKeys ("10.1.211.53")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500

wshShell.SendKeys ("file1.xls")
WshShell.SendKeys ("{Enter}")
WScript.Sleep 2000


'Step 3 - Exit Command Window

WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 500
WshShell.SendKeys "exit"

WshShell.SendKeys ("{Enter}")

Solution

  • Here you go. Just create a file named CiscoIPs.txt with all your IP address with a carriage return after each one. Avoid spaces at the end of the records.

    10.2.2.23
    10.4.5.23
    10.5.7.23
    

    The IP list should be in the same folder as the VBScript, but you can certainly change this by editing "strIPFile = "CiscoIPs.txt" at the top of the script. Let me know if you have any issues.

    Option Explicit
    Dim WshShell, strIPFile, objFSO, objFile, IPAddress
    
    strIPFile = "CiscoIPs.txt"
    
    On Error Resume Next
    
    set WshShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Const ForReading =   1
    Const ForWriting =   2
    Const ForAppending = 8
    Const ReadOnly =     1
    
    Set objFile = objFSO.OpenTextFile(strIPFile, ForReading)
    
    Do Until objFile.AtEndOfStream
      IPAddress = objFile.ReadLine
      'WScript.Echo IPAddress
      WshShell.run "cmd.exe"
    
      WScript.Sleep 1000
    
      'Send commands to the window as needed - IP and commands need to be customized
    
      'Step 1 - Telnet to remote IP'
    
      WshShell.SendKeys "telnet " & IPAddress & " 23"
    
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 1000
    
      'Step 2 - Issue Commands with pauses'
    
      WshShell.SendKeys ("password")
    
      WScript.Sleep 1000
    
      WshShell.SendKeys ("{Enter}")
    
      WScript.Sleep 500
      WshShell.SendKeys ("Enable")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("password")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("terminal length 0")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("show running-config")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("copy run tftp://" & IPAddress & ".xls")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("10.1.211.53")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
    
      WshShell.SendKeys ("file1.xls")
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 2000
    
      'Step 3 - Exit Command Window
    
      WshShell.SendKeys "exit"
      WshShell.SendKeys ("{Enter}")
      WScript.Sleep 500
      WshShell.SendKeys "exit"
      WshShell.SendKeys ("{Enter}")
    Loop
    
    objFile.Close