Run NSTask twice!!!!!

cvcs1

Registered
Hey Guys, I am trying to make an app that ssh's into a server then cancels it and can start it again. I use shell scripts and NSTask to do this. However, I have a problem where once i've ran the script, It only runs the non-ssh parts again, and it does not NSLog again, eventhough it goes over the line.
Heres the code:
Code:
-(IBAction)tunnel:(id)sender{
//    [self performSelector:@selector(tunnelPlease) onThread:tunnel withObject:nil waitUntilDone:NO];
    tunnelThread = [[NSThread alloc] initWithTarget:self selector:@selector(tunnelPlease) object:nil];
    [tunnelThread start];
}

-(IBAction)stopTunnel:(id)sender{
    [tunnelThread cancel];
    [self performSelector:@selector(stopTunnelPlease)];
}

-(void)stopTunnelPlease{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];
    
    NSArray *arguments;
    NSString* newpath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"StopTunnel" ofType:@"sh"]];
    
    NSLog(@"shell script path: %@",newpath);
    arguments = [NSArray arrayWithObjects:newpath, 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];
    task = nil;
    NSLog (@"script returned:\n%@", string);     

}

-(void)tunnelPlease{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];
    
    NSArray *arguments;
    NSString* newpath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"StartTunnel" ofType:@"sh"]];
    
    NSLog(@"shell script path: %@",newpath);
    arguments = [NSArray arrayWithObjects:newpath, 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 (@"script returned:\n%@", string); 
    [pool drain];
    task = nil;
    [NSThread exit];
}

and the shells:
Start:
#!/bin/sh
networksetup -setsocksfirewallproxystate Airport on
ssh -D 9999 root@myIP
stop:
#!/bin/sh
networksetup -setsocksfirewallproxystate Airport off
killall ssh

Any ideas?? I've been brooding over this for hours now.
EDIT: It also seems that it doesn't run the ssh part AT ALL in the .app (outside of xcode)
 
Last edited:
Back
Top