使用Native Mac App在远程Linux机器上执行SSH命令.(对象 - C)

Moj*_*aba 2 linux ssh xcode objective-c

我有一个用Xcode编写的Mac Native应用程序.我想在远程服务器上使用该应用程序执行一些SSH命令,并将结果返回给用户.

是否存在任何库/框架?那可能吗?

Lil*_*ung 7

您将需要使用NSTask该类来执行ssh命令.

以下代码改编自这个问题的答案.

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/ssh"]; // Tell the task to execute the ssh command
[task setArguments: [NSArray arrayWithObjects: @"<user>:<hostname>", @"<command>"]]; // Set the arguments for ssh to contain only your command. If other configuration is necessary, see the ssh(1) man page.

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading]; // This file handle is a reference to the output of the ssh command

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; // This string now contains the entire output of the ssh command.
Run Code Online (Sandbox Code Playgroud)