2013-02-25 71 views
1

我的代码设置为telnet到交换机,并将启动配置tftp到服务器。如果我将IP地址硬编码到脚本中,此代码完美。在预先写好的代码中循环IP地址替换

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

在此先感谢!

这里是代码的副本我使用:

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

试试这个,主要是你通过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