Recent

Author Topic: SOLVED-Running Program as root (DEBIAN BASED LINUX DISTRO's ONLY)  (Read 7689 times)

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Admins Please Sticky This Topic
alot of users will find it useful.

I knew there had to be an easy way to get sudo to report stdOut to pascal and after
a weeks time I finally came up with this simple solution :)

keep in mind yourpassword is your sudo password on your machine so
change it accordingly :)

However I did not get it done with shell, but I did discover a way to do away with
all the garbage on the hundreds of examples i tinkered with. As you guessed
it uses TProcess to pipe input through sh to return stdOut, or stdErr

Uses
Process

var
hprocess: TProcess;

   hProcess := TProcess.Create(nil);

   hProcess.CommandLine := 'sh -c "echo yourpassword | sudo -S fdisk -l"';
   hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
   hProcess.Execute;
   memo1.Lines.LoadFromStream(hprocess.Output);
   hProcess.Free;

Best part is you can pipe as many arguments as you need
for example, you could install packages from respo using:

hprocess.CommandLine := 'sh -c "echo yourpassword | sudo -S apt-get -y install binutils"';
and report the output to your program in a memo control rather then it opening a terminal.

Being how commandline is depreciated im going to attempt to make it work with Executeable
if i succeed i will report back. even though commandline is depreciated it still works and compiles
fine at the time of this writing.
quick simple and does exactly what I needed it to :)

Keep It Simple Stupid
« Last Edit: December 13, 2012, 10:45:31 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: SOLVED-Running Program as root (KISS METHOD)
« Reply #1 on: December 13, 2012, 05:47:03 am »
1. Why post a new thread that is apparently in response to some other thread?
2. Why don't you simply refer to the executing external processes wiki article?
3. why not use some other mechanism such as policykit as recommended in the external process wiki article? (Hint: not everybody may have sudo on his machine)

That said, it's you code, and you're sharing a solution - and glad it works for you ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: SOLVED-Running Program as root (KISS METHOD)
« Reply #2 on: December 13, 2012, 05:57:51 am »
Oh sorry did not mean to offend anyone.
was just trying to mark my problem as solved.

But for the record for linux users, this is
the easiest and most straight forward
example to running progs as root.

Other methods are way over tanked, and confusing
for the beginner such as myself.

so perhaps for an expert coder such as yourself
policykit works best for you, or other methods,
but like i said this is the best method for a newcomer.

as for sudo if one is not come custom for your distro of linux
its a simple process to obtain and install .

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: SOLVED-Running Program as root (KISS METHOD)
« Reply #3 on: December 13, 2012, 06:51:39 am »
I'm not offended - just trying to point out there might be better ways of doing this as not everybody is willing to install e.g. sudo.

As said, thanks for posting your results and sharing - a lot of people don't take the trouble of doing this and posting information may always help the next guy!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: SOLVED-Running Program as root (KISS METHOD)
« Reply #4 on: December 13, 2012, 08:16:34 am »
But for the record for linux users, this is
the easiest and most straight forward
example to running progs as root.

Not really. Only for the subset of Linux users who run distros which use sudo in the Ubuntu way. To do the job properly you would need to make alternative arrangements for those which don't.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: SOLVED-Running Program as root (KISS METHOD)
« Reply #5 on: December 13, 2012, 09:29:28 pm »
It is very intresting that I received negative attitude for my discovery when infact, the thread im about to
post does exactly as the user wanted with a much easier code, sure I have not finished the code err handling etc, but the fact remains it works and it does exactly as it should and without all the confusion and misdirection that was used in the examples of this thread.

http://lazarus.freepascal.org/index.php/topic,14479.0.html

Notice how that thread revolves around sudo, su, gksudo

But you want to flame me for posting a easier method, that does not require
the use of 50+ lines of code that does exactly the same thing with less
headache.

I have done a favor to the community involving that thread, Yes it requires the use of
sudo as most debian distros do use sudo for elevated privledges. Perhaps I should have
posted, how to run app as root user on (DEBIAN LINUX BASED) distro's

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

ElAlex

  • Newbie
  • Posts: 1
Re: SOLVED-Running Program as root (DEBIAN BASED LINUX DISTRO's ONLY)
« Reply #6 on: April 19, 2016, 06:01:24 am »
Congratulations!!

Your post give a solution in the way it should be. Simple. Very simple.

And I agree with you.

Problems need be handled with KISS.

 :) :)

mdalacu

  • Full Member
  • ***
  • Posts: 238
    • dmSimpleApps
Re: SOLVED-Running Program as root (DEBIAN BASED LINUX DISTRO's ONLY)
« Reply #7 on: April 19, 2016, 07:50:54 am »
I don't think this is the way to do it. You should use sudo your program. You can make an shortcut on the desktop with the appropriate commands, Not store theroot password in noyr app. Ex: gksudo -k "your full path to the app" :

 

TinyPortal © 2005-2018