NSTask with complexe command
NSTask with complexe command
- Subject: NSTask with complexe command
- From: Vincent Pottier <email@hidden>
- Date: Thu, 18 Mar 2004 14:05:15 +0100
Hello,
I'm writing an application who notify me when a new network connection
is set. I've find a good way to do so: using tcpdump (available since
last security uptdate) via command:
sudo tcpdump -nqt 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0' //in fact
notify when connection closed to
So to use this command in cocoa i've write a daemon launch with
previlege (executeWithPrivilege...) calling tcpdump via NSTask and
NSFileHandle. And here is my problem, tcpdump is launch with sucess,
but no way to get the output, i'm using readInBackgroundAndNotify (Full
code at mail end) but gotData: function is never call. So i've try with
"tcpdump ip", gotData: fonction is call regulary, but no data is
available (i've lot of active connection).
Does someone know how to get output? Thanks in advance and sorry for
my poor English.
Vincent
- (void)myFunction
{
NSTask *tcpdump=[[NSTask alloc] init];
NSPipe *pipe=[[NSPipe alloc] init];
handle = [[pipe fileHandleForReading] retain];
[tcpdump setLaunchPath:@"/usr/sbin/tcpdump"];
[tcpdump setArguments:[NSArray arrayWithObjects:@"-nqt",
@"tcp[tcpflags] & (tcp-syn|tcp-fin) != 0", nil]];
[tcpdump setStandardOutput:pipe];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(gotData:) name:NSFileHandleDataAvailableNotification
object:handle]; // async read
[handle waitForDataInBackgroundAndNotify];
[tcpdump launch];
[tcpdump waitUntilExit];
}
- (void)gotData:(NSNotification *)myNotification
{
NSData *messageData = [[myNotification userInfo] objectForKey
NSFileHandleNotificationDataItem];
NSString *string=[[NSString alloc] initWith
Data:messageData
encoding:NSASCIIStringEncoding];
NSLog(@"String data: %@", string);
[handle waitForDataInBackgroundAndNotify];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.