I'm making a program in VB.NET that allows you to start putty, log in automatically and execute commands automatically.
So far I have managed to open putty with the following instruction, I do not know if it is correct but it works.
Process.Start("C:\Program Files\PuTTY\putty.exe", "-ssh admin@192.168.1.34 22 -pw admin)
But when it comes to connecting to a new server, putty throws up a confirmation window. I have managed to skip that confirmation window, but with delays applied
Threading.Thread.Sleep("500")
SendKeys.Send("{RIGHT}")
SendKeys.Send("{RIGHT}")
SendKeys.Send("{ENTER}")
Threading.Thread.Sleep("1000")
SendKeys.Send("admin")
SendKeys.Send("{ENTER}")
SendKeys.Send("admin")
SendKeys.Send("{ENTER}")
And that way I send commands to Putty The problem arises when, I must send a command to restart the server and then restart log back in and continue executing commands. For example 10 commands until the first restart and after the restart 15 commands.
As you can see, to execute the commands I am using sleep() to prevent it from doing so at the speed of light.
So far I have not managed to make VB.NET understand that the server is restarting and wait X time to log back in and continue sending commands, since the restart time is variable.
On the other hand this of using sleep() before executing certain commands, does not seem to me a good practice, I have seen that in some cases it works and in others it does not and generates an error in Putty.
In the end my code is staying like this ...
Process.Start("C:\Program Files\PuTTY\putty.exe", "-ssh admin@192.168.1.34 22 -pw admin")
Threading.Thread.Sleep("500")
SendKeys.Send("{RIGHT}")
SendKeys.Send("{RIGHT}")
SendKeys.Send("{ENTER}")
Threading.Thread.Sleep("1000")
SendKeys.Send("admin")
SendKeys.Send("{ENTER}")
SendKeys.Send("admin")
SendKeys.Send("{ENTER}")
Threading.Thread.Sleep("1000")
SendKeys.Send("ping 192.168.1.34 count 20")
SendKeys.Send("{ENTER}")
RIGHT, I'm using to skip the confirmation window that PUTTY launches when connecting to a new server.
I would appreciate it if you could comment if I am using the correct way to open an SSH session in Putty mendiante VB.NET and how to send commands.