Sudo command (linux) through a QThread
-
I want to do : @sudo apt-get install package_name@ and after that give "y" as input so it starts downloading !!! I know i should use QProccess but i dont know how exactly, i need to give password and y input to command !
How can i do it? I already have QString pack_name and QString pass in thread class !
-
[quote author="pedro79" date="1405877522"]QProcess is a QIODevice so just write the 'y' and the password after launching the process into the device.[/quote]
[quote author="pedro79" date="1405877522"]QProcess is a QIODevice so just write the 'y' and the password after launching the process into the device.[/quote]
i tried this, @ QProcess p;
QString command = "sudo apt-get install ";
command.append(pack);
p.start(command);
p.write(pass);
p.write("y");@can you tell me what i'm doing wrong :(? sorry i can't find it on net!
-
[quote author="pedro79" date="1405887248"]first open to read.
then wait until you can write:
waitForReadyRead(int msecs)
[/quote]:o good, i will try that tomorrow, i currently wrote a non GUI app to download my package over night :D hope it works!!
EDIT: 6Am, and im still awake, let me try to see if it works! my terminal programm worked btw, its upgrading ubuntu and downloading steam !!!Edit2: im getting nowhere, i dont understand what i should do:( :(
as i know
i first make a process , then i make a stringlist for arguments, my arguments are apt-get and install and lets say wine, than i start it with "sudo" as prog and args as arguments, tthen i wait for ready ready ! and i write to it, it still gives same result, im so confused !! read like 100 pages of stuff still i dont get it! im disapointed of myself:(this is what im trying right now:
@ QProcess process;
process.open(QIODevice::ReadWrite);
process.waitForReadyRead();
QStringList args;
args << "apt-get" << "install" << "wine";
process.start("sudo",args);
process.waitForReadyRead();
process.write("mepass");
process.waitForFinished(-1);
qDebug() << process.readAll();
qDebug() << process.errorString();@it asks for password!
edit3:
@ QProcess process;
process.setProgram("/bin/sh");
process.startDetached("/bin/sh", QStringList()<< "-c"
<< "echo y | sudo -S apt-get install gimp");
process.waitForFinished();@this works if i open app with sudo !
if i put my pass instead of y it works but requires users input for y! and write("y") doesn't work, i tried putting wait for !
-
Plz remove my pass(edit) ! Im such an Idiot
When you stay awake for 24H weird stuff happen !!
I knew its gonna happen! I checked 2x times but still i was sure !!:D
I changed it to something but guess i control-z before posting ;(
Im gonna try that thanks!!!! I will also make sure im opening right!!!
-
[quote author="pedro79" date="1405935500"]# start the process "sudo apt-get..."
then open it for writing
write your password (did you really post your password here in the forum: kamranwill ?)
waitforwritten
write "y"[/quote]
its not like im lazy or anything, i just can't figure out!
i was always bad at I/O !!!!
anyway:
i first make process:
@QProcess proc;@then i start the process with sudo apt-get upgrade:
@proc.start("sudo", QStringList() << "apt-get" << "upgrade");@then i wait for it to do it because i tried without it and it says process already running:
@
proc.waitForReadyRead();@then i open it as write:
@proc.open(QIODevice::WriteOnly);@than i write my password:
@proc.write("not this time :D");@
and i wait for bytes to be writen:@ proc.waitForBytesWritten();
proc.waitForFinished();@also i dont need the "y" for upgrade so i dont inlcude it, it doesn't do anything!
when i run my app from terminal it still wants me to enter password !
from what you told me it should work!!! can you tell me where im doing wrong!?
edit:
i had to reinstall linux, and i had qt setup on my second hdd, my second hdd doens't work, i dont know why anyway i lost ssetup:( i have to download it again tonight, than i can start to see if i can find out myself,