1. 程式人生 > >iOS實現通過SSH2協議連結Linux伺服器,並執行相關指令

iOS實現通過SSH2協議連結Linux伺服器,並執行相關指令

使用方法:

SSHWrapper* sshWrapper = [[SSHWrapper alloc] init];
    NSError* error;

    [sshWrapper connectToHost:@"0.0.0.0" port:22 user:@"root" password:@"123" error:&error];

    if(error){//連線失敗
        NSLog(@"出錯");
    }else{ //連線成功
        NSLog(@"未出錯");
        //執行“ls -al”執行
        NSString * result = [sshWrapper executeCommand:@"ls -al" error:nil];
        NSLog(@"---%@",result);
        
        //執行“df -h”執行
        result = [sshWrapper executeCommand:@"df -h" error:nil];
        NSLog(@"---%@",result);
    }

執行結果: