M. Sterling
M. Sterling

Reputation: 11

Loop IP address substitution in prewritten code

I have code set up to telnet into a switch and tftp the startup-config to a server. This code works perfect if I hard code the IP address into the script.

What I would like to do is pass a list of IP addresses (one after the other) to the script so that I can copy all switch configs on the network. I have included some code below, this is the code I used to create the script. All I want to be able to do is replace the "telnet xx.xx.xx.xx" xx.xx.xx.xx entry with a list of IP addresses.

Thanks in advance!

Here is a copy of code I used:

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 

Upvotes: 1

Views: 633

Answers (1)

gtochoy
gtochoy

Reputation: 11

Try this, mostly your code with just a few lines inserted to loop through a list of IP addresses in plain text format. Please let me know if this works for you. Doing it this way opens a new command window for each telnet host and then closes it when its done. It would probably be easy to modify to use the same command window.

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 

Upvotes: 1

Related Questions