views:

5482

answers:

7

I have a VBScript script that starts a cmd prompt, telnets into a device and TFTP's the configuration to a server. It works when I am logged in and run it manually. I would like to automate it with Windows Task Scheduler.

Any assistance would be appreciated, here is the VBScript script:

set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "cmd" 
WScript.Sleep 100 
WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
WScript.Sleep 300 
WshShell.SendKeys "telnet 10.20.70.254{ENTER}" 
WScript.Sleep 300 
WshShell.SendKeys "netscreen" 
WScript.Sleep 300 
WshShell.SendKeys "{ENTER}" 
WScript.Sleep 300
WshShell.SendKeys "netscreen" 
WshShell.SendKeys "{ENTER}" 
WScript.Sleep 300 
WScript.Sleep 300 
WshShell.SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
WScript.Sleep 200 
WshShell.SendKeys "exit{ENTER}" 'close telnet session' 
set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "cmd" 
WScript.Sleep 100 
WshShell.AppActivate "C:\Windows\system32\cmd.exe" 
WScript.Sleep 300 
WshShell.SendKeys "telnet 10.20.70.254{ENTER}" 
WScript.Sleep 300 
WshShell.SendKeys "netscreen" 
WScript.Sleep 300 
WshShell.SendKeys "{ENTER}" 
WScript.Sleep 300
WshShell.SendKeys "netscreen" 
WshShell.SendKeys "{ENTER}" 
WScript.Sleep 300 
WScript.Sleep 300 
WshShell.SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed 
WScript.Sleep 200 
WshShell.SendKeys "exit{ENTER}" 'close telnet session' 
WshShell.SendKeys "{ENTER}" 'get command prompt back 
WScript.Sleep 200 
WshShell.SendKeys "exit{ENTER}" 'close cmd.exe

WshShell.SendKeys "{ENTER}" 'get command prompt back 
WScript.Sleep 200 
WshShell.SendKeys "exit{ENTER}" 'close cmd.exe
+3  A: 

Add a scheduled task that runs the script with your credentials. Remind yourself that you need to update the credentials on the task every time you change your password. It be a good idea to have the script "phone home" via email or something every time it is run so that you can tell if it is being executed.

It might also be a good idea to set up a separate service id for these sorts of activities. You may not need to change the password on the service id as frequently.

tvanfosson
If telnet and cscript may need a desktop context to run in (especially with "sendkeys"). You may need to leave the user logged into the machine for the scheduled task to work correctly.
Binary Worrier
+2  A: 

You can add a scheduled task and enter no credentials or password for it. This will cause it to run under LOCAL SYSTEM (which normally is the context the Task Scheduler service uses).

Be aware that this is a backdoor vulnerability scenario: Anyone allowed to edit your script file could misuse it to do undesirable things on the machine that runs the task. Put proper permission on the script file to prevent that. On the other hand - a task running as LOCAL SYSTEM cannot wreck havoc over the network.

I propose you condense your script file a little:

Set WshShell = WScript.CreateObject("WScript.Shell") 

Run "cmd.exe" 
SendKeys "telnet 10.20.70.254{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "save conf to tftp 10.10.40.139 test.cfg{ENTER}"
SendKeys "exit{ENTER}" 'close telnet session' 

Run "cmd.exe" 
SendKeys "telnet 10.20.70.254{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "netscreen" 
SendKeys "{ENTER}" 
SendKeys "save conf to tftp 10.10.40.139 palsg140.cfg{ENTER}" 'repeat as needed 
SendKeys "exit{ENTER}" 'close telnet session' 
SendKeys "{ENTER}" 'get command prompt back 
SendKeys "exit{ENTER}" 'close cmd.exe
SendKeys "{ENTER}" 'get command prompt back 
SendKeys "exit{ENTER}" 'close cmd.exe

Sub SendKeys(s)
  WshShell.SendKeys s
  WScript.Sleep 300
End Sub

Sub Run(command)
  WshShell.Run command
  WScript.Sleep 100 
  WshShell.AppActivate command 
  WScript.Sleep 300 
End Sub

Tomalak
Nice Sub's. That save about 20 lines! +1 ;-)
Lucas McCoy
A: 

Thanks for that I will reduce the size, I do have a scheduled task for the script, i have used a system and a service account to run it but it does not work, it says it ran but the tftp server has no file, the network is testing good during these tests. Is there something i am missing to call this dos app?

A: 

I'm pretty sure SendKeys will not work if the desktop is locked or no user is logged in.

aphoria
A: 

I'm pretty SendKeys will not work if you aren't logged in. It's unreliable in my experience anyway. You might be better off using a DOS batch file.

getftpconf.bat:

telnet 10.10.40.139
netscreen
netscreen
save conf to tftp 10.10.40.139 palsg140.cf
exit

Something like that.

If there is output in the command prompt that you need to record, you can put a " >> output.txt" at the end of the command line shortcut.

You could then call another batch file which sends that output.txt via ftp to where ever you need.

You can easily setup this batch file to run as a scheduled task in windows.

Mrgreen
A: 

Batch files don't work in Windows with Telnet (works fine in UNIX -- again, way to go Microsoft). As already mentioned here, sendkeys does not work in vba when not logged on.

Sorry I don't have the "this does work" solution for you....I'm stuck on the same problem

A: 

just make a batch file that contains this:

cscript.exe myscript.vbs

save it as something like myscript.bat.

Use schedule tasks to schedule the .bat file. After you create the scheduled task, you may have to check it's properties to make sure it's has appropriate user rights.

There are some options you can use with cscript so it doesn't show the logo, etc.

scottm

related questions