QSsh使用

本文详细介绍了一个使用C++和QT库实现的SSH远程连接及文件传输的应用实例。该应用通过QT SSH库实现与远程服务器的连接,进行文件上传,并在服务器上执行shell命令。文章深入解析了SshConnection、SshRemoteProcess和SftpChannel等关键组件的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

04-03
关于QSsh的问题,以下是整理的相关信息: --- QSsh 是一个与 SSH 协议相关的概念或工具。SSH(Secure Shell)是一种网络安全协议,主要用于在网络上传输加密数据以及远程登录计算机系统。以下是对 QSsh 的解释及相关内容整合: ### 方法一:理解 QSsh 的定义 QSsh 并不是一个官方标准术语,但在某些技术社区中可能指代一种基于 Qt 框架实现的 SSH 客户端库或工具。它通常用于开发跨平台的应用程序以支持安全连接。 - 如果您正在寻找如何在项目中集成 QSsh 功能,则需要了解其依赖项及安装方式。 - 常见场景包括嵌入式设备管理、自动化脚本执行等。 --- ### 方法二:QSsh 的应用场景 1. **Qt 开发环境中的应用** 对于使用 Qt 进行 GUI 应用开发的技术人员来说,QSsh 提供了一种便捷的方式来处理 SSH 数据流。例如,在图形界面下建立服务器连接或者传输文件。 2. **替代传统命令行工具** 虽然 OpenSSH 已经非常成熟稳定,但当开发者希望构建自己的定制化解决方案时,可以考虑采用类似 QSsh 的第三方组件来完成特定需求。 3. **多操作系统兼容性测试** 利用 QSsh 编写跨 Windows/Linux/macOS 等不同系统的统一接口代码片段如下所示: ```cpp #include <QCoreApplication> #include "qsstools.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 初始化 QSsh 实例对象 SshConnectionParameters params; params.setHost("example.com"); params.setUser("root"); if (connectToSshServer(params)) { qDebug() << "Connected successfully!"; } else { qDebug() << "Failed to connect."; } return a.exec(); } ``` --- ### 方法三:常见问题排查指南 如果您遇到有关 QSsh 的错误提示,请参考以下建议解决办法: 1. **检查是否正确配置了密钥认证机制** 当尝试无密码访问目标主机失败时,请确保公私钥已妥善交换并且权限设置适当。具体操作步骤可查阅相关文档说明链接[此处](https://doc.qt.io/qt-solutions/qtsolution-ssh.html)。 2. **验证网络连通状况良好与否** 使用 `ping` 或者其他诊断手段确认源地址能够顺利抵达目的节点之前不存在防火墙阻挡等情况发生。 3. **更新至最新版本避免漏洞风险** 定期关注上游维护者的发布动态及时升级软件包从而减少潜在安全隐患影响业务正常运行。 ---
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值