sftp学习

什么是sftp?

sftp的简单操作

  • 远程连接
int RobostSftp::Init(QString ip,QString port,QString user_name, QString user_password)
{   
    int rc;
    session = ssh_new();
    if (!session) {
        fprintf(stderr, "ssh initialization failed\n");
        //        return 1;
    }
    /* Set session options */
    ip_=ip;
    port_=port;
    user_name_=user_name;
    user_password_=user_password;

    ssh_options_set(session, SSH_OPTIONS_PORT_STR, port.toStdString().c_str());
    ssh_options_set(session, SSH_OPTIONS_HOST, ip.toStdString().c_str());
    ssh_options_set(session, SSH_OPTIONS_USER, user_name.toStdString().c_str());
    rc = ssh_connect(session);
    if (rc != SSH_OK) {
        dialog->setLogTagText("警告");
        dialog->setLogText("没有连接到"+ip+"主机!!!");
        dialog->exec();
        fprintf(stderr, "ssh connection failed (%d)\n", rc);
        return 1;
    }
    /* Authenticate with password */
    rc = ssh_userauth_password(session, NULL, user_password.toStdString().c_str());
    if (rc != SSH_AUTH_SUCCESS) {
        dialog->setLogTagText("警告");
        dialog->setLogText("SFTP远程连接密码错误!!!");
        dialog->exec();
   //     fprintf(stderr, "ssh authentication failed (%d)\n", rc);
        return 1;
    }
    /* Initialize SFTP session */
    sftp = sftp_new(session);
    if (!sftp) {
         dialog->setLogTagText("警告");
         dialog->setLogText("SFTP初始化失败!!!");
         dialog->exec();
//        fprintf(stderr, "sftp initialization failed\n");
           return 1;
    }
    /* Initialize SFTP subsystem */
    rc = sftp_init(sftp);
    if (rc != SSH_OK) {
           dialog->setLogTagText("警告");
           dialog->setLogText("SFTP初始化失败!!!");
           dialog->exec();
 //          fprintf(stderr, "sftp initialization failed (%d)\n", rc);
           return 1;
    }
    fileTransferThread->init(ip,port,user_name,user_password);
    fileTransferThread->start();
    return 0;
}

  • 通过本机删除远程主机文件
void RobostSftp::deleteDir(QString filepath)
{
    QString path="/opt/hura/dataset/map/053101/path";
    int rc=sftp_rmdir(sftp,path.toStdString().c_str());

    if(rc!=SSH_OK) qDebug() << "删除失败: " << sftp_get_error(sftp);
    else qDebug()<<"删除成功";
    recursiveDelete(sftp,path.toStdString().c_str());//通过建立的sftp会话去递归的删除远程文件夹
}

void RobostSftp::recursiveDelete(sftp_session sftp, const char *path) {
    sftp_dir dir;
    sftp_attributes attributes;

    dir = sftp_opendir(sftp, path);
    if (!dir) {
        qDebug()<<"无法打开目录 " << path ;
        return;
    }

    while ((attributes = sftp_readdir(sftp, dir)) != NULL) {
        if (strcmp(attributes->name, ".") == 0 || strcmp(attributes->name, "..") == 0) {
            continue;
        }

        char *filepath;
        asprintf(&filepath, "%s/%s", path, attributes->name);

        if (attributes->type == SSH_FILEXFER_TYPE_DIRECTORY) {
            // 如果是子文件夹,递归删除
            recursiveDelete(sftp, filepath);
        } else {
            // 如果是文件,直接删除
            int rc = sftp_unlink(sftp, filepath);
            if (rc != SSH_OK) {
                qDebug()<<"无法删除文件 " << path ;
            } else {
                qDebug()<< "文件 " << filepath << " 删除成功"  ;
            }
        }

        free(filepath);
    }

    sftp_closedir(dir);

    // 删除空目录
    int rc = sftp_rmdir(sftp, path);
    if (rc != SSH_OK) {
        qDebug()<<"无法删除目录 " << path ;
    } else {
        qDebug()<<"删除成功 " << path ;
    }
}

你可能感兴趣的:(计算机网络)