从 VBA 运行 Telnet 会话

发布于 2024-10-23 11:42:01 字数 140 浏览 1 评论 0原文

我有一个可以执行 FTP 功能的 VBA 库,我也想执行 telnet 操作。目前,我正在编写一个 Perl 脚本,该脚本基于文本文件执行 telnet,但我想从 VBA 内部本地驱动 telnet 连接。有人有这方面的资料吗?我不想使用加载项,我需要代码是独立的。

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.

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

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

发布评论

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

评论(1

忘你却要生生世世 2024-10-30 11:42:01

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

更多资源:

MSDN 库:使用 Winsock 控件

(Visual Basic) Winsock Control

如果没有,您也许可以使用 cmd.exeSendKeys

免责声明:我从下面的第二个链接复制了以下代码,并针对 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

SendKeys 不是最好或最可靠的解决方案,但它是我 12 年前在上一份工作中执行此操作时所能找到的。

详细信息:

Telnet 和Excel - Microsoft.excel.programming

如何自动化 Telnet 命令使用 VBScript

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

More resources:

MSDN Library: Using the Winsock Control

(Visual Basic) Winsock Control

if not, you could use cmd.exe and SendKeys perhaps:

Disclaimer: I copied the below code from the second of the links below, and modified it slightly for 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

SendKeys is 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.

More info:

Telnet & Excel - Microsoft.excel.programming

How to automate Telnet commands using VBScript

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