自动Telnet到cisco交换机

时间:2016-08-29 08:07:48

标签: vbscript telnet cisco

我有很多我们每晚备份的开关。我需要创建一个自动备份正在运行的配置的作业。

我目前正在使用此功能,但如何使用服务器IP地址列表而不必重复代码。

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}")

1 个答案:

答案 0 :(得分:0)

你走了。只需创建一个名为CiscoIPs.txt的文件,其中包含您的所有IP地址,并在每个IP地址后添加回车符。避免在记录末尾留出空格。

10.2.2.23
10.4.5.23
10.5.7.23

IP列表应与VBScript位于同一文件夹中,但您当然可以通过编辑" strIPFile =" CiscoIPs.txt"来改变它。在脚本的顶部。如果您有任何问题,请告诉我。

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