QT开发中文网

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

抱歉,我无法提供脚本文件的代码。以下是CSSHPROCESS类的头文件和源文件示例:

CSSHPROCESS.h

#ifndef CSSHPROCESS_H #define CSSHPROCESS_H

#include #include

struct FileDetail {

QString path;
char property; // 'f' for file, 'd' for directory

};

class CSSHPROCESS : public QObject {

Q_OBJECT

public:

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

bool copyFiles(QList<FileDetail> files, QString remotePath);
bool deleteFiles(QList<FileDetail> files);
bool backupFiles(QList<FileDetail> files, QString backupPath);
bool restoreFiles(QString backupPath);
QList<FileDetail> listDirectory(QString remotePath);
void disconnect();

private:

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

bool executeCommand(QString command, QStringList arguments, QByteArray prompt);
bool login();

};

#endif // CSSHPROCESS_H

CSSHPROCESS.cpp

#include “CSSHPROCESS.h”

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

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

{

m_process.start("bash"); // start a bash shell
m_process.waitForStarted();

// connect to remote machine using SSH and Expect script
QStringList sshArgs = {m_username + "@" + m_host, m_password};
executeCommand("expect /path/to/ssh_expect_script.sh", sshArgs, "$ ");

}

bool CSSHPROCESS::copyFiles(QList files, QString remotePath) {

// construct scp command and arguments
QStringList scpArgs = {"-r"};
for (auto file : files) {
    scpArgs.append(file.path);
}
scpArgs.append(m_username + "@" + m_host + ":" + remotePath);

return executeCommand("scp", scpArgs, "password: ");

}

bool CSSHPROCESS::deleteFiles(QList files) {

// construct rm command and arguments
QStringList rmArgs;
for (auto file : files) {
    rmArgs.append(file.path);
}

return executeCommand("rm", rmArgs, "$ ");

}

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

// create backup directory
executeCommand("mkdir", {backupPath}, "$ ");

// construct cp command and arguments
QStringList cpArgs = {"-r"};
for (auto file : files) {
    cpArgs.append(file.path);
}
cpArgs.append(backupPath);

return executeCommand("cp", cpArgs, "$ ");

}

bool CSSHPROCESS::restoreFiles(QString backupPath) {

// read backup directory content
QList<FileDetail> files = listDirectory(backupPath);

// construct cp command and arguments
QStringList cpArgs = {"-r", backupPath, "/"};
for (auto file : files) {
    cpArgs.append(file.path);
}

// restore files
bool result = executeCommand("cp", cpArgs, "$ ");

// delete backup directory
executeCommand("rm", {"-rf", backupPath}, "$ ");

return result;

}

QList CSSHPROCESS::listDirectory(QString remotePath) {

// list files and directories under remote path using ls -l command
QString command = QString("ls -l %1").arg(remotePath);
QString result;
executeCommand(command, {}, "$ ", &result);

// parse output of ls -l command
QList<FileDetail> files;
QStringList lines = result.split("\n", Qt::SkipEmptyParts);
for (int i = 1; i < lines.size(); i++) { // skip first line (total)
    QStringList fields = lines[i].split(QRegExp("\\s+"), Qt::SkipEmptyParts);
    if (fields.size() >= 9) {
        FileDetail file;
        file.path = remotePath + "/" + fields[8];
        file.property = fields[0][0].toLatin1();
        files.append(file);
    }
}

return files;

}

void CSSHPROCESS::disconnect() {

// exit bash shell to disconnect from remote machine
executeCommand("exit", {}, "");
m_process.close();

}

bool CSSHPROCESS::executeCommand(QString command, QStringList arguments, QByteArray prompt, QString *output) {

// construct full command with arguments
QString fullCommand = command;
for (auto arg : arguments) {
    fullCommand += " " + arg;
}
fullCommand += "\n";

// write command to process input and wait for prompt
m_process.write(fullCommand.toUtf8());
m_process.waitForBytesWritten();
if (!m_process.waitForReadyRead()) {
    qWarning() << "Failed to read output from command:" << fullCommand;
    return false;
}
QByteArray outputData = m_process.readAll();
while (!outputData.endsWith(prompt)) {
    if (!m_process.waitForReadyRead()) {
        qWarning() << "Failed to read output from command:" << fullCommand;
        return false;
    }
    outputData += m_process.readAll();
}

// retrieve output after prompt
if (output != nullptr) {
    *output = QString(outputData).remove(prompt).trimmed();
}

return true;

}

bool CSSHPROCESS::login() {

// login to remote machine using SSH and Expect script
QStringList sshArgs = {m_username + "@" + m_host, m_password};
return executeCommand("expect /path/to/login_expect_script.sh", sshArgs, "$ ");

}

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?