Desktop.RunAsRoot (gb.desktop)
Static Sub RunAsRoot ( Command As String [ , Wait As Boolean ] )
Run the specified
Command as root, by using the desktop-specific tool if possible.
Using
Wait will wait till
Command has finished before continuing.
If no desktop-specific tool is found it will raise an error with the message "No graphical sudo program found".
The
Command string is Split() for use with the
EXEC command using "Double quotes" not 'single quotes' so be sure not to use single quotes to encapsulate arguments.
Requires one of the following tools to be installed...
-
pkexec
-
gksudo
-
kdesudo
-
lxde-sudo
Most of the above are obsolete and no longer available except pkexec that is a part of the
policykit package or polkit.
See how to install pkexec on your system here
https://command-not-found.com/pkexec
Warning: Because it uses
EXEC you can only use one single command (See
EXEC for details).
To run multiple commands you will have to either use bash/sh -c with \"quotes\" or save the commands to a file and execute that file.
Dim sMultiple As String = "cd '/boot'\nls \"grub/\"\ncd 'grub'\ncat grub.cfg"
Desktop.RunAsRoot("bash -c \"" & sMultiple & "\"", True) ' run the **double** quoted command using a single bash command
Note: with the above method you cannot use
Quote() to encase the command as it will escape newline \n chars to \\n
and " to \", remove ' chars, etc, this "may" cause you problems.
(commands can still be separated by
; instead of
\n)
You may find it simpler to use a syntax like shown using
\" to encase the command then (as you can see) single or double quotes can also be used.
"bash -c \"" & sMultiple & "\""
Dim sTmp As String = Temp("command") ' get a temporary file name.
Dim sMultiple As String = "cd '/boot'\nls \"grub/\"\ncd 'grub'\ncat grub.cfg"
File.Save(sTmp, sMultiple)
' run the temporary file using a single bash command (you could omit bash if you add a shebang line to the file and make it executable)
Desktop.RunAsRoot("bash " & sTmp, True)
Kill sTmp ' delete the temporary file