如何从 VBA 宏中使用 PuTTY 调用和执行 shell 脚本 [英] How to call and execute a shell script using PuTTY from VBA macro

查看:120
本文介绍了如何从 VBA 宏中使用 PuTTY 调用和执行 shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 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 of 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 应用程序.

      And you should consider using plink.exe instead of putty.exe, what is a PuTTY tool intended to automation. PuTTY is GUI application intended for an interactive use.

      这篇关于如何从 VBA 宏中使用 PuTTY 调用和执行 shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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