c++ connect to network (ubuntu), how to handle wrong password case

204 Views Asked by At

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).

0

There are 0 best solutions below