Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to set ip address to a computer in Qt (Solved)

How to set ip address to a computer in Qt (Solved)

Scheduled Pinned Locked Moved Mobile and Embedded
32 Posts 4 Posters 12.8k 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.
  • p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #2

    Hi,

    AFAIK Qt is not enough for this i.e you will need some platform specific code or use QProcess to call ifconfig or netsh depending upon the OS.

    157

    1 Reply Last reply
    0
    • H Offline
      H Offline
      houmingc
      wrote on last edited by
      #3

      How to issue ubuntu Terminal command using Qt like
      eth0 static 192.168.100.1 netmask 255.255.255.0

      Any idea how to achieve it?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        Like p3c0 suggested use QProcess. However note that for these kind of modification your generally need root access.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • H Offline
          H Offline
          houmingc
          wrote on last edited by
          #5

          I wrote the code below, everytime i open
          a ubuntu terminal, run command ifconfig, the ip is never change. what should i do next ?

          @
          void SetDeviceIP()
          {
          QString commandLine;
          char* ipset = "192.168.100.1";
          commandLine.sprintf(" sudo ifconfig eth0 %s
          netmask 255.255.255.0 up", ipset);

          QProcess* myPro = new QProcess();
          myPro->start( commandLine );
          myPro-> waitForFinished();
          }
          @

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #6

            Since you are sudo'ing the command, have you made sure about the authentication ?

            157

            1 Reply Last reply
            0
            • H Offline
              H Offline
              houmingc
              wrote on last edited by
              #7

              i trying to pass the password over to the terminal after the sudo'ing command.

              is it possible to pass my password and authenticate? I check many links
              http://stackoverflow.com/questions/9619652/qt-setting-the-ip.

              Is it true this cannot be done in Qt?

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #8

                AFAIK, You will have to either run the Qt program as root or you can provide a password file to sudo eg.
                @
                sudo -S ifconfig eth0 192.168.100.4 < /home/user/passwd.txt
                @

                (but beware of security risks)

                157

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jafarabadi.qt
                  wrote on last edited by
                  #9

                  [quote author="p3c0" date="1415858672"]AFAIK, You will have to either run the Qt program as root or you can provide a password file to sudo eg.
                  @
                  sudo -S ifconfig eth0 192.168.100.4 < /home/user/passwd.txt
                  @

                  (but beware of security risks)[/quote]

                  Hi
                  Thanks it's very useful command.
                  i use from QProcess too
                  read Standard Outputs and ...

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    houmingc
                    wrote on last edited by
                    #10

                    Seeking help again. My Ubuntu terminal does open, but nothing is wrote onto it. why is it so?

                    @
                    QProcess *myProcess = new QProcess;
                    myProcess->start("gnome-terminal");
                    myProcess->waitForStarted(5000);
                    myProcess->write("Qt rocks");
                    myProcess->write("sudo -S ifconfig eth0 192.168.100.4 netmask 255.255.255.0");

                    @

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      houmingc
                      wrote on last edited by
                      #11

                      Seeking help again. My Ubuntu terminal does open, but nothing is wrote onto it. why is it so?

                      @
                      QProcess *myProcess = new QProcess;
                      myProcess->start("gnome-terminal");
                      myProcess->waitForStarted(5000);
                      myProcess->write("Qt rocks");
                      myProcess->write("sudo -S ifconfig eth0 192.168.100.4 netmask 255.255.255.0");

                      @

                      1 Reply Last reply
                      0
                      • p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #12

                        You need not open a terminal for launching that process. Try following:
                        @
                        QStringList args;
                        args << "-c" << "sudo -S ifconfig eth0 192.168.0.21 < /home/user/passwd.txt";
                        pro->start("/bin/sh",args);
                        @

                        But as said earlier it doesnot provide security as the password file is revealed.
                        Another way would be to write a script which will run ifconfig command. Call this script instead of that command. Add an entry for this script in /etc/sudoers alongwith the user that will execute this script (i.e linux user with which you run your application). There are plenty of examples on how to edit sudoers using visudo. Check them out.

                        157

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #13

                          You need not open a terminal for launching that process. Try following:
                          @
                          QStringList args;
                          args << "-c" << "sudo -S ifconfig eth0 192.168.0.21 < /home/user/passwd.txt";
                          pro->start("/bin/sh",args);
                          @

                          But as said earlier it doesnot provide security as the password file is revealed.
                          Another way would be to write a script which will run ifconfig command. Call this script instead of that command. Add an entry for this script in /etc/sudoers alongwith the user that will execute this script (i.e linux user with which you run your application). There are plenty of examples on how to edit sudoers using visudo. Check them out.

                          157

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            houmingc
                            wrote on last edited by
                            #14

                            i vi a file in /home/ubuntu/passwd.txt with my password. Debug the code. After Application finished running, i do a ifconfig in ubuntu terminal. The ip address has not been changed

                            @
                            void SetpcAddress{

                                                   QProcess *myProcess = new QProcess();
                                                   QStringList args;
                                                   args<<"-c"<<
                              "sudo -S ifconfig eth0 192.168.100.21</home/ubuntu/passwd.txt";
                                                    myProcess->start("/bin/sh",args);
                            

                            }

                            int main(void)
                            {

                                         SetpcAddress();    
                            

                            }

                            @

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              houmingc
                              wrote on last edited by
                              #15

                              i vi a file in /home/ubuntu/passwd.txt with my password. Debug the code. After Application finished running, i do a ifconfig in ubuntu terminal. The ip address has not been changed

                              @
                              void SetpcAddress{

                                                     QProcess *myProcess = new QProcess();
                                                     QStringList args;
                                                     args<<"-c"<<
                                "sudo -S ifconfig eth0 192.168.100.21</home/ubuntu/passwd.txt";
                                                      myProcess->start("/bin/sh",args);
                              

                              }

                              int main(void)
                              {

                                           SetpcAddress();    
                              

                              }

                              @

                              1 Reply Last reply
                              0
                              • p3c0P Offline
                                p3c0P Offline
                                p3c0
                                Moderators
                                wrote on last edited by
                                #16

                                Does that command work from outside ?

                                157

                                1 Reply Last reply
                                0
                                • p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on last edited by
                                  #17

                                  Does that command work from outside ?

                                  157

                                  1 Reply Last reply
                                  0
                                  • H Offline
                                    H Offline
                                    houmingc
                                    wrote on last edited by
                                    #18

                                    cat passwd.txt return my password correct

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      houmingc
                                      wrote on last edited by
                                      #19

                                      cat passwd.txt return my password correct

                                      1 Reply Last reply
                                      0
                                      • H Offline
                                        H Offline
                                        houmingc
                                        wrote on last edited by
                                        #20

                                        it works when i do a sudo su
                                        it doesn't work without permission, what should i do next?

                                        1 Reply Last reply
                                        0
                                        • H Offline
                                          H Offline
                                          houmingc
                                          wrote on last edited by
                                          #21

                                          it works when i do a sudo su
                                          it doesn't work without permission, what should i do next?

                                          1 Reply Last reply
                                          0

                                          • Login

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