从 VBA 运行 Telnet 会话 [英] Running a Telnet session from VBA

查看:67
本文介绍了从 VBA 运行 Telnet 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行 FTP 的 VBA 库,我也想执行 telnet.目前,我正在使用基于文本文件执行 telnet 的 Perl 脚本,但我想从 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.

推荐答案

如果可以的话使用 MS WinSock 控件(另见 在VBA中使用winsock)

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

更多资源:

MSDN 库:使用 Winsock 控件

(Visual Basic) Winsock 控件

如果没有,你可以使用 cmd.exeSendKeys 或许:

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

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

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 不是最好或最可靠的解决方案,但这是我 12 年前在上一份工作中这样做时所能找到的.

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.

更多信息:

Telnet &Excel - Microsoft.excel.programming

如何使用 VBScript 自动化 Telnet 命令

这篇关于从 VBA 运行 Telnet 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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