[Gambas-user] Running Sudo using Exec acts very differently from distro to distro!!

Willy Raets willy at ...2734...
Tue Apr 2 23:43:50 CEST 2013


On Tue, 2013-04-02 at 22:43 +0200, Benoît Minisini wrote:
> Le 02/04/2013 22:33, Willy Raets a écrit :
> > It is getting more crazy by the minute:
> >
> > This is part of the code of previous version posted (version 0.0.5):
> >
> > Public Sub btnSudo_Click()
> >
> >    If IsNull(txtPassword.Text) Then
> >      Message.Info("First enter a password")
> >    Else
> >      txaOutput.Clear
> >      txaOutput.Text = "Ready to rumble..\n\n"
> >      $hProcess = Exec [System.Shell, "-c", "sudo -s id -u"] For Input
> > Output As "Process"
> >      Print #$hProcess, txtPassword.Text
> >    Endif
> >
> > End
> >
> > To pin point where it goes wrong I added a output to txaOutput
> > (TextArea) before and after the passing of the password.
> > See ExpectedOutcome.png for what is should show when run successfully.
> >
> > Changed the code to (version 0.0.6):
> >
> > Public Sub btnSudo_Click()
> >
> >    If IsNull(txtPassword.Text) Then
> >      Message.Info("First enter a password")
> >    Else
> >      txaOutput.Clear
> >      txaOutput.Text = "Ready to rumble..\n"
> >      $hProcess = Exec [System.Shell, "-c", "sudo -s id -u"] For Input
> > Output As "Process"
> >      txaOutput.Text &= "Passing password..\n"   '<-- ADDED
> >      Print #$hProcess, txtPassword.Text
> >      txaOutput.Text &= "Password passed..\n\n"  '<-- ADDED
> >    Endif
> >
> > End
> 
> I didn't read all your project yet, but by seing that code, I can tell 
> you that what you wrote cannot work reliably.
> 
> Why? As the process execution is not synchronous, 'sudo' may not be yet 
> ready when the "Print #$hProcess,..." line is executed.
> 
> Instead, you must catch the output of 'sudo' in the Process_Read event, 
> and only sends the password after sudo has emitted its "Password:" prompt.
> 
> Look the 'VersionControl.Run' method in the IDE source code: it runs a 
> subversion command that way. In the Process_Read, it watches the output 
> of the svn command, and sends the password as soon as the command emits 
> a password prompt (which may come almost at random with the 'svn' command!)
> 
> So rewrite your code the way I told you before doing all your checks again.
> 
> Regards,
> 

Ok, now I get the problem.

I studied the code you suggested and implemented the passing of the
password in the Read event at the proper moment and it works on both my
development machine and the Fedora-LXDE box...YESSSS!!!

The code (for those ever running into the same problem):
----------------------------------
Private $hProcess As Process
Private $bFound As Boolean

'txtPassword -> TextBox to enter password
'txaOutput -> TextArea to feed back the output of the process

Public Sub btnSudo_Click()

Dim sCommand As String  
If IsNull(txtPassword.Text) Then
    Message.Info("First enter a password")
  Else
    $bFound = False
    sCommand = "id -u"
    txaOutput.Clear
    $hProcess = Exec [System.Shell, "-c", "sudo -s " & sCommand] For
Input Output As "Process"
  Endif
  
End

Public Sub Process_Read()

  Dim sLine As String
  Dim iB As Integer
  sLine = Read #Last, -256
  If Not $bFound Then
    If Len(sLine) <> 0 Then
      iB = InStr(sLine, "password for", 1)
      If iB > 1 Then
        txaOutput.Text &= "Passing password...\n"
        txaOutput.Insert(sLine & "\n")
        Print #$hProcess, txtPassword.Text
        $bFound = True
      Else
        txaOutput.Insert(sLine & "\n")
      Endif
    Endif
  Else
    txaOutput.Insert(sLine & "\n")
  Endif
  
End

Public Sub Process_Error(sError As String)

  txaOutput.Insert("ERROR: " & sError & "\n")
  
End

Public Sub Process_Kill()

  txaOutput.Insert("Killed\n")
  
End
----------------------------------
Note:
The sCommand = "id -u" can be replaced by any command you would like to
run using sudo like sCommand = "apt-get update" for example (on Debian
and Ubuntu based) will work.

Thank you for the insight Benoît, I learned a lot...

-- 
Kind regards,

Willy (aka gbWilly)

http://gambasshowcase.org/
http://howtogambas.org
http://gambos.org








More information about the User mailing list