【问题标题】:Automatic Telnet to cisco switches自动 Telnet 到 cisco 交换机
【发布时间】:2016-08-29 08:07:48
【问题描述】:

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

我目前正在使用这个,但我怎样才能使用服务器 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}")

【问题讨论】:

  • 虽然很明显这是某种 Visual Basic 脚本,但如果您使用语言标签和/或环境标签正确地标记问题,它确实有助于为您的问题找到合适的受众。请编辑您的问题以更新您的标签。也请read about how to ask good questions.
  • 我在脚本的 3 个位置看到了 IP 地址。我看到第一个是目标路由器的 telnet IP。另外2个是干什么用的?这些是否会改变,还是保持不变?在设置循环之前需要知道这一点。
  • 您好,感谢您的回复。抱歉,如果我没有使用正确的标签,我是这个论坛的新手。这是唯一会改变 WshShell.SendKeys "telnet 10.1.130.91 23" ip 地址的 ip 地址是:10.10.130.91, 10.1.130.101 等等 感谢您的帮助:-)
  • 嗨兰迪,是的,这是正确的。非常感谢它可以等待。别着急。

标签: vbscript telnet cisco


【解决方案1】:

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

【讨论】:

  • 拉斯,我忘了说。如果您发现自己需要从头开始创建 Telnet 脚本,那么这个实用程序是我见过的最好的。 “Telnet 脚本工具”查看我对这篇帖子的回答。 stackoverflow.com/questions/39274914/…
猜你喜欢
  • 2012-05-25
  • 1970-01-01
  • 2015-11-21
  • 2013-11-07
  • 2020-03-23
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多