Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何捕获system()启动的流程的标准输出_Iphone_Ios_Io Redirection - Fatal编程技术网

Iphone 如何捕获system()启动的流程的标准输出

Iphone 如何捕获system()启动的流程的标准输出,iphone,ios,io-redirection,Iphone,Ios,Io Redirection,如何将系统(“openssl enc-aes-128-cbc-k secret-p-md sha1>文件名”)的输出保存到文件中 我尝试了以下方法: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fileNam

如何将
系统(“openssl enc-aes-128-cbc-k secret-p-md sha1>文件名”)的输出保存到文件中

我尝试了以下方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",

                      documentsDirectory];

NSString *str=[NSString stringWithFormat:@"openssl enc -aes-128-cbc -k secret -P -md sha1 > %s",[fileName UTF8String]];
NSLog(@"AES Key is %@",str);
system([str UTF8String]);

NSString *strAES = [NSString stringWithContentsOfFile:fileName encoding:nil error:nil];
NSLog(@"strAES Key is %@",strAES);

NSString *strAESKey=[[[[strAES componentsSeparatedByString:@"\n"] objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
NSLog(@"strAESKey Key is %@",strAESKey);

// NSString *content = @"One\nTwo\nThree\nFour\nFive";
  [str writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];`
我哪里做错了


system()
函数将输出发送到控制台,但由于iPhone没有控制台,我需要将输出重定向到文本文件,并从文件中获取密钥以用于加密/解密。

首先:请注意,我不认为您可以在没有崩溃的iOS上使用
system(3)
。但我不确定,因此我将解释如何做:

这有点棘手

要实现这一点,您必须知道每个进程都是用三个文件描述符打开的:一个用于读取(
stdin
用于标准输入),两个用于写入(
stdout
用于标准输出,
stderr
用于标准错误输出)。它们的fd编号为0(
stdin
),1(
stdout
)和2(
stderr

要将标准输出或标准错误从程序重定向到文件,必须执行以下操作:

NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",

                      documentsDirectory];

NSString *str=[NSString stringWithFormat:@"openssl enc -aes-128-cbc -k secret -P -md sha1 > %s",[fileName UTF8String]];
NSLog(@"AES Key is %@",str);
system([str UTF8String]);

NSString *strAES = [NSString stringWithContentsOfFile:fileName encoding:nil error:nil];
NSLog(@"strAES Key is %@",strAES);

NSString *strAESKey=[[[[strAES componentsSeparatedByString:@"\n"] objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
NSLog(@"strAESKey Key is %@",strAESKey);

// NSString *content = @"One\nTwo\nThree\nFour\nFive";
  [str writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];`
  • 首先,
    fork(2)
    。在子项目的父进程
    waitpid(2)
  • 在子进程中:
    • 重新打开(
      freopen(3)
      stdout
      stderr
      到要重定向输出的文件
    • 使用
      execve(2)
      执行要调用的程序
    • 当命令终止时,父级将获得一个SIGCHLD,并且
      waitpid(2)
      应返回
括号中的数字描述了该函数手册页(
man 2 waitpid
)中的章节

希望这有帮助,如果您有更多问题,请提问:-)

更新:由于OP只是想获取输出,而不是具体地保存到文件中,因此您可以使用
popen(3)


嗨,欢迎来到苏。如果你能提供更多关于你所面临的问题的细节,或者你上面的解决方案在哪些方面不起作用,那么你很可能会得到更有建设性的帮助/1。在越狱iOS上,有一个shell,因此您可以使用系统功能。2.为什么不干脆把它打开?这样你就不必处理那些单调乏味的函数调用了。谢谢,你是对的,我编辑我的答案是为了更具体。2.如果您只想读取输出,那么使用
popen()
将是正确的选择。如果要将输出重定向到文件,应该重新打开stdout。当我执行下面的
行系统(“openssl enc-aes-128-cbc-k secret-P-md sha1”)它在控制台输出中打印输出:salt=0879D963A2DF15F9 key=26287A322BE333C6A5E44256791AFEDE iv=23715CAFB431BE252BD2AB20D56B9C42如何从控制台获取此数据?你能给我一个使用popen()的例子吗?嗨,这段代码对设备不起作用。但是它在模拟器上运行。。你能帮帮我吗?嗨,这代码对设备不起作用**它不会进入设备**上的while循环,但它在模拟器上运行。。你能帮我吗?