iOS-NMSSH Simple learning about SFTP

iOS-NMSSH Simple learning about SFTP

Recently, I need to do an SFTP function in an internship project. In order to reduce development costs and online risks, I use the mature third-party library NMSSH to implement it. This is my first contact with NMSSH, so I wrote a simple study note.

About SSH

For an introduction to SSH and more detailed knowledge, I refer to
Jack LDZ - "Introduction to SSH and Two Methods of Remote Login"
https://blog.csdn.net/li528405176/article/details/82810342
SSH Baidu Encyclopedia
https:// baike.baidu.com/item/ssh/10407?fr=aladdin
OPENSSH
http://www.openssh.com/
CocoaDocs/NMSSH
http://cocoadocs.org/docsets/NMSSH/2.2.7/

About NMSSH

NMSSH is a clean, easy-to-use, unit tested framework for iOS and OSX that wraps libssh2. [Quoted from GitHub/NMSSH]
NMSSH is a clean, easy-to-use, unit tested framework for iOS and OSX that wraps libssh2.
The reference is the GitHub introduction of NMSSH: https://github.com/NMSSH/NMSSH

ssh open

Add a user for connection in System Preferences -> Users and Groups
and then go to System Preferences -> Sharing to make settings
Remote login configuration

code usage

CocoaPod integration:

pod 'NMSSH'

header file import

#import <NMSSH/NMSSH.h>

1. Connection

According to the principle of SSH connection establishment, parameters are required:
1. Server address
2. SFTP service port (default is 22)
3. Account and password
Note: SSH connection methods include account password connection and secret key connection. This article uses account password connection. After the secret key is connected, add it again. For the configuration of parameters, see above

Use NMSSHSession to create a connection instance, and then use NMSFTP to connect and operate

// 创建session实例,并配置参数
NMSSHSession *session = [NMSSHSession connectToHost:@"127.0.0.1:22" withUsername:@"user"];
/*
 服务器地址:127.0.0.1
 端口:22
 账号:user
 密码:pass
*/
if (session.isConnected) {
    
    
	NSLog(@"Successfully created a new session");
}
[session authenticateByPassword:@"pass"];
if (session.isAuthorized) {
    
    
	NSLog(@"Successfully authorized");
}
// 创建NMSFTP实例,连接SFTP服务器
NMSFTP *sftp = [[NMSFTP alloc]initWithSession:session];
if ([sftp connect]) {
    
    
	NSLog(@"SFTP connect success");
}else {
    
    
	NSLog(@"SFTP connect failure");
}

2. Get the file (folder) list

How to use NMSFTP

/**
 Read the contents of a file

 @param path An existing file path
 @returns File contents
 */
- (nullable NSData *)contentsAtPath:(nonnull NSString *)path;
NSArray *fileList = [sftp contentsOfDirectoryAtPath:@"/"]; 
// 获取根目录下的文件列表(通常为SD磁盘的列表,文件操作不在此处发生),需要到用户的文件夹下操作,以Public文件夹作为例子
NSArray *fileList = [sftp contentsOfDirectoryAtPath:@"/Users/user/Public"]; 

3. Download files

The method of using NMSSH

/**
 Read the contents of a file

 @param path An existing file path
 @returns File contents
 */
- (nullable NSData *)contentsAtPath:(nonnull NSString *)path;

For example: download file /Users/Test/Downloads/Test.txt

[sftp contentsAtPath:@"/Users/Test/Downloads/Test.txt"]// 获得一个NSData,将NSData以原来的文件名和类型,用文件操作方法存入即可

4. Upload files

The method of using NMSSH

/**
 Overwrite the contents of a file

 If no file exists, one is created.

 @param contents Bytes to write
 @param path File path to write bytes at
 @returns Write success
 */
- (BOOL)writeContents:(nonnull NSData *)contents toFileAtPath:(nonnull NSString *)path;

For example: Upload the Test.txt file in NSBundle to the /Users/Test/Public folder

// 拿到文件路径,转化为NSData后上传到目标路径
NSString *pathBundle = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"txt"];
NSData *data = [[NSData alloc]initWithContentsOfFile:pathBundle];
if ([sftp writeContents:data toFileAtPath:@"/Users/Test/Public/Test.txt"]) {
    
    
    NSLog(@"SFTP updata success");
}else {
    
    
    NSLog(@"SFTP updata failure");
}

Note: Both uploading and downloading files have methods to get progress

5. Disconnect

// 断开SFTP连接
[sftp disconnect];

postscript

I am a novice in iOS development, if there is something wrong, welcome dalao to point it out.
This article will continue to be updated as the study progresses.
simple demo

おすすめ

転載: blog.csdn.net/qq_38718912/article/details/101697042