1

I'm trying to make a c++ program to connect to a network.

Snippet works perfectly fine if the password is correct.

bool connect(const char* ssid, const char* key)
{
bool ret = false;

try
{
    if ((ssid) && (key))
    {
        std::string cmd = "nmcli device wifi connect " + std::string(ssid) + " password " + std::string(key);

        auto fp = popen(cmd.c_str(), "r");
        if (fp)  //changed if contents to make a "sharable" sample
        {
            char buff[2048] = {0};

            while (fgets(buff, sizeof(buff), fp) != nullptr)
            {
                if(std::string(buff).find("success") != std::string::npos) 
                {
                    ret = true;
                    break;
                }
            }

        }
    }
}
catch (std::exception& ex)
{
    std::cerr<<"exception: "<<ex.what()<<std::endl;
}

return ret;
}

My issue is that the software opens a prompt if the password isn't correct, instead of giving me the credential error directly.

If I manually close the prompt, snippet behaves as expected.

Is there a way to prevent the password retry prompt? or even a better way to handle this*?

*currently disregarding safety issues(pwd stored in cmd history, etc).

elleryn
  • 11
  • 2
  • https://stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr – pm100 Jun 01 '20 at 04:45
  • This is entirely a matter of how the exec'd program `nmcli` works, which is documented. Little or nothing to do with your code, or programming. – user207421 Jun 01 '20 at 04:45
  • @user207421 thus the question, is there a way to test given credentials that won't create a prompt? – elleryn Jun 01 '20 at 04:56

0 Answers0