帮我写一段C语言在linux 下基于libssh的sftp登录 获取文件列表和下载文件的代码,封装成函数
时间: 2023-04-02 18:00:50 浏览: 134
好的,以下是基于libssh的sftp登录、获取文件列表和下载文件的C语言代码,已封装成函数:
```c
#include <libssh/libssh.h>
#include <libssh/sftp.h>
int sftp_login(const char *host, const char *username, const char *password, ssh_session *session, sftp_session *sftp)
{
// 初始化ssh会话
*session = ssh_new();
if (*session == NULL) {
return SSH_ERROR;
}
// 设置ssh连接选项
ssh_options_set(*session, SSH_OPTIONS_HOST, host);
ssh_options_set(*session, SSH_OPTIONS_USER, username);
ssh_options_set(*session, SSH_OPTIONS_PASSWORD, password);
// 连接ssh服务器
int rc = ssh_connect(*session);
if (rc != SSH_OK) {
ssh_free(*session);
return rc;
}
// 认证
rc = ssh_userauth_password(*session, NULL, password);
if (rc != SSH_AUTH_SUCCESS) {
ssh_disconnect(*session);
ssh_free(*session);
return rc;
}
// 初始化sftp会话
*sftp = sftp_new(*session);
if (*sftp == NULL) {
ssh_disconnect(*session);
ssh_free(*session);
return SSH_ERROR;
}
// 开启sftp会话
rc = sftp_init(*sftp);
if (rc != SSH_OK) {
sftp_free(*sftp);
ssh_disconnect(*session);
ssh_free(*session);
return rc;
}
return SSH_OK;
}
int sftp_get_file_list(sftp_session sftp, const char *path, char ***file_list)
{
// 打开目录
sftp_dir dir = sftp_opendir(sftp, path);
if (dir == NULL) {
return SSH_ERROR;
}
// 获取文件列表
int count = ;
*file_list = NULL;
while (1) {
sftp_attributes attributes = sftp_readdir(sftp, dir);
if (attributes == NULL) {
break;
}
if (strcmp(attributes->name, ".") == || strcmp(attributes->name, "..") == ) {
sftp_attributes_free(attributes);
continue;
}
count++;
*file_list = realloc(*file_list, count * sizeof(char *));
(*file_list)[count - 1] = strdup(attributes->name);
sftp_attributes_free(attributes);
}
// 关闭目录
sftp_closedir(dir);
return count;
}
int sftp_download_file(sftp_session sftp, const char *remote_path, const char *local_path)
{
// 打开远程文件
sftp_file remote_file = sftp_open(sftp, remote_path, O_RDONLY, );
if (remote_file == NULL) {
return SSH_ERROR;
}
// 创建本地文件
FILE *local_file = fopen(local_path, "wb");
if (local_file == NULL) {
sftp_close(remote_file);
return SSH_ERROR;
}
// 下载文件
char buffer[1024];
int nbytes;
while ((nbytes = sftp_read(remote_file, buffer, sizeof(buffer))) > ) {
fwrite(buffer, 1, nbytes, local_file);
}
// 关闭文件
fclose(local_file);
sftp_close(remote_file);
return SSH_OK;
}
```
使用方法:
```c
ssh_session session;
sftp_session sftp;
char **file_list;
int count;
// 登录sftp服务器
if (sftp_login("host", "username", "password", &session, &sftp) != SSH_OK) {
fprintf(stderr, "sftp login failed\n");
return -1;
}
// 获取文件列表
count = sftp_get_file_list(sftp, "/path/to/dir", &file_list);
if (count < ) {
fprintf(stderr, "get file list failed\n");
sftp_free(sftp);
ssh_disconnect(session);
ssh_free(session);
return -1;
}
for (int i = ; i < count; i++) {
printf("%s\n", file_list[i]);
}
for (int i = ; i < count; i++) {
free(file_list[i]);
}
free(file_list);
// 下载文件
if (sftp_download_file(sftp, "/path/to/remote/file", "/path/to/local/file") != SSH_OK) {
fprintf(stderr, "download file failed\n");
sftp_free(sftp);
ssh_disconnect(session);
ssh_free(session);
return -1;
}
// 关闭sftp会话和ssh会话
sftp_free(sftp);
ssh_disconnect(session);
ssh_free(session);
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)