How to call and execute a shell script using PuTTY from VBA macro

拈花ヽ惹草 提交于 2019-12-08 04:05:36

问题


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

Script throws an error saying

'Run-time error '53' - File not found'.

Please help.


回答1:


  • You have to quote the path to PuTTY, as it contains spaces.

  • 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)

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.




回答2:


File paths with spaces need to be quoted:

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


来源:https://stackoverflow.com/questions/45475168/how-to-call-and-execute-a-shell-script-using-putty-from-vba-macro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!