我有用Xcode编写的Mac原生应用程序。我想在远程服务器上使用该应用程序执行一些SSH命令,并将结果返回给用户。使用本机Mac应用程序在远程Linux计算机上执行SSH命令。 (Obj-C)
是否有任何库/框架存在?那可能吗?
我有用Xcode编写的Mac原生应用程序。我想在远程服务器上使用该应用程序执行一些SSH命令,并将结果返回给用户。使用本机Mac应用程序在远程Linux计算机上执行SSH命令。 (Obj-C)
是否有任何库/框架存在?那可能吗?
您将需要使用NSTask
类来执行ssh
命令。
下面的代码是从this question的答案改编而来的。
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.
system(“ssh .....”)? – KevinDTimm 2012-08-02 22:17:31
@KevinDTimm我需要在远程机器上执行它! – Mojtaba 2012-08-03 16:44:55
不,它会运行'ssh',它会(可以)连接到远程机器并在那里运行命令。请参阅下面的答案以获得丰富的版本。 – KevinDTimm 2012-08-03 18:34:10