从 VBA 运行 Telnet 会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5337527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:51:28  来源:igfitidea点击:

Running a Telnet session from VBA

vbaftpexcel-vbatelnetexcel

提问by PhilHibbs

I have a VBA library that does FTP, I'd like to do telnet as well. At the moment I'm shelling out to a Perl script that does the telnet based on a text file, but I'd like to drive the telnet connection natively from within the VBA. Does anyone have any source for this? I don't want to use an add-in, I need the code to be self-contained.

我有一个做 FTP 的 VBA 库,我也想做 telnet。目前,我正在使用基于文本文件执行 telnet 的 Perl 脚本,但我想从 VBA 内部驱动 telnet 连接。有没有人有任何来源?我不想使用加载项,我需要代码是自包含的。

回答by Our Man in Bananas

If you can use the MS WinSock control(also see using winsock in VBA)

如果您可以使用 MS WinSock 控件(另请参阅在 VBA 中使用 winsock

More resources:

更多资源:

MSDN Library: Using the Winsock Control

MSDN 库:使用 Winsock 控件

(Visual Basic) Winsock Control

(Visual Basic) Winsock 控件

if not, you could use cmd.exeand SendKeysperhaps:

如果没有,你可以使用的cmd.exeSendKeys可能:

Disclaimer: I copied the below code from the second of the links below, and modified it slightly for VBA.

免责声明:我从下面的第二个链接中复制了下面的代码,并为 VBA 稍微修改了它。

sub telNETScript()
On Error Resume Next
Dim WshShell as object

set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000

'Send commands to the window as needed - IP and commands need to be customized
'Step 1 - Telnet to remote IP'
WshShell.SendKeys "telnet xx.xx.xx.73 9999"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000

'Step 2 - Issue Commands with pauses'
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000
WshShell.SendKeys "5"
WshShell.SendKeys ("{Enter}")
WScript.Sleep 1000

'Step 3 - Exit Command Window
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit

end sub

SendKeysis not the best or most reliable solution, but it was all I could find when I did this 12 years ago in my last job.

SendKeys不是最好或最可靠的解决方案,但这是我 12 年前在上一份工作中做这件事时所能找到的。

More info:

更多信息:

Telnet & Excel - Microsoft.excel.programming

Telnet & Excel - Microsoft.excel.programming

How to automate Telnet commands using VBScript

如何使用 VBScript 自动执行 Telnet 命令