安排 .vbs 文件在 Windows 中运行

发布于 2024-07-08 02:56:53 字数 1842 浏览 5 评论 0原文

我有一个 VBScript 脚本,它启动 cmd 提示符、telnet 到设备和 TFTP 是服务器的配置。 当我登录并手动运行它时它就可以工作。 我想使用 Windows 任务计划程序 实现自动化。

如有任何帮助,我们将不胜感激,这是 VBScript 脚本:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

叹倦 2024-07-15 02:56:53

添加使用您的凭据运行脚本的计划任务。 提醒自己,每次更改密码时都需要更新任务的凭据。 最好在每次运行脚本时通过电子邮件或其他方式“打电话回家”,以便您可以判断它是否正在执行。

为此类活动设置单独的服务 ID 可能也是一个好主意。 您可能不需要频繁更改服务 ID 上的密码。

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.

ㄟ。诗瑗 2024-07-15 02:56:53

您可以添加计划任务并且无需为其输入凭据或密码。 这将导致它在本地系统(通常是任务计划程序服务使用的上下文)下运行。

请注意,这是一个后门漏洞场景:任何允许编辑脚本文件的人都可能滥用它在运行任务的计算机上执行不需要的操作。 对脚本文件赋予适当的权限以防止这种情况发生。 另一方面,作为本地系统运行的任务不会对网络造成严重破坏。

我建议你稍微压缩一下你的脚本文件:

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

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

疧_╮線 2024-07-15 02:56:53

我很确定如果桌面被锁定或没有用户登录,SendKeys 将无法工作。

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

捎一片雪花 2024-07-15 02:56:53

我很高兴,如果您未登录,SendKeys 将无法工作。
无论如何,以我的经验来看,这是不可靠的。
使用 DOS 批处理文件可能会更好。

getftpconf.bat:

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

类似的东西。

如果命令提示符中有需要记录的输出,可以在命令行快捷方式的末尾添加“>>output.txt”。

然后,您可以调用另一个批处理文件,该文件通过 ftp 将 output.txt 发送到您需要的任何地方。

您可以轻松地将此批处理文件设置为在 Windows 中作为计划任务运行。

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.

全部不再 2024-07-15 02:56:53

只需创建一个包含此内容的批处理文件:

cscript.exe myscript.vbs

将其另存为类似 myscript.bat 的文件。

使用计划任务来计划 .bat 文件。 创建计划任务后,您可能需要检查其属性以确保其具有适当的用户权限。

一些选项 您可以与 cscript 一起使用,因此它不会显示徽标等

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.

七婞 2024-07-15 02:56:53

批处理文件在带有 Telnet 的 Windows 中不起作用(在 UNIX 中工作得很好——再次,微软的做法)。 正如这里已经提到的,未登录时,sendkeys 在 vba 中不起作用。

抱歉,我没有为您提供“这确实有效”的解决方案......我遇到了同样的问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文