I'm trying to use the techniques from this answer to run a command on a remote machine over SSH, and collect the output. This is my code:
dispatch_async(dispatch_queue_create("closet ping", NULL), ^{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/ssh"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects:@"validusername:192.168.0.15", @"-t 'ps -C mpg123'", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"ping returned:\n%@", string);
...
Running this code produces this in my Xcode console:
parker:192.168.0.15: illegal option --
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
2014-04-15 13:09:56.640 NetworkBoard[26148:8443] ping returned:
Running whereis ssh
in the console shows /usr/bin/ssh
- exactly where I would expect it to be. It seems like the first argument in my arguments
array gets used as the command for some reason. I've tried changing it to this:
arguments = [NSArray arrayWithObjects:@"ssh", @"validusername:192.168.0.15", @"-t 'ps -C mpg123'", nil];
But then this is shown in the console:
ssh: Could not resolve hostname ssh: nodename nor servname provided, or not known
2014-04-15 13:11:48.574 NetworkBoard[26280:2103] ping returned:
So it seems that the first value is used - unless it's the value I want to be used. Any ideas?