如何从 VBA 宏中使用 PuTTY 调用和执行 shell 脚本 [英] How to call and execute a shell script using PuTTY from VBA macro
问题描述
我正在尝试编写一个 VBA 宏,它将使用 PuTTY 登录并执行命令.下面的代码已被使用.
I am trying to write a VBA macro which will login using PuTTY and execute commands. The below code has been used.
Sub open_putty()
Dim UserName 'assign user name
Dim Passwrd 'assign password
Dim TaskID As Long
UserName = "user name"
Passwrd = "password"
pc1 = "C:\Program Files (x86)\PuTTY/putty.exe -ssh " & UserName & "@ip address -pw " & Passwrd
pc2 = "putty.exe -m ""C:\Temp\emu.sh"""
TaskID = Shell(pc1, 1)
TaskID = Shell(pc2, 1)
End Sub
脚本抛出错误提示
'运行时错误'53' - 找不到文件'.
'Run-time error '53' - File not found'.
请帮忙.
推荐答案
您必须引用 PuTTY 的路径,因为它包含空格.
You have to quote the path to PuTTY, as it contains spaces.
你也执行了两次
putty.exe
.相反,您必须将所有这些参数传递给putty.exe
Also you execute
putty.exe
twice. Instead you have to pass all those parameters to a single instance ofputty.exe
pc1 = """C:\Program Files (x86)\PuTTY\putty.exe"" " & _ "-ssh " & UserName & "@ip address -pw " & Passwrd & " -m ""C:\Temp\emu.sh""" TaskID = Shell(pc1, 1)
你应该考虑使用
plink.exe
而不是putty.exe
,这是一个旨在自动化的 PuTTY 工具.PuTTY 是用于交互式使用的 GUI 应用程序.And you should consider using
plink.exe
instead ofputty.exe
, what is a PuTTY tool intended to automation. PuTTY is GUI application intended for an interactive use.这篇关于如何从 VBA 宏中使用 PuTTY 调用和执行 shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!