Выполните команду SSH на удаленной машине Linux с помощью собственного приложения Mac. (Obj-C),
У меня есть приложение Mac Native, написанное с помощью Xcode. Я хочу выполнить некоторую команду SSH с помощью этого приложения на удаленных серверах и вернуть результат пользователю.
Существует ли какая-либо библиотека /Framework для этого? Это возможно?
1 ответ
Решение
Вы хотите использовать 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.