如何在vbscript中将字符串变量用作telnet命令? [英] How to use a string variable as a telnet command in a vbscript?

查看:99
本文介绍了如何在vbscript中将字符串变量用作telnet命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行Powershell脚本,该脚本将调用将启动telnet会话并运行以下命令的批处理脚本:

I'm trying to run a Powershell script that will call a batch script that will start a telnet session and run the following command:

mediaGet("gei",1)

感谢这个stackoverflow问题是否可以使用批处理文件建立telnet会话,发送命令并将输出写入文件?我编写了两个脚本:

Thanks to this stackoverflow question Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file? I have written two scripts:

start telnet.exe 162.100.10.10
cscript telnet.vbs



telnet.vbs



telnet.vbs

set OBJECT=WScript.CreateObject("WScript.Shell")
Dim cmd
cmd = "mediaGet("gei",1)
WScript.sleep 50 
OBJECT.SendKeys cmd
WScript.sleep 50 
OBJECT.SendKeys "{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "exit{ENTER}" 
WScript.sleep 5000
OBJECT.SendKeys " "

但是,当我运行脚本时,vbs脚本无法读取变量cmd的字符串。因此,我将其更改为:

However, when I run the scripts, the vbs script cannot read the string of the variable cmd. So I changed it as such:

cmd = "mediaGet" & chr(40) & chr(34) & "gei" & ",1" & chr(41)

但是,它仍然在telnet终端上这样输出:

But still, it outputs it on the telnet terminal as such:

mediaGet"gei",1

如何使其读取字符串变量中的括号?
如果脚本最终可以工作,是否有人知道如何将vbs脚本的输出保存到日志文件中?

How can I make it read the parenthesis in the string variable? If the scripts end up working, does anyone know how to save the output of the vbs script into a log file?

推荐答案

我不知道什么是mediaget或它做什么(实际上我从未听说过),所以,我不会对此发表评论。

I have no idea what mediaget is or what it does (actually I've never heard of it), so, I will not comment on that.

但是,通过PowerShell运行外部命令非常普遍并且有据可查。看到这些:

However, running external commands via PowerShell is very common and well documented. See these:

PowerShell:正在运行可执行文件

在PowerShell中正确执行外部命令

所有所说的...

如果您使用的是powershell.exe consolehost,那是一回事,但是您所要做的只是调用VBScript和cmd.exe项目。既然是这种情况,那为什么不只使用cmd.exe?

If you are using the powershell.exe consolehost that is one thing, but all you are doing is calling VBScript and cmd.exe items. Since that is the case then why not just use cmd.exe?

但是,PowerShell可以直接执行此操作,所以,为什么要对.bat和.vbs执行额外的步骤?

Yet, PowerShell can do this directly, so, why the extra steps to .bat and .vbs?

示例:粗糙且未经测试,因为我无需下载和测试mediaget

Example: rough and untested, because I have no need to download and test mediaget

Start-Process -FilePath 'telnet.exe' -ArgumentList '162.100.10.10'
[system.reflection.assembly]::loadwithpartialname("System.Windows.Forms")
$SendKeys = [System.Windows.Forms.SendKeys]
$sendkeys::SendWait('mediaGet gei,1')
Start-Sleep -Seconds .5
$sendkeys::SendWait("{ENTER}")
Start-Sleep -Seconds .5
$sendkeys::SendWait("exit{ENTER}")

您甚至可以使用.Net名称空间用自己的代码替换Telnet。

You can even replace Telnet with your own code, using the .Net namespace.

New-Object System.Net。 Sockets.TcpClient

New-Object System.Net.Sockets.TcpClient

以这种方式调用...

Calling it this way ...

新对象System.Net.Sockets.TcpClient ('162.100.10.10',80)

New-Object System.Net.Sockets.TcpClient('162.100.10.10', 80)

您可以使用它来编写您自己的模块,或从MS powershellgallery.com

You can use that to write your own module or use this one from the MS powershellgallery.com.

YouTube快速搜索显示,mediaget是一个Web下载工具。

A quick YouTube search shows me that mediaget is a web download tool.

如果您要做的只是访问网站/ ftp站点以下载文件,则也可以直接在PowerShell中进行操作。看到以下内容:

If all you are doing is hitting a website / ftp site to download a file, you can do that directly in PowerShell as well. See this:

使用PowerShell下载文件的3种方法

这篇关于如何在vbscript中将字符串变量用作telnet命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆