Results 1 to 7 of 7

Thread: IP address find

  1. #1
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default IP address find

    Hi !

    guys, In my project i need to find out the current system ip address. By using ifconfig we can get ip address. But i really need ipaddress by using the qt. I tried with QHostAddress, but leads failed (:. How can i get ip address of local mech using qt???

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IP address find

    try using QProcess

  3. #3
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: IP address find

    Thanks for your reply....
    ya i used QProcess. But i need only ipaddress.

    if,
    Qt Code:
    1. QProcess *proc=new QProcess();
    2. proc->start("ifconfig eth0 | head -n 2 \ | sed 'N;s/\n/ /;N;s/\n/ /' | awk '{print $7 " " $9 " " $5}' \ | sed -e 's/addr://g' -e 's/Mask://g'");
    To copy to clipboard, switch view to plain text mode 

    How can i use the | (pipe symbol) in QProcess??? And also i tried grep but it doesn't work. Other wise tell me how to use grep in QProcess.
    Last edited by kpmsivachand; 21st February 2009 at 10:36.

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IP address find

    u r executing commands using QProcess the wrong way

    try this:
    Qt Code:
    1. QString prog = "/bin/bash";//shell
    2. QStringList arguments;
    3. arguments << "-c" << "ifconfig eth0 | head -n 2 \ | sed 'N;s/\n/ /;N;s/\n/ /' | awk '{print $7 " " $9 " " $5}' \ | sed -e 's/addr://g' -e 's/Mask://g'";
    4. QProcess* process = new QProcess(this);
    5. process->start(prog , arguments);
    6. process->waitForFinished();
    7. QString tmp = process->readAll();
    8. qDebug() << tmp;
    To copy to clipboard, switch view to plain text mode 

    like this, u can run any command that u can run on a normal shell. just change the second argument in the "arguments" stringlist.. if it doesnt work, break commands and see if the results are right and then move forward..hopefully, this will work anyway

  5. The following user says thank you to talk2amulya for this useful post:

    kpmsivachand (21st February 2009)

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: IP address find

    Using ifconfig as information source makes you dependent on the platform. Probably the gurus will come up with an easier way, but how about opening up a socket with a default address and reading the IP address out of that?

  7. The following user says thank you to Cruz for this useful post:

    kpmsivachand (21st February 2009)

  8. #6
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Cool Re: IP address find

    Thank u very much for your replies I got it.

    Finally i got it the ipaddress only by using the QProcess.

    My code was

    Qt Code:
    1. QString prog = "/bin/bash";//shell
    2. QStringList arguments;
    3. arguments << "-c" << "ifconfig eth0 | grep ‘inet ‘ | awk ‘{print $2}’ | sed ’s/addr://’";
    4. QProcess* process = new QProcess(this);
    5. process->start(prog , arguments);
    6. process->waitForFinished();
    7. QString tmp = process->readAll();
    8. qDebug() << tmp;
    To copy to clipboard, switch view to plain text mode 

    Above code will return only the ip address of the local mechine [ etho inet address: ]
    Last edited by kpmsivachand; 21st February 2009 at 18:43.

  9. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: IP address find

    Did you have a look at QHostInfo ??
    This might work for you ..
    Qt Code:
    1. QString ipaddress = QHostInfo::.addresses().first().toString()
    To copy to clipboard, switch view to plain text mode 


Similar Threads

  1. problems installing Qt opensource with msvc2008 support
    By odin1985 in forum Installation and Deployment
    Replies: 6
    Last Post: 24th May 2008, 10:06
  2. GCC can't find QSqlDatabase!!
    By brevleq in forum Qt Programming
    Replies: 5
    Last Post: 29th October 2007, 08:05
  3. Unable to resolve the IP address. ( Qt 3.)
    By joseph in forum Qt Programming
    Replies: 18
    Last Post: 5th July 2007, 13:02
  4. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 14:15
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 23:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.