0

I have written a shell script and the purpose of that script is to capture packet using TCPDUMP and write that capture packet to .pcap file

Script is: TCPDumpScript.sh

echo "peter" | sudo -S tcpdump -i rv0 -n -s 0 -w dumpfile.pcap tcp

I am launching (TCPDumpScript.sh) using NSTask. Getting following output

tcpdump: WARNING: rv0: That device doesn't support promiscuous mode (BIOCPROMISC: Operation not supported on socket) tcpdump: WARNING: rv0: no IPv4 address assigned tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on rv0, link-type PKTAP (Packet Tap), capture size 65535 bytes

Problem 1: Using NSTask. "dumpfile.pcap" is being created. but when I run the same script using Terminal, its being created with desired captured packets.

Problem 2: Whenever making changes in TCPDumpScript.sh and then launching script using NSTask. I am getting following error "launch path not accessible"

But I do via terminal chmod +x TCPDumpScript.sh

Then I am able to launch this script without any error using NSTask.

So, my question is Can't I create file (Problem 1) "dumpfile.pcap" using NSTask ? And Do I always require to run permission change command on script file (Problem 2) ?

Please guide me.

  • (void)runScript:(NSArray*)arguments {

    dispatch_queue_t taskQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); dispatch_async(taskQueue, ^{

    self.isRunning = YES;
    
    @try {
    
        NSString *path  = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] pathForResource:@"TcpDumpScript" ofType:@"sh"]];         
        self.buildTask            = [[NSTask alloc] init];
        self.buildTask.launchPath = path;
        self.buildTask.arguments  = arguments;
    
        // Output Handling
        self.outputPipe               = [[NSPipe alloc] init];
        self.buildTask.standardOutput = self.outputPipe;
    
        [[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
    
        [[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification object:[self.outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){
    
            NSData *output = [[self.outputPipe fileHandleForReading] availableData];
            NSString *outStr = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
    
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.outPutTxt.string = [self.outPutTxt.string stringByAppendingString:[NSString stringWithFormat:@"\n%@", outStr]];
                // Scroll to end of outputText field
                NSRange range;
                range = NSMakeRange([self.outPutTxt.string length], 0);
                [self.outPutTxt scrollRangeToVisible:range];
            });
    
            [[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
        }];
    
        [self.buildTask launch];
    
        [self.buildTask waitUntilExit];
    }
    @catch (NSException *exception) {
        NSLog(@"Problem Running Task: %@", [exception description]);
    }
    @finally {
        [self.Start setEnabled:YES];
        [self.spinner stopAnimation:self];
        self.isRunning = NO;
    }
    

    }); }

peterDriscoll
  • 377
  • 4
  • 8
  • Show the code for the `NSTask` - how are you setting the current directory and environment? What permissions are required for the created file? – Wain Jun 20 '14 at 09:48
  • @Wain: How to set current directory and environment ? created file will be used for reading only. I used NSTask first time.So, I may be missing something. please suggest me. – peterDriscoll Jun 20 '14 at 10:27
  • issue resolved. Followed following steps Xcode->Editor->Add Build Phase->Add Run script Build Phase in Run Script added following lines chmod +x ${TARGET_BUILD_DIR}/appName.app/Resources/TcpDumpScript.sh Things are working fine now :). – peterDriscoll Jun 20 '14 at 11:28

0 Answers0