Login, su to root, execute command - WeOnlyDo Discussion board

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

by wodSupport, Thursday, November 11, 2004, 23:25 (7107 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]


Complete thread: