// CSSHPROCESS.h #ifndef CSSHPROCESS_H #define CSSHPROCESS_H
#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
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
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
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
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
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
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;
}