PDA

View Full Version : QT to linux terminal



Zexix
5th March 2012, 12:58
how do i connect Qt with Iptables?
I tried to add rules in ipTables using



QString prog = "/bin/bash";
QStringList arguments;
arguments << "-c" << "iptables -A "<<ui->comboBox->currentText()<<" -p icmp -i eth0 -j DROP";
QProcess* process = new QProcess(this);
process->start(prog , arguments);
process->waitForFinished();
QString tmp = process->readAll();
qDebug() << tmp;
just a trial run but the rule doesnt get added to iptables.
It did when I run this code a week ago, please suggest some changes so that this code works!

wysota
5th March 2012, 13:08
Did you run it as root?

TemplarKnight
5th March 2012, 18:18
In this case, looking at your code:


arguments << "-c" << "iptables -A "<<ui->comboBox->currentText()<<" -p icmp -i eth0 -j DROP";


Using QProcess you must know that every argument should be independent. Something like this:



arguments << "-c" << "iptables" << "-A" << ui->comboBox->currentText() << "-p" << "icmp" << "-i" << "eth0" << "-j" << "DROP";


... should work. But, two points about this:
1- I never tried commands with so many arguments, but this should not be a problem.
2- Actually, your program to execute should not be "/bin/bash", it should be "iptables" directly, because QProcess at the end executes every command at the bash, so you don't need to specify this.

Maybe this code could be better:


QString prog = "iptables";
QStringList arguments;
arguments "-A" << ui->comboBox->currentText() << "-p" << "icmp" << "-i" << "eth0" << "-j" << "DROP";
QProcess* process = new QProcess(this);
process->start(prog , arguments);
process->waitForFinished();
QString tmp = process->readAll();
qDebug() << tmp;


Hope it helps. :-)