Weird behavior of QProcess and multiple qoutes



  • I've tried to connect wi-fi using QProcess.
    I've used wpa_cli utility.
    To successfully connect to security network I should set password in multiple quotes:

    QProcess wpacli;
    wpacli.execute("wpa_cli set_network 0 psk '\"12345678\"'");
    

    When I execute my application, this command returns FAIL.
    When I execute this line in console, it returns OK.
    I've printed this line to see is it correct:

    qDebug() << cmd.toUtf8().constData();
    

    This shows me correct line.
    I don't know why this command does not executes using QProcess.
    Can anybody help me?



  • Use QProcess::execute overload with QStringList arguments


  • Qt Champions 2016

    Hi
    In other words, do not append to one string

    String program = "wpa_cli";
    QStringList arguments;
    arguments <<"set_network"<< "0" << "psk" << "12345" ;
    
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);
    

    Also to help yourself, at least hook up to
    http://doc.qt.io/qt-5/qprocess.html#errorOccurred



  • Hello,

    I tried to use execute with argument list and my results were the same as earlier.
    I've added connection to signal errorOccurred() but this signal was not emited.

    My code is here:

        QString program = "wpa_cli";
        QStringList arguments;
        QProcess * wpacli = new QProcess();
    
        connect(wpacli, SIGNAL(errorOccurred(QProcess::ProcessError )), this, SLOT(onError(QProcess::ProcessError)));
    
        arguments << "remove_network" << "all";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "add_network";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "set_network" << "0" << "ssid" << id.toUtf8().toHex();
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "set_network" << "0" << "psk" << pwd;
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "enable_network" << "0";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "select_network" << "0";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    
        arguments.clear();
        arguments << "save_config";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    

    In onError() slot I've printed some logs:

    qDebug() << Q_FUNC_INFO;
    
        QString errorStr;
    
        switch(error) {
        case QProcess::FailedToStart: errorStr = "QProcess::FailedToStart"; break;
        case QProcess::Crashed: errorStr = "QProcess::Crashed"; break;
        case QProcess::Timedout: errorStr = "QProcess::Timedout"; break;
        case QProcess::WriteError: errorStr = "QProcess::WriteError"; break;
        case QProcess::ReadError: errorStr = "QProcess::ReadError"; break;
        case QProcess::UnknownError: errorStr = "QProcess::UnknownError"; break;
        }
    
        qDebug() << errorStr;
    

    In console I can see the next results:

    ("remove_network", "all")
    Selected interface 'wlan0'
    OK
    ("add_network")
    Selected interface 'wlan0'
    0
    ("set_network", "0", "ssid", "416e64726f69644150")
    Selected interface 'wlan0'
    OK
    ("set_network", "0", "psk", "12345678")
    Selected interface 'wlan0'
    FAIL
    ("enable_network", "0")
    Selected interface 'wlan0'
    OK
    ("select_network", "0")
    Selected interface 'wlan0'
    OK
    ("save_config")
    Selected interface 'wlan0'
    OK
    

    I'm not sure that using of argument list is good idea, because wpa_cli required to get password in multiple quotes: '""'.

    Do you have other ideas?


  • Moderators

    @Manta-Ray
    Then just do

    arguments << "set_network" << "0" << "psk" << "\"" + pwd + "\"";
    


  • @jsulm
    Ok, added

        arguments.clear();
        arguments << "set_network" << "0" << "psk" << "'\"" << pwd << "\"'";
        qDebug() << arguments;
        wpacli->execute(program, arguments);
    

    and got the same result:

    ("set_network", "0", "psk", "'\"", "12345678", "\"'")
    Selected interface 'wlan0'
    FAIL
    
    

  • Moderators

    @Manta-Ray Please read again what I wrote and compare it to what you did.



  • Wow!
    It works! Thanks a lot!
    Thanks!
    Thanks!
    Thanks!

    Can you, please, explain me, what's the magic is going here?



  • @Manta-Ray You need to pass 4 arguments to wpa_cli:

    set_network, 0, psk, "12345678"

    instead you passed 6

    set_network, 0, psk, ", 12345678, "


  • Moderators

    @Manta-Ray No magic. You simply put both " as own parameters:

    arguments << "set_network" << "0" << "psk" << "'\"" << pwd << "\"'";
    

    see here:

    ("set_network", "0", "psk", "'\"", "12345678", "\"'")
    

    Instead you need to put your password as one string with leading and trailing ", like here:

    arguments << "set_network" << "0" << "psk" << "\"" + pwd + "\"";
    


  • Ahaaaa, Thanks you guys a lot!!!!!!
    I really appreciate you!!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.