0

I have created one batch file through which I am trying to create one .vbs file and place some command into that. But I am not able to transfer my all the command to the .vbs file.

@echo off
echo:
set /a i=1
:Loop
if %i% GTR 0 (

rem create temp vbs file    
    echo Dim IPAddress > temp.vbs
    echo IPAddress="192.168.1.109" >> temp.vbs
    echo SET WshShell = WScript.CreateObject("WScript.Shell"^) >> temp.vbs
    echo WshShell.run("telnet.exe " &IPAddress) >> temp.vbs
    echo WScript.Sleep 3000 >> temp.vbs
    echo WshShell.SendKeys"qwert" >> temp.vbs
    echo WshShell.SendKeys("{Enter}") >> temp.vbs
    echo WScript.Sleep 3000 >> temp.vbs
    echo WshShell.SendKeys"asdf123" >> temp.vbs
    echo WshShell.SendKeys("{Enter}") >> temp.vbs
    echo WScript.Sleep 1000 >> temp.vbs

    echo ' Reboot Device >> temp.vbs
    echo ' WshShell.SendKeys"reboot" >> temp.vbs
    echo ' WshShell.SendKeys("{Enter}") >> temp.vbs
    echo ' WScript.Sleep 1000 >> temp.vbs
    echo strPath = WshShell.CurrentDirectory >> temp.vbs

    echo ' Close telnet session >> temp.vbs
    echo WshShell.Run "taskkill /im telnet.exe", , True  >> temp.vbs
    rem wscript C:\Users\guest\Desktop\temp.vbs
    echo Reboot Count %i%
    echo:
    set /a i+=1
    Timeout /t 90 >nul
    GOTO Loop
    ) ELSE (
    echo Out of if loop 
    )

Following is the Actual output of the temp.vbs file

WshShell.run("telnet.exe " 
WScript.Sleep 3000 
WshShell.SendKeys"qwert" 
WshShell.SendKeys("{Enter}") 
WScript.Sleep 3000 
WshShell.SendKeys"asdf123" 
WshShell.SendKeys("{Enter}") 
WScript.Sleep 1000 
' Reboot Device 
' WshShell.SendKeys"reboot" 
' WshShell.SendKeys("{Enter}") 
' WScript.Sleep 1000 
strPath = WshShell.CurrentDirectory 
' Close telnet session 
WshShell.Run "taskkill /im telnet.exe", , True

Expected output of temp.vbs file

Dim IPAddress
IPAddress="192.168.1.109"
SET WshShell = WScript.CreateObject("WScript.Shell"^)
WshShell.run("telnet.exe " &IPAddress)
WScript.Sleep 3000
WshShell.SendKeys"qwert"
WshShell.SendKeys("{Enter}")
WScript.Sleep 3000
WshShell.SendKeys"asdf123"
WshShell.SendKeys("{Enter}")
WScript.Sleep 1000

' Reboot Device
' WshShell.SendKeys"reboot"
' WshShell.SendKeys("{Enter}")
' WScript.Sleep 1000
strPath = WshShell.CurrentDirectory

' Close telnet session
WshShell.Run "taskkill /im telnet.exe", , True

Kindly help me on this to achieve this scenario.

Thank your very much in advance.

Vinkesh Shah
  • 105
  • 1
  • 1
  • 8
  • 4
    You must escape all closing parentheses in your `echo` statements. With that said: do not use `SendKeys` for scripting `telnet`. Use a `telnet` command that's actually scriptable (like [`plink`](https://the.earth.li/~sgtatham/putty/0.70/htmldoc/Chapter7.html#plink) from the [PuTTY suite](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)). – Ansgar Wiechers Aug 16 '17 at 11:15
  • 4
    There are [more graceful ways](https://stackoverflow.com/a/9074483/1683264) to make a Batch + VBScript hybrid. (And [a few more examples](https://stackoverflow.com/search?q=user%3A1683264+batch+vbscript+wsf).) – rojo Aug 16 '17 at 11:46
  • 2
    Please stop promoting Frankenscripts. They may have a certain fascination intellectually, but they're a pain to maintain and troubleshoot in production. – Ansgar Wiechers Aug 16 '17 at 11:53
  • 1
    I have to agree... There are so many better ways to solve this problem. You're making it hard for yourself! What is the *root problem* that you're trying to solve with telnet? When you can answer that, then close this and ask a new question. – BoffinBrain Aug 16 '17 at 12:32
  • I have one device & I need to reboot it. The only way to reboot that device by doing telnet and then give reboot command. I am trying to make one script, using that I needs to put device reboot contentiously over the night & in morning I should able to check how many times device get rebooted. – Vinkesh Shah Aug 16 '17 at 12:52

1 Answers1

0

You can create a .vbs script with commands you walt to run in the same folder and hidden it. Next you can run it from batch file.

Pietro122
  • 17
  • 3