从VBA运行Telnet会话

问题描述 投票:5回答:1

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

vba ftp excel-vba telnet excel
1个回答
0
投票

如果你能use the MS WinSock control(也见using winsock in VBA

更多资源:

MSDN Library: Using the Winsock Control

(Visual Basic) Winsock Control

如果没有,你可以使用cmd.exe和SendKeys

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

How to automate Telnet commands using VBScript

© www.soinside.com 2019 - 2024. All rights reserved.