objective c - Running shell script using NSTask in Mac OS -
objective c - Running shell script using NSTask in Mac OS -
i have written shell script , purpose of script capture packet using tcpdump , write capture packet .pcap file
script is: tcpdumpscript.sh
echo "peter" | sudo -s tcpdump -i rv0 -n -s 0 -w dumpfile.pcap tcp
i launching (tcpdumpscript.sh) using nstask. getting next output
tcpdump: warning: rv0: device doesn't back upwards promiscuous mode (biocpromisc: operation not supported on socket) tcpdump: warning: rv0: no ipv4 address assigned tcpdump: verbose output suppressed, utilize -v or -vv total protocol decode listening on rv0, link-type pktap (packet tap), capture size 65535 bytes
problem 1: using nstask. "dumpfile.pcap" beingness created. when run same script using terminal, beingness created desired captured packets.
problem 2: whenever making changes in tcpdumpscript.sh , launching script using nstask. getting next error "launch path not accessible"
but via terminal chmod +x tcpdumpscript.sh
then able launch script without error using nstask.
so, question can't create file (problem 1) "dumpfile.pcap" using nstask ? , require run permission alter 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 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; }
}); }
objective-c xcode shell networking nstask
Comments
Post a Comment