QT开发中文网

给我封装一个类,名称为CSSHPROCESS,类的作用为实现SSH与远程机器交互,在调用这个类创建实例时会传入三个参数:远程主机的用户名,主机地址,登录密码。创建实例时函数内部会使用QProcess将这几个参数作为QStringList传入连接远程机器的脚本中并运行连接,接下来的操作命令都是在使用ssh和expect与远程主机连接之后进行交互。要操作的的方法函数有: 1.复制文件,传入本地文件路径和远程机器文件路径,可以将远程机器文件复制到本地,也可以将本地文件发送到远程机器。其中要复制的...

// CSSHPROCESS.h #ifndef CSSHPROCESS_H #define CSSHPROCESS_H

#include #include

struct FileDetail {

QString path;
QString attribute;

};

class CSSHPROCESS : public QObject {

Q_OBJECT

public:

explicit CSSHPROCESS(QString username, QString host, QString password, QObject *parent = nullptr);
~CSSHPROCESS();

QList<FileDetail> printDirectory(QString remotePath);
bool copyFile(QList<FileDetail> localFiles, QList<FileDetail> remoteFiles);
bool deleteFile(QList<FileDetail> files);
bool backupFile(QList<FileDetail> files, QString backupPath);
bool restoreFile(QList<FileDetail> files, QString backupPath);

private:

QString m_username;
QString m_host;
QString m_password;
QProcess m_process;

void runCommand(QString command, QStringList arguments);
QString getOutput();
QString getError();

};

#endif // CSSHPROCESS_H

// CSSHPROCESS.cpp #include “CSSHPROCESS.h” #include #include #include #include

CSSHPROCESS::CSSHPROCESS(QString username, QString host, QString password, QObject *parent)

: QObject(parent), m_username(username), m_host(host), m_password(password)

{

QStringList arguments;
arguments << "-l" << m_username << m_host << "expect_ssh.sh" << m_password;
m_process.start("ssh", arguments);

if (!m_process.waitForStarted()) {
    qCritical() << "SSH connection failed";
}

}

CSSHPROCESS::~CSSHPROCESS() {

if (m_process.isOpen()) {
    m_process.write("exit\n");
    m_process.waitForFinished(3000);
    m_process.close();
}

}

void CSSHPROCESS::runCommand(QString command, QStringList arguments) {

m_process.write((command + " " + arguments.join(" ") + "\n").toUtf8());
QString output = getOutput();
QString error = getError();

if (!error.isEmpty()) {
    qCritical() << error;
}
if (!output.isEmpty()) {
    qDebug() << output;
}

}

QString CSSHPROCESS::getOutput() {

m_process.waitForReadyRead(3000);
QByteArray output = m_process.readAllStandardOutput();
QString outputString = QString::fromUtf8(output).trimmed();
return outputString;

}

QString CSSHPROCESS::getError() {

m_process.waitForReadyRead(3000);
QByteArray error = m_process.readAllStandardError();
QString errorString = QString::fromUtf8(error).trimmed();
return errorString;

}

QList CSSHPROCESS::printDirectory(QString remotePath) {

QList<FileDetail> fileList;
runCommand("ls", QStringList() << "-l" << remotePath);

QString result = getOutput();
QStringList lines = result.split('\n');
for (int i = 0; i < lines.count(); i++) {
    QString line = lines.at(i);
    QStringList parts = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
    if (parts.size() >= 9) {
        QString attribute = parts[0];
        QString path = parts[parts.count() - 1];
        FileDetail detail = {path, attribute};
        fileList.append(detail);
    }
}

return fileList;

}

bool CSSHPROCESS::copyFile(QList localFiles, QList remoteFiles) {

bool success = true;

for (int i = 0; i < localFiles.count(); i++) {
    FileDetail localFile = localFiles.at(i);
    FileDetail remoteFile = remoteFiles.at(i);

    QFileInfo localInfo(localFile.path);
    QString localFileName = localInfo.fileName();
    QString remoteFileName = remoteFile.path + "/" + localFileName;

    if (localInfo.isDir()) {
        runCommand("scp", QStringList() << "-r" << localFile.path << m_username + "@" + m_host + ":" + remoteFile.path);
    } else {
        runCommand("scp", QStringList() << localFile.path << m_username + "@" + m_host + ":" + remoteFile.path);
    }

    QString error = getError();
    if (!error.isEmpty()) {
        qCritical() << "SCP error: " << error;
        success = false;
    }
}

return success;

}

bool CSSHPROCESS::deleteFile(QList files) {

bool success = true;

for (int i = 0; i < files.count(); i++) {
    FileDetail file = files.at(i);
    QString path = file.path;

    if (file.attribute == "d") {
        runCommand("rm", QStringList() << "-r" << path);
    } else {
        runCommand("rm", QStringList() << path);
    }

    QString error = getError();
    if (!error.isEmpty()) {
        qCritical() << "Delete error: " << error;
        success = false;
    }
}

return success;

}

bool CSSHPROCESS::backupFile(QList files, QString backupPath) {

QDir dir(backupPath);
if (!dir.exists()) {
    dir.mkpath(".");
}

QString backupFolder = backupPath + "/" + QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
QDir(backupFolder).mkpath(".");

bool success = copyFile(files, QList<FileDetail>() << {backupFolder});

return success;

}

bool CSSHPROCESS::restoreFile(QList files, QString backupPath) {

bool success = true;

for (int i = 0; i < files.count(); i++) {
    FileDetail file = files.at(i);
    QString path = file.path;

    QString backupFile = backupPath + "/" + file.path;
    if (QFileInfo(backupFile).isDir()) {
        runCommand("cp", QStringList() << "-r" << backupFile << path);
    } else {
        runCommand("cp", QStringList() << backupFile << path);
    }

    QString error = getError();
    if (!error.isEmpty()) {
        qCritical() << "Restore error: " << error;
        success = false;
    }
}

return success;

}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=189

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?