Login, su to root, execute command (General questions)

by Jasmine, (7523 days ago)

We have many users ask something very similar to: how do I login to a server, 'su -' to root, and then execute some command? Since regular expression is supported on both ActiveX and NET versions, you can use below code (with very small changes) on both of them. We use Blocking mode in below sample. Change Hostname, Login, Password and RootPass variables with your data.

Sample will connect to the server and give you list of current folder in a messagebox.
[code]Sub Main()
Dim Ssh1 As New WeOnlyDo.Client.SSH

Dim hostname As String, login As String, password As String, rootpass As String

Hostname = put_your_host
login = put_your_login
password = put_your_pass
rootpass = put_root_pass

Ssh1.Hostname = hostname
Ssh1.Login = login
Ssh1.Password = password
Ssh1.Blocking = True
Ssh1.Connect()

' once connected, wait for prompt
Ssh1.WaitFor( regex:[$ #>] $ , 5)

' if anything left in incoming buffer, delete it
If Ssh1.DataReady > 0 Then Ssh1.Receive()

' execute su command, wait for password prompt
Ssh1.Execute( su - & vbLf, Password: , 5)

' send password and wait for command prompt
Ssh1.Send(rootpass & vbLf)
Ssh1.WaitFor( regex:[$ #>] $ , 5)

' if anything left in incoming buffer, delete it
If Ssh1.DataReady > 0 Then Ssh1.Receive()

' execute command and dump it's output to a messagebox
MsgBox(Ssh1.Execute( ls -al & vbLf, regex:[$ #>] $ , 10))

' and disconnect
Ssh1.Disconnect()
End Sub[/code]

locked

Re: Login, su to root, execute command

by M @, (7522 days ago) @ Jasmine

Thanks a lot for the example you provided me.

I've just one more question:

when I use the command “Ssh1.WaitFor( regex:[$ #>] $ , 5)” I have to know in advance what prompt to expect (regex:[$ #>] $).

What shall I do if I don't know what the prompt will be like ? (the system administrator could change it)

I tried to set it from my application with a command export PS1= .... but with no success.

Is it possible to adjust your example to manage this problem?


locked

Re: Login, su to root, execute command

by Jasmine, (7522 days ago) @ M

M,

that's the best part - it will most probably work. Regular expression

$ #>]

means it will accept any of those chars ($, ,#,>) if they appear at the end of the line. This includes 99.9 of most UNIX command prompts (including root account). Why don't you give it a try?

locked

Re: Login, su to root, execute command

by M @, (7522 days ago) @ Jasmine

M,

that's the best part - it will most probably work. Regular expression

$ #>]

means it will accept any of those chars ($, ,#,>) if they appear at the end of the line. This includes 99.9 of most UNIX command prompts (including root account). Why don't you give it a try?

Sorry,
the command WaitFor doesn't translate regular expressions.
It always looks for the exact string.
If I use the command locSSH.WaitFor( [$ #>] $ , 5) it looks for a prompt like this [$ #>] $
My prompt is $PWD > , how can I do?

Thanks, bye

M

locked

Re: Login, su to root, execute command

by Jasmine, (7522 days ago) @ M

M,

in newest wodSSH it does work, I am sure. But you must put 'regex:' text before your pattern, otherwise it is treated as exact text. Notice we also put regex: in the sample.

locked

Re: Login, su to root, execute command

by M @, (7522 days ago) @ Jasmine

M,

in newest wodSSH it does work, I am sure. But you must put 'regex:' text before your pattern, otherwise it is treated as exact text. Notice we also put regex: in the sample.

Thanks a lot,
I wasn't using the last version of the component [:doh:]
Now it's ok.

Boban for president [:wink:]

locked

Re: Login, su to root, execute command

by Jasmine, (7522 days ago) @ M

Boban for president? hehe... could be, who knows..

Anyway I'm glad all works ok now!

Regards.

locked

Re: Login, su to root, execute command

by iyikedi @, (7393 days ago) @ Jasmine

This code working with VB yes.
but we need exp code in ASP
please help us...
[:sad:]

locked

Re: Login, su to root, execute command

by Jasmine, (7393 days ago) @ iyikedi

It should work in ASP the same way. It's important to use Blocking = 1 and that's it. What happens, why it doesn't work? Where does it fail? Can you cut it after Connect, and then add line by line until it fails?

locked

Re: Login, su to root, execute command

by Kaliorin @, (7242 days ago) @ Jasmine

hi,

i just downloaded the trial version of WodSSH...
i have trouble with the WaitFor method...
is the regex feature working with the trial version ?

here is the code (i just copy/paste/modify the code you provided)

The first WaitFor is blocking the application (timeout)

Private Sub Button1Click(sender As System.Object, e As System.EventArgs)

Dim Ssh1 As New WeOnlyDo.Client.SSH

Dim hostname As String, login As String, password As String, rootpass As String

Hostname = host
login = login
password = pass
rootpass = rootpass

Ssh1.Hostname = hostname
Ssh1.Login = login
Ssh1.Password = password
Ssh1.Blocking = True
Ssh1.Connect()

' once connected, wait for prompt
Ssh1.WaitFor( regex:[$ #>] $ , 5)

' if anything left in incoming buffer, delete it
If Ssh1.DataReady > 0 Then Ssh1.Receive()

' execute su command, wait for password prompt
Ssh1.Execute( su -
, Password: , 5)

' send password and wait for command prompt
Ssh1.Send(rootpass +
)
Ssh1.WaitFor( regex:[$ #>] $ , 5)

' if anything left in incoming buffer, delete it
If Ssh1.DataReady > 0 Then Ssh1.Receive()

' execute command and dump it's output to a messagebox
MessageBox.Show(Ssh1.Execute( ls -al
, regex:[$ #>] $ , 10))

' and disconnect
Ssh1.Disconnect()
End Sub

locked

Re: Login, su to root, execute command

by Jasmine, (7242 days ago) @ Kaliorin

Kaliorin,

regex expression we use match 99 of command prompts, but perhaps you're unlucky and your command prompt is different? What does it look like?

locked

Re: Login, su to root, execute command

by kaliorin @, (7242 days ago) @ Jasmine

the prompt looks like this
[name@hostname name] $

and the root
[root@hostname root] #


thanks ;)

Vincent

locked

Re: Login, su to root, execute command

by kaliorin @, (7242 days ago) @ kaliorin

hi i solve regex problem... but... i have another trouble

the code below works perfectly in step-by-step debug mode (the goal is to change a password)
but this don't work if y run it without a breakpoint in my code.(and the textbox acting like a log don't display all the text)
is there a problem with the Received() event ?


ssh1.Login= Login ;
ssh1.Password = Password ;
ssh1.Protocol = WeOnlyDo.Client.SSH.SupportedProtocols.SSH2;


this.ssh1.Connect( HostName ,22);
this.textBox1.Text += ssh1.WaitFor( $ );
this.textBox1.Text += this.ssh1.Execute( su -
, regex:[A-z]*[.,!?:...]* );
this.textBox1.Text += ssh1.Receive();


this.textBox1.Text += this.ssh1.Execute( rootpass
, regex:[A-z]*[.,!?:...]* );
this.textBox1.Text += ssh1.Receive();


this.textBox1.Text += this.ssh1.Execute( passwd guyname
, regex:[A-z]*[.,!?:...]* );
this.textBox1.Text += ssh1.Receive();

this.textBox1.Text += ssh1.Execute( Test2360
, regex:[A-z]*[.,!?:...]* );
this.textBox1.Text += ssh1.Receive();


this.textBox1.Text += ssh1.Execute( Test2360
, regex:[A-z]*[.,!?:...]* );
this.textBox1.Text += ssh1.Receive();

ssh1.Disconnect();

locked

Re: Login, su to root, execute command

by Jasmine, (7242 days ago) @ kaliorin

Kaliorin,

I cannot know what happens and why it doens't work. I suggest you try line at a time - make sure Connect finished, and look if first WaitFor was reached, then if it was passed etc.. First time your code stops - that's where the problem is.

Perhaps there's more than one $ returned before command prompt is displayed?

locked

Re: Login, su to root, execute command

by Kaliorin @, (7239 days ago) @ Jasmine

hi, i solve the problem by using System.Threading.


I just put some sleep ... and it works well.

Thread.Sleep(500);
sData += this.ssh1.Execute( passwd GuyName , regex:[A-z]*[.,!?:...]* );
Thread.Sleep(500);
sData += ssh1.Receive();

locked

Re: Login, su to root, execute command

by shin @, (6231 days ago) @ Kaliorin

Hi ,wodDamir :

I have a problem in my ASP code and I don't know what's wrong with it.
Can you please try to find the problem, thanks.

Code :
<
dim SSH
set SSH = Server.CreateObject( WeOnlyDo.wodSSHCom.1 )

hostname = 10.80.90.100
login = oam8adm
password = A12345
rootpass = scsicard

ssh.Hostname = hostname
ssh.Login = login
ssh.Password = password
ssh.Blocking = 1
//ssh.Protocol = 3
//SSH.Port = 22
ssh.Connect

// once connected, wait for prompt
ssh.WaitFor regex:[$ #>] $ ,5

// if anything left in incoming buffer, delete it
If ssh.DataReady > 0 Then
response.write ssh.Receive
end if

// execute su command, wait for password prompt
ssh.Execute su - & vbLf, Password: ,5

// send password and wait for command prompt
ssh.Send rootpass & vbLf
ssh.WaitFor regex:[$ #>] $ ,5

// if anything left in incoming buffer, delete it
If ssh.DataReady > 0 Then
ssh.Receive
end if

// execute command and dump it//s output to a messagebox
ssh.Execute ls -al & vbLf, regex:[$ #>] $ ,10

// and disconnect
ssh.Disconnect
>
Result :
WeOnlyDo.wodSSHCom.1 (0x800A274C)
The current connection has timeout.
/ssh/ssh.asp, line 60

locked

Re: Login, su to root, execute command

by shin @, (6231 days ago) @ shin

Result :
WeOnlyDo.wodSSHCom.1 (0x800A274C)
The current connection has timeout.
/ssh/ssh.asp, line 60

sorry , it should be /ssh/ssh.asp, line 17

locked

Re: Login, su to root, execute command

by wodDamir @, (6230 days ago) @ shin

Hi Shin,

The code you provided me with works on my side. It logs me in, executes su - , and lists the directory.

However, If I'm correct, you receive error WaitFor line, right after root password is sent. Perhaps the WaitFor is waiting for a wrong prompt? Can you try connecting to thta host using some other client (putty) and check what comes back as prompt?

Regards,
Damba

locked

Re: Login, su to root, execute command

by shin @, (6228 days ago) @ wodDamir

Hi Shin,

The code you provided me with works on my side. It logs me in, executes su - , and lists the directory.

However, If I'm correct, you receive error WaitFor line, right after root password is sent. Perhaps the WaitFor is waiting for a wrong prompt? Can you try connecting to thta host using some other client (putty) and check what comes back as prompt?

Regards,
Damba

Hi, wodDamir

normal account prompt is $
root account prompt is #

locked

Re: Login, su to root, execute command

by shin @, (6228 days ago) @ shin

Hi, wodDamir

I use this ASP code, It should not have anything wrong.
But it shows the result :

WeOnlyDo.wodSSHCom.1 (0x800A274C)
The current connection has timeout.
/ssh/ssh.asp, line 15

<

dim SSH
set SSH = Server.CreateObject( WeOnlyDo.wodSSHCom.1 )

hostname = 10.80.90.100
login = oam8adm
password = A12345
rootpass = scsicard

ssh.Hostname = hostname
ssh.Login = login
ssh.Password = password
ssh.Blocking = 1
ssh.Connect
ssh.Disconnect

>

locked

Re: Login, su to root, execute command

by woddrazen @, (6227 days ago) @ shin

Shin,


You are trying to connect to SSH or Telnet server?

Did you try to increase value of timeout in Execute and WaitFor Method from 5 to 30, what happened?


Drazen

locked

Re: Login, su to root, execute command

by shin @, (6217 days ago) @ woddrazen

Shin,


You are trying to connect to SSH or Telnet server?

Did you try to increase value of timeout in Execute and WaitFor Method from 5 to 30, what happened?


Drazen

Hi, Drazen:

I modify timeout to 30 the code works. But it shows timeout in ssh.recive.
Could you please give me some suggestion ?? Thanks.

Shin

locked

Re: Login, su to root, execute command

by woddrazen @, (6217 days ago) @ shin

Shin,


Can you show use please exact code snippet you are using?


Drazen

locked

Re: Login, su to root, execute command

by shin @, (6203 days ago) @ woddrazen

Hi, Drazen :
This is my test code.
It use telnet to to get the response from UNIX server.
But it works so strange.
Every time I run it, I can't get the same result.

<

set ssh = CreateObject( WeOnlyDo.wodSSHCom.1 )
ssh.Login = name
ssh.Password = pwd
ssh.Hostname = ip
ssh.Blocking = 1
ssh.Protocol = 1
ssh.Port = 23
ssh.Timeout = 30
ssh.Connect

ssh.Send rsh server2 & vbLf
ssh.WaitFor ,30

do while ssh.DataReady = 0

loop

a = ssh.Receive


ssh.Send ls & vbLf
ssh.WaitFor ,30

do while ssh.DataReady = 0

loop
b = ssh.Receive

ssh.disconnect

response.write a

response.write <br>---------------<br>

response.write b

>

locked

Re: Login, su to root, execute command

by woddrazen @, (6203 days ago) @ shin

Shin,


Please try this:
[code]<

set ssh = CreateObject( WeOnlyDo.wodSSHCom.1 )
ssh.Login = name
ssh.Password = pwd
ssh.Hostname = ip
ssh.Blocking = 1
ssh.Protocol = 1
ssh.Port = 23
ssh.Timeout = 30
ssh.Connect

ssh.WaitFor regex:[$ #>] $ ,30
ssh.Send rsh server2 & vbLf
ssh.DataReady = 0
ssh.WaitFor regex:[$ #>] $ ,30
a = ssh.Execute( ls & vbLf, regex:[$ #>] $ ,30)
ssh.Disconnect

response.write a

>[/code]
Drazen

locked

Re: Login, su to root, execute command

by shin @, (6203 days ago) @ woddrazen

Shin,


Please try this:
[code]<

set ssh = CreateObject( WeOnlyDo.wodSSHCom.1 )
ssh.Login = name
ssh.Password = pwd
ssh.Hostname = ip
ssh.Blocking = 1
ssh.Protocol = 1
ssh.Port = 23
ssh.Timeout = 30
ssh.Connect

ssh.WaitFor regex:[$ #>] $ ,30
ssh.Send rsh server2 & vbLf
ssh.DataReady = 0
ssh.WaitFor regex:[$ #>] $ ,30
a = ssh.Execute( ls & vbLf, regex:[$ #>] $ ,30)
ssh.Disconnect

response.write a

>[/code]
Drazen

Thanks a lot, Drazen !
This code works normally & stably.
But why the original code can't work ?
What's the different between execute and send ?

locked

Re: Login, su to root, execute command

by shin @, (6203 days ago) @ shin

Hi, Drazen :

There is no receive command in your code.
How did it work ??

BR,
Shin

locked

Re: Login, su to root, execute command

by wodDamir @, (6202 days ago) @ shin

Shin,

You can see that Drazen used Send, WaitFor and then Execute methods. Difference between Send and Execute is that Execute is blocking method, which will send a string, and wait for some specific string to be returned by server.

As you can see from the code, WaitFor is used after the Send method, which is used to do the same thing. Send the command, and wait until some specific string is returned.

Also, there is no receive in the code, since this code is running in blocking mode. Both of the methods, WaitFor and Execute methods return string type.

Hope I helped.

Regards,
Damba

locked