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