在预写代码中循环IP地址替换

时间:2013-02-25 18:58:53

标签: vbscript

我已将代码设置为telnet到交换机并将startup-config tftp到服务器。如果我将IP地址硬编码到脚本中,则此代码非常有用。

我想要做的是将一个IP地址列表(一个接一个)传递给脚本,以便我可以复制网络上的所有交换机配置。我在下面包含了一些代码,这是我用来创建脚本的代码。我希望能够做的就是将“telnet xx.xx.xx.xx”xx.xx.xx.xx条目替换为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 xx.xx.xx.xx"

WshShell.SendKeys ("{Enter}")

WScript.Sleep 1000

'Step 2 - Issue Commands with pauses'

WshShell.SendKeys ("{Enter}")

WScript.Sleep 1000

WshShell.SendKeys "5"

WshShell.SendKeys ("{Enter}")

WScript.Sleep 1000

'Step 3 - Exit Command Window

WshShell.SendKeys "exit"

WshShell.SendKeys ("{Enter}")

WScript.Quit 

1 个答案:

答案 0 :(得分:1)

尝试这一点,主要是你的代码只插入几行,以纯文本格式循环遍历IP地址列表。如果这对你有用,请告诉我。这样做会为每个telnet主机打开一个新的命令窗口,然后在完成后关闭它。修改它以使用相同的命令窗口可能很容易。

Option Explicit

On Error Resume Next

Dim WshShell
Dim objFSO
Dim objInputFile
Dim strIP

set WshShell=CreateObject("WScript.Shell")


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("IPFilelist.txt", 1)

Do Until objInputFile.AtEndofStream

    strIP = objInputFile.ReadLine

    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 " & strIP

    WshShell.SendKeys ("{Enter}")

    WScript.Sleep 1000

    'Step 2 - Issue Commands with pauses'

    WshShell.SendKeys ("{Enter}")

    WScript.Sleep 1000

    WshShell.SendKeys "5"

    WshShell.SendKeys ("{Enter}")

    WScript.Sleep 1000

    'Step 3 - Exit Command Window

    WshShell.SendKeys "exit"

    WshShell.SendKeys ("{Enter}")

Loop

WScript.Quit