Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] How to use nmap by QProcess?



  • Hi friends.

    I downloaded & installed the latest version of Nmap.
    I want to use Nmap to ping & scan but dont know how to do. I can send parameters to CMD to execute namp & run "nmap -sn 192.168.1.1", but I dont want to use CMD.
    I want to send parameters to nmap directly .
    In the other word, Nmap is my process & "-sn" along with "192.168.1.1." are my arguments.

    Thanks a lot.
    Ya Ali.


  • Lifetime Qt Champion

    Hi,

    You have an example right there in "QProcess's documentation":http://qt-project.org/doc/qt-5/qprocess.html#details



  • Hi dear SGaist.
    I searched & read it & another examples but I cant get my expected results. In the other words, just nmap,exe workes but its arguments dont work.

    Here`s my simple code:

    mainwindow.h

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QProcess>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    QProcess *g_process;

    public slots:
    void ReadData();
    private slots:
    void on_pushButton_clicked();
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::ReadData()
    {
    ui->plainTextEdit->appendPlainText(QString(g_process->readAllStandardOutput()));
    }

    void MainWindow::on_pushButton_clicked()
    {
    g_process = new QProcess();

    QObject::connect (g_process, SIGNAL(readyReadStandardOutput()),
        this, SLOT(ReadData()));
    
    
    g_process->start (QLatin1String ("C:\\Progra~2\\Nmap\\nmap.exe"));
    g_process->waitForStarted();
    
    g_process->write ("nmap -sn 192.168.1.1\n\r");
    

    }
    @



  • Solved!
    I must send parameters when I call the start funcion.

    @
    QStringList args;
    args << "namp" << "-sn" << "192.168.1.1";
    g_process->start (QLatin1String ("C:\Progra~2\Nmap\nmap.exe"), args);
    @


  • Lifetime Qt Champion

    On a side note, with Qt you can use slashes for paths on all platforms


Log in to reply