run multiple non interacticve root commands

Post your must have Gambas Components/Controls/Classes/Modules here.
Post Reply
User avatar
BruceSteers
Legend
Posts: 2186
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

run multiple non interacticve root commands

Post by BruceSteers »

A class to let your program start an internal process as root and use multiple commands.

password is held in memory and used with sudo and Process.Expect() when run
You do not have the required permissions to view the files attached to this post.
User avatar
thatbruce
Regular
Posts: 312
Joined: Sat Sep 04, 2021 11:29 pm
Location: Sitting at my desk in South Australia

Re: run multiple non interacticve root commands

Post by thatbruce »

Interesting! Will try it as soon as we have a need, which may be sooner than I'd like.

(Linear Programming job is progressing leaps and bounds. Pity I own none of it :( I'll just have to remember how we did it for personal use one day.)
It looks like what you did there might solve installs for the thousands of students that are going to use it. Would you believe hundreds? How about a few.

b
User avatar
BruceSteers
Legend
Posts: 2186
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: run multiple non interacticve root commands

Post by BruceSteers »

if it helps 1 person i'll be happy :)

It's helped me.
I use the same class in my VBox-image mounter program. https://forum.gambas.one/viewtopic.php?t=1849

It's a Static class,
I just use RootCommand(sCommand) where it needs it (to mount/unmount the vbox drives) and it only asks for password the first time.
User avatar
BruceSteers
Legend
Posts: 2186
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: run multiple non interacticve root commands

Post by BruceSteers »

I have found other ways to do this also using bash internally.

I have used either a hidden TerminalView or just Exec/Shell process to run bash as su.
hBashProcess = Shell "pkexec env XAUTHORITY=" & Env["XAUTHORITY"] & " DISPLAY=" & Env["DISPLAY"] & " bash" For Input Output As "Proc"

Then use Write/Print to send the commands through the bash process.
Print #hBashProcess, sCommand

It works but has limitations with not knowing if the commands have finished running as Process.State relates to bash not the command it's running.
Also you have to exit bash before program closes.

The above RootCommander.class is better as it uses Process.Expect() on commands run directly (using sudo) So you can Check RootCommand.Process.State to see if the individual commands have finished running or not before allowing more.
Last edited by BruceSteers on Tue May 27, 2025 4:45 pm, edited 1 time in total.
User avatar
gbWilly
Site Admin
Posts: 368
Joined: Fri Sep 23, 2016 11:41 am
Location: Netherlands
Contact:

Re: run multiple non interacticve root commands

Post by gbWilly »

BruceSteers wrote: Tue May 27, 2025 3:53 am A class to let your program start an internal process as root and use multiple commands.

password is held in memory and used with sudo and Process.Expect() when run
Now we're talking :lol:
I might wanna have a look at this ;)
gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- Gambas3 Debian/Ubuntu repositories

- GambOS

... there is always a Catch if things go wrong!
Post Reply