从VBA运行Telnet会话

Phi*_*bbs 5 ftp excel vba telnet excel-vba

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

Our*_*nas 1

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

更多资源:

MSDN 库:使用 Winsock 控件

(Visual Basic) Winsock 控件

如果没有,您可以使用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
Run Code Online (Sandbox Code Playgroud)

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

更多信息:

Telnet 和 Excel - Microsoft.excel.programming

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