Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. system() library function is not writing output in a text file
QtWS25 Last Chance

system() library function is not writing output in a text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
ubuntunetworkingsystem
7 Posts 4 Posters 785 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Himanshu Rohilla
    wrote on 4 Oct 2022, 07:08 last edited by
    #1

    Hi All,

    I am writing an application that will be used to set the selected WIFI network. The user will enter SSID and its password and on a button click, I am calling a function that will take these 2 parameters. The code snippet for this function is as follows:

    QString strCmdWifi = "nmcli dev wifi connect '"+ssid+"' password '"+pwd+"' &> out.txt";
    
    qDebug() << "strCmdWifi" << strCmdWifi; 		// Prints correct command
    
    
            int nCmdResponse = system(strCmdWifi.toStdString().c_str());
    
            if(nCmdResponse == 0)
            {
                QFile file("out.txt");
    
                if(file.open(QIODevice::ReadOnly | QIODevice::Text))
                {
                    QString strVal(file.readAll());
    				
    				qDebug() << "strVal" << strVal;  // Prints blank
    
                    file.close();
    
                    if(strVal.startsWith("Error",Qt::CaseInsensitive) || strVal.contains("Error",Qt::CaseInsensitive))
                    {
                        // Handling error here.
                    }
    				else
    				{
    					// Showing successful msg
    				}
                }
            }
    
    

    "ssid" and "pwd" are parameters. I am not getting any error in the out.txt file if I pass the wrong "pwd" value. The application is running with sudo permission. If I run the command ( strCmdWifi variable) on a terminal, it writes an error in a file but when I am trying from the Qt application, it is not writing an error in the file.

    HImanshu Rohilla

    J 1 Reply Last reply 4 Oct 2022, 07:57
    0
    • H Himanshu Rohilla
      4 Oct 2022, 07:08

      Hi All,

      I am writing an application that will be used to set the selected WIFI network. The user will enter SSID and its password and on a button click, I am calling a function that will take these 2 parameters. The code snippet for this function is as follows:

      QString strCmdWifi = "nmcli dev wifi connect '"+ssid+"' password '"+pwd+"' &> out.txt";
      
      qDebug() << "strCmdWifi" << strCmdWifi; 		// Prints correct command
      
      
              int nCmdResponse = system(strCmdWifi.toStdString().c_str());
      
              if(nCmdResponse == 0)
              {
                  QFile file("out.txt");
      
                  if(file.open(QIODevice::ReadOnly | QIODevice::Text))
                  {
                      QString strVal(file.readAll());
      				
      				qDebug() << "strVal" << strVal;  // Prints blank
      
                      file.close();
      
                      if(strVal.startsWith("Error",Qt::CaseInsensitive) || strVal.contains("Error",Qt::CaseInsensitive))
                      {
                          // Handling error here.
                      }
      				else
      				{
      					// Showing successful msg
      				}
                  }
              }
      
      

      "ssid" and "pwd" are parameters. I am not getting any error in the out.txt file if I pass the wrong "pwd" value. The application is running with sudo permission. If I run the command ( strCmdWifi variable) on a terminal, it writes an error in a file but when I am trying from the Qt application, it is not writing an error in the file.

      J Offline
      J Offline
      JonB
      wrote on 4 Oct 2022, 07:57 last edited by JonB 10 Apr 2022, 08:00
      #2

      @Himanshu-Rohilla
      This seems to be a question about system() and maybe nmcli, not about Qt.

      Verify that the redirection works in a much simpler command, e.g. ls -l &> out.txt. Verify some other command which produces an error message (ls -abcdefghijklm &> out.txt) sends it to the file too.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 4 Oct 2022, 08:17 last edited by
        #3

        You are using a relative path the the output file. Verify that the current working directory is where you think it is if you are looking for the file manually.

        The error message when you provide an invalid password is almost certainly written to stderr. You are using a bash redirection &> to attempt capturing both stdout and stderr. system() runs the command with sh which may not support that syntax.

        You do not need the temporary file at all if you use QProcess.

        Your method is fragile. Think what occurs if the password provided by the user contains a single quote (or something more deliberately structured to be malicious).

        J 1 Reply Last reply 4 Oct 2022, 08:32
        1
        • C ChrisW67
          4 Oct 2022, 08:17

          You are using a relative path the the output file. Verify that the current working directory is where you think it is if you are looking for the file manually.

          The error message when you provide an invalid password is almost certainly written to stderr. You are using a bash redirection &> to attempt capturing both stdout and stderr. system() runs the command with sh which may not support that syntax.

          You do not need the temporary file at all if you use QProcess.

          Your method is fragile. Think what occurs if the password provided by the user contains a single quote (or something more deliberately structured to be malicious).

          J Offline
          J Offline
          JonB
          wrote on 4 Oct 2022, 08:32 last edited by
          #4

          @ChrisW67 said in system() library function is not writing output in a text file:

          system() runs the command with sh which may not support that syntax

          On Ubuntu at least /bin/sh is a link to /bin/bash. But we shall see if the OP tries e.g. the ls -l &> out.txt.

          C 1 Reply Last reply 4 Oct 2022, 22:13
          0
          • J JonB
            4 Oct 2022, 08:32

            @ChrisW67 said in system() library function is not writing output in a text file:

            system() runs the command with sh which may not support that syntax

            On Ubuntu at least /bin/sh is a link to /bin/bash. But we shall see if the OP tries e.g. the ls -l &> out.txt.

            C Offline
            C Offline
            ChrisW67
            wrote on 4 Oct 2022, 22:13 last edited by
            #5

            Curious. On my Ubuntu 22.04 system:

            $ ls -l /bin/sh /bin/dash
            -rwxr-xr-x 1 root root 125688 Mar 23  2022 /bin/dash
            lrwxrwxrwx 1 root root      4 Mar 23  2022 /bin/sh -> dash
            

            dash is minimal, POSIX-compliant sh replacement.

            J 1 Reply Last reply 4 Oct 2022, 22:35
            0
            • C ChrisW67
              4 Oct 2022, 22:13

              Curious. On my Ubuntu 22.04 system:

              $ ls -l /bin/sh /bin/dash
              -rwxr-xr-x 1 root root 125688 Mar 23  2022 /bin/dash
              lrwxrwxrwx 1 root root      4 Mar 23  2022 /bin/sh -> dash
              

              dash is minimal, POSIX-compliant sh replacement.

              J Offline
              J Offline
              JonB
              wrote on 4 Oct 2022, 22:35 last edited by JonB 10 Apr 2022, 22:40
              #6

              @ChrisW67
              You're doubtless correct, sorry. I didn't fire my Ubuntu up, I took it from man sh in Google -> https://linux.die.net/man/1/sh. Still waiting to hear back from the OP as to their findings :)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kent-Dorfman
                wrote on 5 Oct 2022, 05:50 last edited by
                #7

                system() is the naive OS interface mecahnism. It will stall the calling program while executing. an async QProcess is a better mechanism if you must call a subshell...The sophisitcated Linux way is to use the dbus system to talk directly to NetworkManager.

                1 Reply Last reply
                0

                4/7

                4 Oct 2022, 08:32

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved