0

I am using the below code to get tcpdump output of a URL in iOS, but it is printing in console, how can i get the response to NSString object?

system("tcpdump -i en1 -A -vvv host www.facebook.com");
Sudheer Kumar Palchuri
  • 2,919
  • 1
  • 28
  • 38

1 Answers1

0

I think tcpdump have output file param, or you can use output redirect, and then read all from saved file

tcpdump -l -i en0 -A -vvv host www.facebook.com  > /tmp/facebook_packets.txt

tmp path just for example, use any you like

edit:

and alternative way, redirect output to NSPipe, see answer here How to get the log from system();?

Community
  • 1
  • 1
sage444
  • 5,661
  • 4
  • 33
  • 60
  • NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; [[NSFileManager defaultManager]createFileAtPath:[documentsDirectory stringByAppendingString:@"/output.txt"] contents:nil attributes:nil]; NSString *strCmd=[NSString stringWithFormat:@"tcpdump -i en1 -vvv -A host www.facebook.com > /%@",[documentsDirectory stringByAppendingString:@"/output.txt"]]; system([strCmd UTF8String]); – Sudheer Kumar Palchuri Apr 07 '14 at 12:06
  • I tried with above code, but the file is not creating at the location, how can i fix this? – Sudheer Kumar Palchuri Apr 07 '14 at 12:07
  • I see you missed `-l` flag – sage444 Apr 07 '14 at 12:18
  • 1
    and you have two slashes in path `//output.txt` – sage444 Apr 07 '14 at 12:20
  • @PalchuriSudheerKumar please, also check link I added to answer, I guess this also can help you – sage444 Apr 07 '14 at 12:26
  • I tried below code using NSTask, but getting error when i run the code, please help me to fix this – Sudheer Kumar Palchuri Apr 07 '14 at 13:50
  • NSTask *task = [[NSTask alloc] init]; [task setLaunchPath: @"tcpdump"]; [task setArguments: [[NSArray alloc] initWithObjects: @"-i",@"en1",@"-A",@"-vvv",@"host",@"www.facebook.com", nil]]; NSPipe *pipe= [NSPipe pipe]; [task setStandardOutput: pipe]; file = [pipe fileHandleForReading]; [task launch]; – Sudheer Kumar Palchuri Apr 07 '14 at 13:50
  • also, you must put full path to tcpdump – sage444 Apr 07 '14 at 13:56
  • Error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' – Sudheer Kumar Palchuri Apr 07 '14 at 14:49
  • 1
    try `/usr/sbin/tcpdump` or search to get real path to executable – sage444 Apr 07 '14 at 14:51
  • Crash issue resolved with your suggestion, after the execution of code , getting error in console, please see below, any idea why it is? – Sudheer Kumar Palchuri Apr 07 '14 at 15:15
  • tcpdump: en1: You don't have permission to capture on that device ((cannot open BPF device) /dev/bpf4: Permission denied) – Sudheer Kumar Palchuri Apr 07 '14 at 15:15