How to set ip address to a computer in Qt (Solved)
-
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.
-
Hi,
Like p3c0 suggested use QProcess. However note that for these kind of modification your generally need root access.
-
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 ?
-
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)
-
[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 ... -
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");@
-
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. -
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();
}
@
-
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 ?