2

I have a Mac Native app written with Xcode. I want to execute some SSH command using that application on remote servers and get the result back to user.

Is there any library/Framework exist for that? Is that possible?

2
  • @KevinDTimm I need to execute that on Remote machine! Commented Aug 3, 2012 at 16:44
  • No, it will run ssh, which will (can) connect to a remote machine and run commands there. See the answer below for a fleshed out version. Commented Aug 3, 2012 at 18:34

1 Answer 1

6

You will want to use the NSTask class to execute an ssh command.

The following code was adapted from the answer to 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.
Sign up to request clarification or add additional context in comments.

2 Comments

This will execute the <command> on local machine (Mac OS) but I need to connect to a remote machine which running Linux.
So sorry, I forgot an argument! There needs to be the user and hostname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.