我正在尝试编写一个VBA宏,它将使用PuTTY登录并执行命令。以下代码已被使用。
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' - 找不到文件'。
请帮忙。
答案 0 :(得分:2)
你必须引用PuTTY的路径,因为它包含空格。
您也执行putty.exe
两次。相反,您必须将所有这些参数传递给putty.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应用程序。
答案 1 :(得分:0)
需要引用带空格的文件路径:
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