0

some context first, Im using vba to automate putty into loging into many host with ssh retrieve info and save it.

I can do that currently, however, each time I execute a command, I have to set some waiting time just so the command is correctly executed and then I can execute next command. Is there any way to feeback from Putty output into vba so vba knows when to send next command? that would reduce execution time

sub retrievinginfo() 

Lines to ssh open putty to X host  

SendKeys "first command for putty session"

Application.wait Now+Timeserial(0,0,"estimated seconds") '<---- here the problem

SendKeys "second command for putty session"

end

Fingo
  • 1

1 Answers1

0

Do not use PuTTY. That's a GUI application, not intended for automation.

Use Plink from PuTTY package. Plink is a console application intended for automation. It uses standard input/output, so that you do not have to hack sending of command by simulating keystokes.

Why do you even execute command this way? Why don't you specify a script file using -m switch?

See Automating command/script execution using PuTTY.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Hi! thanks! I'm working on a citrix session within customer network and I have no access to any other tools than the ones already installed (I cannot install anything, not even can I run "cmd" directly, I had to invoke shell using vba in xls). So, I should stick to vba+putty. – Fingo Nov 29 '18 at 07:23
  • Plink comes with PuTTY. It's the same package. – Martin Prikryl Nov 29 '18 at 07:25
  • Really, I tried many examples posted here and it would not work, somehow the customer disabled it. The Putty folder does show "PLINK" but whenever I click on it, it sends me a message saying the file cant be found and the shorcut will be removed. Or if I try to use a bat to execute plink (just to test if it's there somewhere), it wont even open the promt. – Fingo Nov 29 '18 at 08:16
  • OK. What about using the `-m` switch? Do you really have to wait between the commands? – Martin Prikryl Nov 29 '18 at 08:19
  • I tried too, but this is another detail: the initial connection to the host is done thru ssh, from there you have to go to a TL1 session and it is there where you have to send a new authentication and then the TL1 commands. So, TL1 interface does not really work like common CLIs, if I create a file witht he commands and then use the -m switch, when putty pulls them it will only "paste" them, but it wont be executed, so no useful output is received. – Fingo Nov 29 '18 at 08:34
  • OK, then as the last resort option, you can enable session output logging in PuTTY and monitor the log for some keywords, to decide when to send the next keystrokes. – Martin Prikryl Nov 29 '18 at 08:36
  • Yes... well, as you can see, the resources are very limited and I do not have much operation margin. The way I'm running it currently does not really allow me to open the putty log file to monitor it, because it's being used to save the current output: the script is writing on it as the commands are executed, so I cannot open the file and check if the command was already executed, because that would take me back to first stage (when should I look? how much time I should wait?). But I really appreciate you took the time to take a look at this. Thanks a bunch! – Fingo Nov 29 '18 at 09:14
  • *"the script is writing on it as the commands are executed, so I cannot open the file and check if the command was already executed"* - I do not understand why you cannot. – Martin Prikryl Nov 29 '18 at 09:18
  • Because I would have to open the log, right? but it's being written by putty, so: putty executes command and starts writing the log, if I open the log thru vba to monitor, for that fraction of time, putty wont be able to write on the log file, until I close it, correct? that's what happens when I manually open the log: while it's open, putty cannot write on it, even when the putty commands are executed. So I would need another timer to wait for the command to complete and then open the log and check. But timers is precisely what I'm trying to avoid. – Fingo Nov 30 '18 at 07:21
  • PuTTY has the log file open for writing for whole duration of the session. So there's no way you can prevent it from continuing with writing (of course, unless you open the log file with a write lock even before PuTTY starts logging). – Martin Prikryl Nov 30 '18 at 07:32