Inconsistent SSH behavior (General questions)

by Herut @, (5489 days ago)

Hi,

I am trying to run background process and get his process ID.
Part of the time my code is working well, and part of the time I can't get the process ID.

Here is my code:
[code]
public string Execute(string command)
{
if (sshConnect.State != SSH.States.Connected)
return Not Connected ;

answer = sshConnect.Execute(command +
);
return answer;
}

public int BackgroundProcess(string command, ref string pAnswer)
{
Execute(command + & );

pAnswer = answer;
string[] lines = answer.Split(new string[] {
}, StringSplitOptions.None);
foreach (string line in lines)
{
if (line.StartsWith( [ ) && line.Contains( ))
return Convert.ToInt32(line.Substring(line.IndexOf( ))); // returns process ID
}

// process ID yet didn't find - no process ID
return 0;
}
[/code]

Too many times the process is running, but I can't get his process ID.

Can you help please? Should I use other SSH functions?

Thanks.

locked

Re: Inconsistent SSH behavior

by woddrazen @, (5489 days ago) @ Herut

Hi Herut,


Inside your Execute Method line in your code I don't see that you catch prompt that should be received after command is executed.

Can you please try something like this
[code]answer = sshConnect.Execute(command + \r\n , next_prompt);[/code]
That could solve your problem.

Let us know how it goes.


Regards,
Drazen

locked

Re: Inconsistent SSH behavior

by Herut @, (5489 days ago) @ woddrazen

Hi Drazen,

Isn't it enough that I wrote this in the constructor?
[code]
sshConnect.Prompt = PROMPT ;
[/code]
Should I again write this?
[code]
answer = sshConnect.Execute(command +
, sshConnect.Prompt);[/code]

Thanks,
Herut

locked

Re: Inconsistent SSH behavior

by woddrazen @, (5489 days ago) @ Herut

Herut,


Yes, you should specify prompt inside Execute Method.

Let us know how it goes.


Drazen

locked

Re: Inconsistent SSH behavior

by Herut @, (5489 days ago) @ woddrazen

Drazen,

I checked it again and again. Execute waits only for
.
It goes so fast, he can't get the process ID, and again I get the 0 answer...

Any suggestions?
Herut

locked

Re: Inconsistent SSH behavior

by woddrazen @, (5489 days ago) @ Herut

Herut,


When you use Prompt Property you should wait for result inside PromptReceived Event.

Maybe you should switch to WaitFor and Execute Method in blocking mode. Can you maybe try something like this:

http://example.weonlydo.com/index.asp?did=Send+multiple+commands+to+SSH+server+using+sy...

You can try with regex value for a prompt in WaitFor and Execute line but you can also add there your direct prompt value.

Let us know how it goes.


Drazen

locked