0

i'd need to check all the active IP address in my LAN. i tried with this code:

void MainWindow::on_pushButton_clicked() {
QString baseNetowrk = "192.168.1.";
for(int i = 0;i < 255;i++) {
    QString currIp = (baseNetowrk + "%1").arg(i);
    //qDebug() << "IP: " << currIp;
    QHostInfo hostInfo = QHostInfo::fromName(currIp);
    qDebug() << "NOME: " << hostInfo.hostName();
}

}

But this shows all IP (I think in the ARP cache).
How can i only display active IP?

matteo
  • 2,121
  • 4
  • 20
  • 33
  • What do you mean by "active"? I guess to determine if they network device is active you need to send a ping or some other form of network packet and analyse the answer. – xander Jul 13 '17 at 13:34
  • how can I send ping or packet through Qt?? – matteo Jul 13 '17 at 13:42
  • maybe look at the answers in this question: https://stackoverflow.com/questions/22935103/get-the-ping-from-a-remote-target-with-qt-windows-linux – xander Jul 13 '17 at 13:53

3 Answers3

1

ok, this code works:

void MainWindow::on_pushButton_clicked() {
    QString baseNetowrk = "192.168.1.";
#if defined(WIN32)
    QString parameter = "-n 1";
#else
    QString parameter = "-c 1";
#endif
    for(int i = 0; i < 256; i++) {
        QString currIp(baseNetowrk + QString::number(i));
        int exitCode = QProcess::execute("ping", QStringList() << parameter << currIp);
        if (exitCode == 0) {
            qDebug() << "OK :" << baseNetowrk + i;
        } else {
            qDebug() << "KO";
        }
    }
}

the only problem is that it is a little bit slow....

matteo
  • 2,121
  • 4
  • 20
  • 33
  • off course that is not very fast, but you can send send multiple ping requests at the same time with a thread pool or something. I'm not sure if teher is a limit how many you should ping in parallel, but if you do like 10 pings at a time it's still almost 10 times faster :D – xander Jul 14 '17 at 10:32
0

All ip adress

   foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
            if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
                 qDebug() << address.toString();
        }

or ARP scan

 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
        for(int i = 0; i < ipAddressesList.size(); ++ i) {
           if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
               ipAddressesList.at(i).toIPv4Address()) {
               ipAddress = ipAddressesList.at(i).toString();
               if(ipAddress.left(3) == "192") {
                   break;
               }
           }
       }

 ipAddress = ipAddress.left(ipAddress.lastIndexOf(".") + 1);

      for(int i = 0; i <= 255; ++i ) {
            m_socketsPool.append(new QTcpSocket(this));
            QString currentHost = ipAddress + QString::number(i);
            connect(m_socketsPool.at(i), SIGNAL(readyRead()), this, SLOT(readFortune()));
            connect(m_socketsPool.at(i), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
            m_socketsPool.at(i)->connectToHost(currentHost, yourHost);


   }




void readFortune()
{
    int i = 0;
    for(; i < m_socketsPool.size(); ++i) {
        if(sender() == m_socketsPool.at(i)) {
            QString currentHost = ipAddress + QString::number(i);
            qDebug()<<currentHost;
            ipStringList +=(QStringList() << currentHost);
            break;
        }
    }

    emit onOutStr(ipStringList);
}
Vahagn Avagyan
  • 750
  • 4
  • 16
  • @Programmer_ARM: I guess he wants to check remote targets in the LAN, not his own IPs. – xander Jul 13 '17 at 13:55
  • there is no support of Qt for your task. I would assume that you require the support of your LAN access point for providing all IP addresses. For instance a router modem may tell you all IP addresses connected. – Vahagn Avagyan Jul 13 '17 at 13:59
  • I added new changes, maybe this will help you ?? thanks) – Vahagn Avagyan Jul 13 '17 at 14:34
  • the arp-scan show me all the ip in the arp cache, also the ip that are not active; i think i have to use QProcess and ping! – matteo Jul 14 '17 at 06:44
0

Hi you might want to try using regular expressions

    QString IpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
QRegularExpression IpRegex ("^" + IpRange
                            + "(\\." + IpRange + ")"
                            + "(\\." + IpRange + ")"
                            + "(\\." + IpRange + ")$");
QRegularExpressionValidator *ipValidator = new QRegularExpressionValidator(IpRegex, this);
Cyphix
  • 1
  • 2