Getting IPs of all the remote machines
Getting IPs of all the remote machines
- Subject: Getting IPs of all the remote machines
- From: Lorenzo <email@hidden>
- Date: Sat, 26 Apr 2003 11:33:36 +0200
Hi,
I would like to say Thanks to Charles Srstka, Mark Dalrymple, Tom Sutcliffe,
Finlay Dobbie who helped me to build the following routine.
I would like to share it, maybe someone could like to know this technic.
// This routine asks the shell to execute a general unix string command and
// takes the result through a pipeline. The specific routine here below
// returns a list of the IPs of all the remote machines connected to the
// local machine. (currentHostIPAddress is the IP of the local machine.)
NSString *currentHostIPAddress = [[NSHost currentHost] address];
NSString *commandLine = [NSString stringWithFormat:@"netstat -f inet -n |
grep '%@' | grep ESTABLISHED | awk '{ print $5 }'", currentHostIPAddress];
NSArray *args = [NSArray arrayWithObjects:@"-c", commandLine, nil];
NSLog(@"currentHostIPAddress: %@", currentHostIPAddress);
NSTask *task = [[NSTask alloc] init];
NSPipe *thePipe = [NSPipe pipe];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:args];
[task setStandardOutput:thePipe];
[task launch];
[task waitUntilExit];
if([task terminationStatus] != noErr){
[task release];
NSLog(@"Error from the shell.");
return;
}
NSData *dataOut = [[[task standardOutput] fileHandleForReading]
availableData];
if(dataOut){
NSString *theIDList = [[NSString alloc] initWith
Data:dataOut
encoding:NSASCIIStringEncoding];
NSLog(@"The output of netstat is: %@", theIDList);
[theIDList release];
}
else NSLog(@"The output of netstat is NULL");
[task release];
Best Regards
--
Lorenzo
email: email@hidden
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
_______________________________________________
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.