VBA Excel 将 echo y 与 PLink 结合使用的代码

VBA Excel code using echo y with PLink

警告:**如果您正在使用不受信任的 DEVICES/IP,请确保您不使用此方法。 您不想自动缓存您不知道的 Ssh 主机密钥。保重。

我的代码的目的是从 VBA Excel 运行 通过 plink.exe ssh 获取给定的 IP 列表。我只是在检查 SSH 连接和 IP 列表是动态的。

我正在尝试在 运行ning ssh 使用 plink.exe 时通过 yy 的原因是因为第一次 PLink (PuTTY) 要求缓存 IP。

Echo y 从命令提示符自动执行此操作,运行 如下所示。

C:\>echo y | Users\Admin\Desktop\plink.exe -ssh 10.0.0.1

命令通过 y 并且 IP 被缓存,这使得我的代码自动化并且代码循环遍历整个列表。

我无法在 VBA excel(工具所在的位置)中执行相同的命令,需要有关如何实施它的指导。请指出我哪里出错了。

Dim strShellCommand As String
Dim filename As String
Dim Run As String
Dim a As String
Dim b As String

filename = Sheet1.Cells(8, 2).Value
a = "echo y |"
b = "-ssh"

' Comments!!
' filename from cell = "C:\Users\Admin\Desktop\plink.exe"
' echo y | C:\Users\Admin\Desktop\plink.exe -ssh 10.0.0.1
' strCompaddress is any IP

Run = a & " " & filename & " " & b & " " & strCompAddress

Set osh = CreateObject("Wscript.Shell")
Set oEx = osh.Exec(Run)

不要试图绕过 SSH 主机密钥的验证。就在那里 by purpose for your own security:

This is a feature of the SSH protocol. It is designed to protect you against a network attack known as spoofing: secretly redirecting your connection to a different computer, so that you send your password to the wrong machine. Using this technique, an attacker would be able to learn the password that guards your login account, and could then log in as if they were you and use the account for their own purposes.


改用 -hostkey 开关来提供预期主机密钥的指纹。

3.8.3.20 -hostkey: manually specify an expected host key

This option overrides PuTTY's normal SSH host key caching policy by telling it exactly what host key to expect, which can be useful if the normal automatic host key store in the Registry is unavailable. The argument to this option should be either a host key fingerprint, or an SSH-2 public key blob. See section 4.20.2 for more information.

You can specify this option more than once if you want to configure more than one key to be accepted.

请注意 -hostkey 开关是在 PuTTY/PLink 0.64 中引入的。


如果您使用代码来测试连接性,为什么需要接受主机密钥?服务器能够提供主机密钥这一事实本身就是连通性的证明。


如果您真的使用您的代码来缓存主机密钥,那么 “他们没有点击 'yes' 为每个 IP 缓存它,“ 不要。这是绝对不能接受的。不仅如此,您还破坏了自己的安全感。您故意破坏其他用户的安全,这些用户会在不知不觉中信任您盲目接受的任何主机密钥。

预缓存主机密钥的唯一正确方法是将已知的主机密钥导入注册表。

个人验证(我是认真的)主机密钥从您的注册表导出到一个文件,例如SshHostKeys.reg:

[HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\SshHostKeys]
"rsa2@22:example.com"="0x23,0xab603b8511a67679bdb540db3bd2034b004ae936d06be3d760f08fcbaadb4eb4edc3b3c791c70aae9a74c95869e4774421c2abea92e554305f38b5fd414b3208e574c337e320936518462c7652c98b31e16e7da6523bd200742a6444d83fcd5e1732d03673c7b7811555487b55f0c4494f3829ece60f94255a95cb9af537d7fc8c7fe49ef318474ef2920992052265b0a06ea66d4a167fd9f3a48a1a4a307ec1eaaa5149a969a6ac5d56a5ef627e517d81fb644f5b745c4f478ecd082a9492f744aad326f76c8c4dc9100bc6ab79461d2657cb6f06dec92e6b64a6562ff0e32084ea06ce0ea9d35a583bfb00bad38c9d19703c549892e5aa78dc95e250514069"

并将它们导入目标机器。

例如使用:

reg import SshHostKeys.reg

如果您真的不关心安全性,例如因为您在专用网络中连接,请使用:

Run = Environ("COMSPEC") & " /c echo y | " & filename & " -ssh " & strCompAddress

(假设 filenameplink.exe 的路径)

要使输入重定向工作,您必须通过 shell 解释器 (cmd.exe) 运行 该过程。环境变量 COMSPEC 指向它(通常是 C:\WINDOWS\system32\cmd.exe)。

另见 Redirecting input to an executable from Excel VBA