Vbscript 自动Telnet到cisco交换机

Vbscript 自动Telnet到cisco交换机,vbscript,telnet,cisco,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地址列表而不必重复代码呢

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

给你。只需创建一个名为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

虽然很明显这是某种Visual Basic脚本,但如果您使用语言标记和/或环境标记正确标记问题,它确实有助于为您的问题找到合适的受众。请编辑您的问题以更新标记。另外,请。我在脚本中的3个位置看到了IP地址。我看到第一个是的telnet IP目标路由器。其他两个用于什么?它们是否会改变,还是保持不变?在设置循环之前需要知道这一点。您好,感谢您的回复。抱歉,如果我没有使用正确的标记,我是此论坛的新成员。这是唯一会更改WshShell.SendKeys“telnet 10.1.130.91 23”的ip“ip地址是:10.10.130.91、10.1.130.101等等。感谢您的帮助:-)嗨,兰迪,是的,没错。非常感谢,可以等一下。别急,拉尔斯,我忘了提了。如果您发现自己需要从头开始创建Telnet脚本,那么这个工具是我见过的最好的工具。“Telnet脚本工具”查看我在这篇帖子上的答案。