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

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

问题描述

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

解决方案

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



更多资源:



MSDN库:使用Winsock控件



(Visual Basic)Winsock控件



如果没有,您可以使用 cmd.exe SendKeys 或许:



免责声明:我从下面的第二个链接复制了下面的代码,并对VBA进行了一些修改。

  sub telNETSc ript()
On Error Resume Next
Dim WshShell as object

set WshShell = CreateObject(WScript.Shell)
WshShell.runcmd.exe
WScript.Sleep 1000

'根据需要向窗口发送命令 - 需要自定义IP和命令
'步骤1 - Telnet到远程IP'
WshShell .SendKeystelnet xx.xx.xx.73 9999
WshShell.SendKeys({Enter})
WScript.Sleep 1000

'步骤2 - 发出命令与暂停'
WshShell.SendKeys({Enter})
WScript.Sleep 1000
WshShell.SendKeys5
WshShell.SendKeys({Enter})
WScript.Sleep 1000

'步骤3 - 退出命令窗口
WshShell.SendKeys退出
WshShell.SendKeys({Enter})
WScript 。Quit

end sub

SendKeys 不是最好的或最可靠的解决方案,但是当我12年前在我上一次工作的时候,我才能找到这一切。



更多信息:



Telnet& Excel - Microsoft.excel.programming



如何使用VBScript自动执行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.

解决方案

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

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

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