QSsh使用

QSsh源码可以在网上下载,这里就不介绍

.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <sshconnection.h>
#include <sshremoteprocess.h>
#include <sftpchannel.h>
using namespace QSsh;

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

    void handleRemoteStdout();
    void handleConnected();
    void handleShellStarted();
    void handleRemoteStderr();
    void handleChannelInitialized();
    void handleChannelInitializationFailure(const QString &reason);
private:
    Ui::Dialog *ui;

    SshConnection *m_connection;
    QSharedPointer<SshRemoteProcess> m_shell;
    SftpChannel::Ptr m_channel;
};

#endif // DIALOG_H

.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QProcess>
#include <QTimer>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    m_connection = nullptr,
    ui->lineEdit_ip->setText("192.168.0.0");
    ui->lineEdit_name->setText("xx");
    ui->lineEdit_pass->setText("xxxxxx");
}

Dialog::~Dialog()
{
    delete ui;
    delete m_connection;
}

void Dialog::on_pushButton_clicked()
{
    SshConnectionParameters parameters;
    parameters.port = 22;
    parameters.userName = ui->lineEdit_name->text();
    parameters.password = ui->lineEdit_pass->text();
    parameters.host = ui->lineEdit_ip->text();
    parameters.timeout = 10;
    parameters.authenticationType = QSsh::SshConnectionParameters::AuthenticationTypePassword;
    if(m_connection == nullptr) {
        m_connection = new SshConnection(parameters);
        connect(m_connection, SIGNAL(connected()), SLOT(handleConnected()));
    }
    m_connection->connectToHost();
}

void Dialog::handleConnected()
{
    //传送文件
    m_channel = m_connection->createSftpChannel();
    connect(m_channel.data(), SIGNAL(initialized()), this, SLOT(handleChannelInitialized()));
    connect(m_channel.data(), SIGNAL(channelError(QString)), this, SLOT(handleChannelInitializationFailure(QString)));
    //初始化传送文件
    m_channel->initialize();

    //执行shell,这里先不执行,所以未start
    m_shell = m_connection->createRemoteShell();
    connect(m_shell.data(), SIGNAL(started()), SLOT(handleShellStarted()));
    connect(m_shell.data(), SIGNAL(readyReadStandardOutput()), SLOT(handleRemoteStdout()));
    connect(m_shell.data(), SIGNAL(readyReadStandardError()), SLOT(handleRemoteStderr()));
}

void Dialog::handleRemoteStdout()
{
    QString str_out = QString::fromUtf8(m_shell->readAllStandardOutput());
    QTimer timer;
    qDebug()<< "str out" << str_out;
    timer.start(1000);
    if(timer.isActive()) timer.stop();
    if(str_out.contains("password for"))
        m_shell->write(ui->lineEdit_pass->text().toLatin1().data());
}

void Dialog::handleShellStarted()
{
    m_shell->write(QString("sudo xxxx\n").toLatin1().data());
}

void Dialog::handleRemoteStderr()
{
    qDebug() << "shell standard err"<< QString::fromLocal8Bit(m_shell->readAllStandardError());
}

void Dialog::handleChannelInitialized()
{
    //传送到服务器的tmp目录下,可以传送任意类型文件
    m_channel->uploadFile(QString("E:/xx.txt").toLatin1().data(), "/tmp/xx.txt", SftpOverwriteExisting);
    //传送完成,开始执行指令
    QTimer timer;
    timer.start(5000);
    if(timer.isActive()) timer.stop();
    m_shell->start();
}

void Dialog::handleChannelInitializationFailure(const QString &reason)
{
    qDebug() << "Could not initialize SFTP channel: " << reason;
}
发布了34 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Sparrow_du/article/details/91951830