running putty in c# and accepting hostkey

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
I'm trying to open putty and to automatically connect to a server. The problem is that i've managed to open the putty with the server i need, now i've spent all night trying to figure it out how to press the freaking YES button. can somebody PLEASE tell me how to do it, because so far couldn't get it to work. Also use bvssh, and the same problem there too, can't press the accept button, managed to open it but can't press the accept button.

Thanks.
 

Вложения

rostonix

Известная личность
Регистрация
23.12.2011
Сообщения
29 067
Благодарностей
5 705
Баллы
113
  • Спасибо
Реакции: awzse00

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Still doesn't works. Here's the code maybe i'm doing something wrong. It opens putty enters everything, but it still doesn't push the YES button.
To test it it requires putty to be in system32

Код:
System.Diagnostics.Process cmd = new System.Diagnostics.Process();

cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;

cmd.Start();

cmd.StandardInput.WriteLine("putty -N -D 9000 -l ubnt -pw ubnt 67.222.195.227");

string result = Emulator.MouseClick("PuTTy Security Alert", MouseButton.Left, MouseButtonEvent.Click, 200, 200);
System.Threading.Thread.Sleep(2000);
if (result == "ok" && !Emulator.ErrorDetected)
{
    // click on button with caption "Yes"
    result = Emulator.ButtonClick("Yes", "Yes");
    // make answer
    if (result == "ok" && !Emulator.ErrorDetected) return "All done";
    else return "Fail";
}
 
  • Спасибо
Реакции: Anfim777

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113
You have activate the putty window before doing mouse click in there I believe.

Код:
Emulator.ActiveWindow("PuTTy Security Alert");
 
  • Спасибо
Реакции: awzse00

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
I'm running the code and it says it works and it selects the putty window

Код:
string result = Emulator.MouseClick("PuTTy Security Alert", MouseButton.Left, MouseButtonEvent.Click, 200, 200);
Putty window name is "PuTTy Security Alert" and in that window click the YES button. Can't find out what i'm doing wrong.

L.E.: My c# knowledge is nearly NONE, so far i've done it with trial and error, can't say i understand very much from what i've done.
 
Последнее редактирование:

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113
And have you used Jruler or some other screen measuring tool to make sure coordinates for click on button is on target?
 
  • Спасибо
Реакции: awzse00

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Got the coordonates with Jruler.

Код:
MouseButtonEvent.Click, 164, 273
Can you please tell me what's the c# to wait for the window to open and then click the button. I'm thinking that he's clicking on the YES button, before the window becomes active, not after it shows up
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113
Just put a longer pause in there. System.Threading.Thread.Sleep(10000); That will give you 10 seconds instead of the 2 you have in there
 
  • Спасибо
Реакции: awzse00

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Just put 20 sec. and still nothing, jesus christ it's eating my brain a YES button.
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
nobody know how to fix this ? PLEASEEEE HELP ME OUT I'M dying. Second I idea, how do I make it to press (LEFT) and then (ENTER) in the window putty .
This is the code so far, it works, but it just doesn't want to press enter.

Код:
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.StandardInput.WriteLine("putty -N -D 9000 -l ubnt -pw ubnt 67.222.195.227");
System.Threading.Thread.Sleep(6000);
Emulator.ActiveWindow("PuTTy Security Alert");
System.Windows.Forms.SendKeys.Send("{ENTER}");
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Fianlly, got it working,
Код:
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.StandardInput.WriteLine("putty -N -D 9000 -l admin -pw default 216.106.125.114");
System.Threading.Thread.Sleep(6000);
System.Windows.Forms.SendKeys.SendWait("Y");
 
  • Спасибо
Реакции: bigcajones

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
681
Баллы
113
Awesome dude. Glad you finally got it worked out.
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Now the question is why in test mode works just fine, and in project it shows that runs but nothing happens
 

awzse00

Client
Регистрация
16.03.2013
Сообщения
231
Благодарностей
36
Баллы
28
Ok, here's the template, and if anybody has the time to take a look over it would be great.
Please note that you have to put putty.exe in windows/system32 in order to run ,or the direct path to the putty where you have it.
Now, if you open the c# and click on test, it opens everything, clicks on YES works like a charm, when you click save and close, and try to run the project, it says that macro has been successfully run and nothing, doesn't open putty doesn't do anything, just OK. Now at this point I'm really clueless about what to do. Any hint would be greatly appreciated and I owe you guys a beer.

L.E.: just remove "Take Line Tunele", there's already one inside, working.
 

Вложения

  • Спасибо
Реакции: avtostopshik

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)