Follow us on...
Follow us on Twitter Follow us on Facebook
Register
Results 1 to 1 of 1
  1. #1
    cvcs1 is offline Registered User
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Run NSTask twice!!!!!

    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 by cvcs1; April 13th, 2011 at 11:25 PM.

 

 

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •