...continua......
« 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
Attached the source code archive version 0.0.6
Next tested this on my Gambas 3.3.4 running distributions. Note the
difference
Test results version 0.0.5 (see previous post):
GAMBAS 3.3.4
------------
1. Linux Mint 13 - Mate 1.4.0 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> ok run 1 -> ok
run 2 -> ok run 2 -> ok
run 3 -> ok run 3 -> ok
run 4 -> ok run 4 -> ok
run 5 -> ok run 5 -> ok
2. Lubuntu 12.04 - LXDE 0.5.8 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> hang!! run 1 -> ok
run 2 -> hang!! run 2 -> hang!!
run 3 -> hang!! run 3 -> hang!!
run 4 -> hang!! run 4 -> hang!!
run 5 -> hang!! run 5 -> hang!!
3. Linux Mint 13 - Xfce 4.10 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> hang!! run 1 -> hang!!
run 2 -> ok run 2 -> hang!!
run 3 -> hang!! run 3 -> hang!!
run 4 -> hang!! run 4 -> hang!!
run 5 -> ok run 5 -> hang!!
Test results version 0.0.6 (see attachement)
GAMBAS 3.3.4
------------
1. Linux Mint 13 - Mate 1.4.0 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> ok run 1 -> ok
run 2 -> ok run 2 -> ok
run 3 -> ok run 3 -> ok
run 4 -> ok run 4 -> ok
run 5 -> ok run 5 -> ok
2. Lubuntu 12.04 - LXDE 0.5.8 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> ok run 1 -> ok
run 2 -> ok run 2 -> ok
run 3 -> ok run 3 -> ok
run 4 -> ok run 4 -> ok
run 5 -> ok run 5 -> ok
3. Linux Mint 13 - Xfce 4.10 - sudo version: 1.8.3p1 (Ubuntu 12.04
based)
run 1 -> ok run 1 -> ok
run 2 -> ok run 2 -> ok
run 3 -> ok run 3 -> ok
run 4 -> ok run 4 -> ok
run 5 -> ok run 5 -> ok
Will be testing further, but there is improvement as all seems to work
now all of a sudden just by adding two lines of code to check what is
happening, Xfce and LXDE seem to be doing fine....??!!
Anyone who can make some sense out of this? Explain what is happening
and why?
I'll be back with a Gambas 3.4.0 report later...wonder what happens
there..
--
Kind regards,
Willy (aka gbWilly) »
« 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,
--
Benoît Minisini »
« 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) »