How to execute commands one by one - WeOnlyDo Discussion board

How to execute commands one by one (wodSSH / wodSSH.NET)

by lax4u, Tuesday, July 03, 2007, 00:31 (6354 days ago)

How my code should look like if i want to send commands one by one after connected.
I have following code to connect.

public void DoWork()
{

WODTELNETDLXCOMLIB.wodTelnetDLXCom telnet1 = new WODTELNETDLXCOMLIB.wodTelnetDLXCom();
telnet1.Timeout = 30;
telnet1.Connected += new WODTELNETDLXCOMLIB._IwodTelnetDLXComEvents_ConnectedEventHandler(telnet1_Connected);
telnet1.PromptReceived += new WODTELNETDLXCOMLIB._IwodTelnetDLXComEvents_PromptReceivedEventHandler(telnet1_PromptReceived);
telnet1.Disconnected += new WODTELNETDLXCOMLIB._IwodTelnetDLXComEvents_DisconnectedEventHandler(telnet1_Disconnected);

telnet1.Blocking = true;
telnet1.Login = user ;
telnet1.Password = password ;
telnet1.Connect(IPADDRESS, 23, WODTELNETDLXCOMLIB.ProtocolsEnum.Telnet);
if (this.isConnected)
{

// here i want to execute commands one by one. how do i do that.
// when i login i get screen which shows Press any key to continue and i have to hit enter key to goto next screen
// on next screen i get some other text plus prompt Code: where i have to enter command 'DTL' and then enter.
// on next screen i get : where i have to execute command 'sys' to go to unix prompt
// so on...

can somebody show me sequence of code in C# to achieve this.

}

}

Re: How to execute commands one by one

by wodSupport, Tuesday, July 03, 2007, 01:38 (6354 days ago) @ lax4u

Laxu,

I would suggest you use WaitFor and Execute methods for that.

You would do something like

Telnet1.Waitfor( press any key to continue );
Telnet1.Execute( ls -al\r\n , regex:[\$ #>] $ );

etc. Just make sure 2nd parameter to Execute call is expected command prompt. Regex match from above should catch most of UNIX prompts.

Hope that helps!

Kreso

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 06:54 (6354 days ago) @ wodSupport

see when i coonect using windows telnet screen after connecting succussfully i get screen which at the bottom says Press Any Key Continue . So to by pass this screen i have to send enter which is /r/n right? then i get one more screen which has buch of text and also a prompt says Code: and here i have to type 'DTL' and enter. Remeber this is yet not Unix screen. After this i get screen with : , where i have to type sys to goto Unix screen.

so tell me if this is right
Telnet1.Waitfor( press any key to continue );
Telnet1.Execute( \r\n , Code: ,timeout);
Telnet1.Waitfor( Code: );
Telnet1.Execute( DTL , : ,timeout);
Telnet1.Waitfor( : );
Telnet1.Execute( sys , $ ,timeout);
Telnet1.Waitfor( $ );

Laxu,

I would suggest you use WaitFor and Execute methods for that.

You would do something like

Telnet1.Waitfor( press any key to continue );
Telnet1.Execute( ls -al\r\n , regex:[\$ #>] $ );

etc. Just make sure 2nd parameter to Execute call is expected command prompt. Regex match from above should catch most of UNIX prompts.

Hope that helps!

Kreso

Re: How to execute commands one by one

by woddrazen, Tuesday, July 03, 2007, 08:13 (6354 days ago) @ lax4u

Hi Laxu,


Please try something like this:
[code]
Telnet1.Waitfor( press any key to continue );
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute(
, Code: ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( DTL
, : ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( sys
, $ ,30);
[/code]

If problem persist maybe we can connect to your server and find out what went wrong. You can send you private information to techsupport@weonlydo.com

Let us know how it goes.


Regards,
Drazen

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 15:27 (6354 days ago) @ woddrazen

That screen will have other text also, not only Press any key to continue . Also for enter key should i use rn or /r/n

Hi Laxu,


Please try something like this:
[code]
Telnet1.Waitfor( press any key to continue );
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute(
, Code: ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( DTL
, : ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( sys
, $ ,30);
[/code]

If problem persist maybe we can connect to your server and find out what went wrong. You can send you private information to techsupport@weonlydo.com

Let us know how it goes.


Regards,
Drazen

Re: How to execute commands one by one

by woddrazen, Tuesday, July 03, 2007, 15:53 (6354 days ago) @ lax4u

Laxu,


You should use \r\n

[code]
Telnet1.Waitfor( press any key to continue );
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( \r\n , Code: ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( DTL\r\n , : ,30);
Telnet1.DataReady = 0;
Console.Write Telnet1.Execute( sys\r\n , $ ,30);
[/code]

Let us know how it goes.


Drazen

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 15:59 (6354 days ago) @ woddrazen

but how the regular expression will look like for press any key to continue . or there wont be any reg expression in this case. The screen has other text with Press any key to continue is the last line

Re: How to execute commands one by one

by wodSupport, Tuesday, July 03, 2007, 16:03 (6354 days ago) @ lax4u

Lax,

regular expressions are used only when you prepend waitfor with 'regex:'. If you don't put that in front, then exact match is used.

BTW you don't have to wait for full line, you can waitfor for anything that will arrive, even part of the line, even one byte if that's safe enough for you.

Drazen's sample was nice - he was waiting for part of the 'press any key' line and then just set DataReady=0 to remove any leftovers from the queue (such as newline chars).

Regards,
Kreso

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 16:19 (6354 days ago) @ wodSupport

if execute above code, it timeouts at
Telnet1.Execute(
, Code: ,30);

Re: How to execute commands one by one

by wodSupport, Tuesday, July 03, 2007, 16:26 (6354 days ago) @ lax4u

Lax,

that simply means that 'Code:' that you expected to arrive never really arrived.

So, things are not coming as you expect - let's see what arrived. Instead of yuor line, can you do this:

Telnet1.Send( \r\n );
Debug.Print Telnet1.Receive()

perhaps you would need to put more than one Receive to see all the data.

Also, some UNIX systems prefer to receive only \n instead of \r\n - so perhaps you can try to adjust that too.

Kreso

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 17:10 (6354 days ago) @ wodSupport

i did something like this. it will find Token Code: and then i send DTL + /r/n which is LF constant in my case. and check for : but it never find :


screen = telnet1.WaitFor( Press any key to continue , timeout);
telnet1.DataReady = 0;
telnet1.Send(LF);
if (!WhileTokenNotFound( Code: ))
{

telnet1.Send( DTL + LF);
telnet1.DataReady = 0;
screen = string.Empty;
if (!WhileTokenNotFound( : ))
{
telnet1.DataReady = 0;
screen = telnet1.Execute( sys + LF, $ , timeout);
}
}

private bool WhileTokenNotFound(string Token)
{
bool notFound = true;
while (notFound)
{
if (screen.IndexOf(Token) == -1)
{
screen = telnet1.ReceiveLine();
AddToList(screen);
}
else
{
notFound = false;
}
}
return notFound;
}


Re: How to execute commands one by one

by wodSupport, Tuesday, July 03, 2007, 17:41 (6354 days ago) @ lax4u

Lax,

ok, assuming \r\n is your LF, wht do you receive in 'Received' line?

I would change ReceiveLine to Receive - perhaps ReceiveLine never returns because there's no full line inside?

Also, change screen = ... to screen = screen + ...., it's better to add to your buffer so if data arrives partially you still receive it. When you see your pattern, empty screen variable.

So, all is there but you're still missing last : ? If you, can you try to send only \n instead of \r\n sequence? Cisco doesn't like \r\n, perhaps this is simple problem.

Kreso

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 17:41 (6354 days ago) @ wodSupport

using above logic i come to the point where i want to send file.
how do i do that. What is the third parameter in SendFile.
telnet1.SendFile(WODTELNETDLXCOMLIB.TransferProtocolsEnum.ZMODEM, C:/MyTestFile.txt ,????);

Re: How to execute commands one by one

by wodDamir, Tuesday, July 03, 2007, 17:57 (6353 days ago) @ lax4u

Hi Lax,

Kreso responded to your last question, so i'll proceed with SendFile question.

The third parameter in SendFile Method is ResumeTransfer parameter. It accepts True or False, True if you wish to allow transfer to be resumed, False if you wish to overwrite the existing file.

You should also send rz command to the server prior to using SendFile in order to tell server to accept a file.

You can find more information about SendFile Method on the following link: http://www.weonlydo.com/TelnetDLX/Help/WODTELNETDLXLIB~wodTelnetDLX~SendFile.html

Hope I helped.

Regards,
Damba

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 18:15 (6353 days ago) @ wodDamir

Thank you very much guys. It wotked, im able to transfer file finally. The only thing, Execute command doesnt work for me, dont know why. So i have keep reading line by line untill i find Token and then Send the command. My working code look like this.. any improvement u suggest

telnet1.Connect(IPADDRESS, 23, WODTELNETDLXCOMLIB.ProtocolsEnum.Telnet);
screen = telnet1.WaitFor( Press any key to continue , timeout);
telnet1.DataReady = 0;
telnet1.Send(LF);
if (!WhileTokenNotFound( Enter a function code ))
{
telnet1.Send( DTL +LF);
if (!WhileTokenNotFound( : ))
{
telnet1.Send( sys );
if (!WhileTokenNotFound( $ ))
{
telnet1.Send( cd /ppp/client/test );
if (!WhileTokenNotFound( $ ))
{
telnet1.Send( rz + LF);
telnet1.SendFile(WODTELNETDLXCOMLIB.TransferProtocolsEnum.ZMODEM, C:\myTestFile.txt , false);
}
}
}
}

private bool WhileTokenNotFound(string Token)
{
screen = string.Empty;
bool notFound = true;
while (notFound)
{
if (screen.IndexOf(Token) == -1)
{
screen = telnet1.ReceiveLine();
telnet1.Send(LF);
AddToList(screen);
}
else
{
notFound = false;
}
}
return notFound;
}

Re: How to execute commands one by one

by wodDamir, Tuesday, July 03, 2007, 18:26 (6353 days ago) @ lax4u

Lax,

Great, I'm glad everything now works as expected. As for the code, I don't have any suggestions about improving.

However, if you run into any other issue related to component, feel free to contact us.

Regards,
Damba

Re: How to execute commands one by one

by lax4u, Tuesday, July 03, 2007, 20:56 (6353 days ago) @ wodDamir

Hey i have one more question. Since i dont know how large the return string will be when i use Telnet.Receive method. What parameters should i pass?
i tried
object obj = telnet1.Receive(null, null);
receivedData = obj.ToString();

and this is working, but is that okay to pass null values.
I really appriciate you guys for giving me quick response.

Re: How to execute commands one by one

by wodDamir, Tuesday, July 03, 2007, 21:10 (6353 days ago) @ lax4u

Hi Lax,

You can use null values, wodTelnetDLX will use default values in that case. Also, you can use DataReady Property for the Count parameter (DataReady Property holds total numbers of bytes to be received).

Hope I helped.

Regards,
Damba