当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/247013.html

相关文章:

  • C++之STL库:string类(用法列举和总结)
  • 小程序中的大道理--综述
  • tlais智能学习辅助系统-修改部门功能实现
  • GLM: 自回归空白填充的多任务预训练语言模型
  • 函数递归所应满足的条件
  • Python入职某新员工大量使用Lambda表达式,却被老员工喷是屎山
  • Android Bitmap保存成至手机图片文件,Kotlin
  • frp V0.52.3 搭建
  • 最近数据分析面试的一点感悟...
  • ZYNQ_project:IIC_EEPROM
  • Leetcode 2940. Find Building Where Alice and Bob Can Meet
  • C++ 泛型编程,函数模版和类模版
  • 【封装UI组件库系列】封装Button图标组件
  • windows系统mobaxterm远程执行linux上ssh命令
  • debian 12 配置
  • AIGC创作系统ChatGPT网站源码、支持最新GPT-4-Turbo模型、GPT-4图片对话能力+搭建部署教程
  • Vue 中简易封装网络请求(Axios),包含请求拦截器和响应拦截器
  • git提交报错error: failed to push some refs to ‘git url‘
  • 【Python】(自定义函数)模块的相对路径导入
  • 巧妙之中见真章:深入解析常用的创建型设计模式
  • Selenium切换窗口、框架和弹出框window、ifame、alert
  • QT linux下应用程序打包
  • Java高级技术(单元测试)
  • leetCode 1080.根到叶路径上的不足节点 + 递归 + 图解
  • C++基础 -10- 类
  • 【软件测试】性能测试相关指标
  • Leetcode 2943. Maximize Area of Square Hole in Grid
  • qt 简单了解QHBoxLayout QVBoxLayout QFormLayout水平,垂直,表单布局管理器.
  • springboot中4级配置文件优先级
  • Python(八十九)函数的参数的内存分析