How to set ip address to a computer in Qt (Solved)
-
wrote on 11 Nov 2014, 09:57 last edited by houmingc 6 Sept 2015, 06:58
i manage to read ip address into variable QStringList DeviceIP, like to set ip address to a computer. Please assist.
@
QTextStream stream(&file);
QString DeviceLine;
do
{ DeviceLine =stream.readLine();
DeviceIP<<DeviceLine;
}while(!DeviceLine.isNull());
file.close();@
-
Hi,
AFAIK Qt is not enough for this i.e you will need some platform specific code or use QProcess to call ifconfig or netsh depending upon the OS.
-
wrote on 11 Nov 2014, 14:39 last edited by
How to issue ubuntu Terminal command using Qt like
eth0 static 192.168.100.1 netmask 255.255.255.0Any idea how to achieve it?
-
Hi,
Like p3c0 suggested use QProcess. However note that for these kind of modification your generally need root access.
-
wrote on 12 Nov 2014, 08:10 last edited by
I wrote the code below, everytime i open
a ubuntu terminal, run command ifconfig, the ip is never change. what should i do next ?@
void SetDeviceIP()
{
QString commandLine;
char* ipset = "192.168.100.1";
commandLine.sprintf(" sudo ifconfig eth0 %s
netmask 255.255.255.0 up", ipset);QProcess* myPro = new QProcess();
myPro->start( commandLine );
myPro-> waitForFinished();
}
@ -
Since you are sudo'ing the command, have you made sure about the authentication ?
-
wrote on 13 Nov 2014, 01:02 last edited by
i trying to pass the password over to the terminal after the sudo'ing command.
is it possible to pass my password and authenticate? I check many links
http://stackoverflow.com/questions/9619652/qt-setting-the-ip.Is it true this cannot be done in Qt?
-
AFAIK, You will have to either run the Qt program as root or you can provide a password file to sudo eg.
@
sudo -S ifconfig eth0 192.168.100.4 < /home/user/passwd.txt
@(but beware of security risks)
-
wrote on 14 Nov 2014, 06:15 last edited by
[quote author="p3c0" date="1415858672"]AFAIK, You will have to either run the Qt program as root or you can provide a password file to sudo eg.
@
sudo -S ifconfig eth0 192.168.100.4 < /home/user/passwd.txt
@(but beware of security risks)[/quote]
Hi
Thanks it's very useful command.
i use from QProcess too
read Standard Outputs and ... -
wrote on 27 Jan 2015, 08:29 last edited by
Seeking help again. My Ubuntu terminal does open, but nothing is wrote onto it. why is it so?
@
QProcess *myProcess = new QProcess;
myProcess->start("gnome-terminal");
myProcess->waitForStarted(5000);
myProcess->write("Qt rocks");
myProcess->write("sudo -S ifconfig eth0 192.168.100.4 netmask 255.255.255.0");@
-
wrote on 27 Jan 2015, 08:29 last edited by
Seeking help again. My Ubuntu terminal does open, but nothing is wrote onto it. why is it so?
@
QProcess *myProcess = new QProcess;
myProcess->start("gnome-terminal");
myProcess->waitForStarted(5000);
myProcess->write("Qt rocks");
myProcess->write("sudo -S ifconfig eth0 192.168.100.4 netmask 255.255.255.0");@
-
You need not open a terminal for launching that process. Try following:
@
QStringList args;
args << "-c" << "sudo -S ifconfig eth0 192.168.0.21 < /home/user/passwd.txt";
pro->start("/bin/sh",args);
@But as said earlier it doesnot provide security as the password file is revealed.
Another way would be to write a script which will run ifconfig command. Call this script instead of that command. Add an entry for this script in /etc/sudoers alongwith the user that will execute this script (i.e linux user with which you run your application). There are plenty of examples on how to edit sudoers using visudo. Check them out. -
You need not open a terminal for launching that process. Try following:
@
QStringList args;
args << "-c" << "sudo -S ifconfig eth0 192.168.0.21 < /home/user/passwd.txt";
pro->start("/bin/sh",args);
@But as said earlier it doesnot provide security as the password file is revealed.
Another way would be to write a script which will run ifconfig command. Call this script instead of that command. Add an entry for this script in /etc/sudoers alongwith the user that will execute this script (i.e linux user with which you run your application). There are plenty of examples on how to edit sudoers using visudo. Check them out. -
wrote on 27 Jan 2015, 09:26 last edited by
i vi a file in /home/ubuntu/passwd.txt with my password. Debug the code. After Application finished running, i do a ifconfig in ubuntu terminal. The ip address has not been changed
@
void SetpcAddress{QProcess *myProcess = new QProcess(); QStringList args; args<<"-c"<< "sudo -S ifconfig eth0 192.168.100.21</home/ubuntu/passwd.txt"; myProcess->start("/bin/sh",args);
}
int main(void)
{SetpcAddress();
}
@
-
wrote on 27 Jan 2015, 09:26 last edited by
i vi a file in /home/ubuntu/passwd.txt with my password. Debug the code. After Application finished running, i do a ifconfig in ubuntu terminal. The ip address has not been changed
@
void SetpcAddress{QProcess *myProcess = new QProcess(); QStringList args; args<<"-c"<< "sudo -S ifconfig eth0 192.168.100.21</home/ubuntu/passwd.txt"; myProcess->start("/bin/sh",args);
}
int main(void)
{SetpcAddress();
}
@
-
Does that command work from outside ?
-
Does that command work from outside ?
-
wrote on 27 Jan 2015, 09:30 last edited by
cat passwd.txt return my password correct
-
wrote on 27 Jan 2015, 09:30 last edited by
cat passwd.txt return my password correct
-
wrote on 27 Jan 2015, 09:34 last edited by
it works when i do a sudo su
it doesn't work without permission, what should i do next?