I hope my reply is informative, and also leads to the additional answers.
Like already mentioned, pLink is the command line way to do this, as puTTY is designed for interactive use.
In a previous position, I was able to use SecureCRT which has its own internal scripting language. I could pass a list of commands through that quite nicely (with all kinds of prompt waiting for commands to complete). I could even have the SecureCRT code do it's own conditional switching, when output wasn't as expected. If I could talk my current employer into buying SecureCRT, I'd be doing it that way.
Here is how I've been able to do this:
Sub testpLinkIO()
Dim WshShell As Object, oExec As Object, sOutput$
Set WshShell = VBA.Crea****ject("WScri**.**ell")
' **** is teOb (something in this code gets kicked out by the forum as an attempted exploit)
' **.** is pt.Sh
Set oExec = WshShe**.**ec("plink -x -a -t [email protected] -pw ******") ' This exec command can be built by passing it variables however you like
' **.** is ll.ex (just to be sure that isn't what's getting this post blocked)
oExec.StdIn.Write "su - user" & vbCr
oExec.StdIn.Write "grep -i " & Chr(34) & "took" & Chr(34) & " ~/data.log | awk '{print $1,$2,$3,$9,$15,$21}'" & vbCr ' This one required embedded dbl-quotes
oExec.StdIn.Write "tail /home/user/data.log" & vbCr
oExec.StdIn.Write Chr(4) & Chr(4) ' Everything is hung until the shell exits - Ctrl-D ends a Linux shell and since I'm in an extra layer deep, I have to exit twice.
' Every Write needs its own vbCr (hitting enter for the command)
' You could build and load an array of commands and loop them here
' So far, I know this goes bonkers if you send commands too fast - there isn't a way to wait for the prompt
sOutput = oExec.StdOut.ReadAll() ' Once the shell exits, we have data
Debug.Print sOutput
End Sub
I have a few problems with this myself...- I must exit out of the shell to be able to see and manipulate the results.
- If I want to do something in response to the output, I have to go back in with new commands.
- This is not interactive by any means. If one of these commands hangs, it all dies.
- My bash shell assumes a tty with ANSI features, so the output has escape codes that are meaningless to pLink (but might be able to be ignored).
It would be great if there is some kind of dynamic way to communicate to plink, or maybe an ssh environment that just runs in VBA.
So, I hope this helps, and gets more complete answers for us all.
Bookmarks