I'm attempting to create a VBScript I can run from a scheduled task to restart our SonicWall firewall. The firewall is a "lower-end" business model (TZ400), and I periodically have to reboot it because our Internet connection seems to be bogged down (that's another issue that I'll have to research later).
I found the following code on another site for using plink.exe
and modified it for my environment:
Set sw= CreateObject("WScript.Shell")
sw.run"C:\plink\path\Plink.exe -ssh adminuser@X.X.X.X -pw adminpassword"
WScript.Sleep 500
sw.SendKeys "restart"
sw.SendKeys "{Enter}"
WScript.Sleep 800
sw.SendKeys "yes"
sw.SendKeys "{Enter}"
WScript.Sleep 10000
sw.SendKeys "exit"
sw.SendKeys "{Enter}"
Unfortunately, when I run this script, it seems to skip over some of the characters from the SendKeys
method calls. If I run it from my machine (Windows 7), I can usually get it to pass "enough" characters in order to complete the operation, but when I try running the same script on the server (Windows Server 2008 R2 - I've connected through RDP), it goes completely haywire. See the screenshot below for an example after one particularly "wrong" attempt.
I've tried putting a delay between each of the commands and it's corresponding {ENTER}
, but that just seems to make it worse. I've tried enclosing the text to send in parentheses just in case it had something to do with the syntax of the method call, but that didn't help either.
From what I can tell, it seems that the command prompt is simply not able to "keep up" with the SendKeys
method for some reason. Has anyone else experienced this issue, and does anyone have a suggestion for the best way to avoid or get around it? For example, is there a way to "slow down" the execution of the script in some way? Thanks in advance for any suggestions.
EDIT: I've been able to get the script to execute successfully on the server by altering the script to send each character separately with a 100ms sleep between them:
Set sw= CreateObject("WScript.Shell")
sw.run"C:\plink\path\Plink.exe -ssh adminuser@X.X.X.X -pw adminpassword"
WScript.Sleep 500
sw.SendKeys "r"
WScript.Sleep 100
sw.SendKeys "e"
WScript.Sleep 100
sw.SendKeys "s"
WScript.Sleep 100
sw.SendKeys "t"
WScript.Sleep 100
sw.SendKeys "a"
WScript.Sleep 100
sw.SendKeys "r"
WScript.Sleep 100
sw.SendKeys "t"
WScript.Sleep 100
sw.SendKeys "{Enter}"
WScript.Sleep 800
sw.SendKeys "y"
WScript.Sleep 100
sw.SendKeys "e"
WScript.Sleep 100
sw.SendKeys "s"
WScript.Sleep 100
sw.SendKeys "{Enter}"
WScript.Sleep 10000
sw.SendKeys "e"
WScript.Sleep 100
sw.SendKeys "x"
WScript.Sleep 100
sw.SendKeys "i"
WScript.Sleep 100
sw.SendKeys "t"
WScript.Sleep 100
sw.SendKeys "{Enter}"
wScript.quit
While this workaround seems to work, it seems a bit "brute-force" and doesn't actually resolve whatever the underlying issue is, so I'm still interested in any thoughts or suggestions.
NOTE: While this does allow me to run the VBScript manually from the command line, I'm still having issues getting it to run from the Scheduled Task. It seems to start and finish, but the firewall never actually restarts. Nevermind... I know why it isn't running from the Scheduled Task, and I'm going to have to come up with a better script to handle this particular operation.
restat
instead ofrestart
- missing the lastr
, but not the finalt
character), which, unfortunately, wouldn't be solved by waiting after the command is sent. – G_Hosa_Phat Oct 3 '18 at 1:26sw.AppActivate "%WinTitle%"
where%WinTitle%
is the actual value of the plink window title first after connection and before puttying in the sendkeys wouldn't help? If not, I wonder if the SSH session lags and using sendkeys just doesn't get send to the terminal or whatever when there is a lag. – IT Thug Ninja Oct 3 '18 at 1:34