vba 如何从 Excel 自动化 PuTTy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24331647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:32:33  来源:igfitidea点击:

How to automate PuTTy from Excel?

excelvbaexcel-vbaputty

提问by Arun

I have been trying to automate the following sequence of steps in puTTy via VBA from Excel

我一直在尝试通过 Excel 中的 VBA 在 puTTy 中自动执行以下步骤序列

  1. Login to putty
  2. execute a command
  3. copy the result from putty and paste it in excel.
  1. 登录腻子
  2. 执行命令
  3. 从腻子复制结果并将其粘贴到excel中。

So far I have written the code for logging into the server by initiating the putty Application.(Refer the code below)

到目前为止,我已经编写了通过启动 putty 应用程序登录服务器的代码。 (参考下面的代码)

Sub Putty()
  Dim TaskID As Long
  TaskID = Shell("path/putty.exe servername", vbNormalFocus)
  AppActivate TaskID, True
  SendKeys "username"
  SendKeys "{ENTER}"
  SendKeys "password"
  SendKeys "{ENTER}"
  '---- Here i need to execute a command for eg: "ls -l" and copy the result into Excel
End Sub

Can you please give some idea about how to execute a command after this step and to copy the result from PuTTy to Excel?

您能否提供一些有关如何在此步骤之后执行命令并将结果从 PuTTy 复制到 Excel 的想法?

回答by AndASM

I would use Plinkinstead of the main Putty application. Plink is putty designed for the command-line.

我会使用Plink而不是主要的 Putty 应用程序。Plink 是为命令行设计的腻子。

Look into shell redirection, answers such as this here.

查看 shell 重定向,这里的答案如下。

The basic idea is you can create a WScript.Shell object, run plink.exe using it, then write to StdIn and read from StdOut. StdIn is like typing text into a console window and StdOut is what gets displayed to the user in said console window.

基本思想是您可以创建一个 WScript.Shell 对象,使用它运行 plink.exe,然后写入 StdIn 并从 StdOut 读取。StdIn 就像在控制台窗口中输入文本一样,而 StdOut 是在所述控制台窗口中向用户显示的内容。

回答by user6618942

You can specify a log file in putty and watch that log file as you go. It's not the best way to do it, but without admin rights it is one way of doing it.

您可以在 putty 中指定一个日志文件,并随时查看该日志文件。这不是最好的方法,但没有管理员权限,这是一种方法。

回答by Flint

It would run better if you add application wait timer in the script, sendkeys is not very reliable function.

如果在脚本中添加应用程序等待计时器会运行得更好,sendkeys 不是很可靠的功能。