iOS通过代码执行系统指令后如何获取执行后结果

需求: 在iOS代码中使用system指令,如何获取返回结果,比如 system("ls")

比如执行完 ls 指令

iPhone813:/ root# ls
Applications  Developer  Library  System  User	bin  boot  cores  dev  etc  lib  mnt  ppuntether  private  sbin  tmp  usr  var

怎么通过代码获取到Applications Developer Library System User bin boot cores dev etc lib mnt ppuntether private sbin tmp usr var这些内容

下面这这段是我在网上找的执行系统指令的代码,但是全部都没有拿到返回值

  • 互联网
- (NSString *)cmd:(NSString *)cmd
{
    // 初始化并设置shell路径
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/bash"];
    // -c 用来执行string-commands(命令字符串),也就说不管后面的字符串里是什么都会被当做shellcode来执行
    NSArray *arguments = [NSArray arrayWithObjects: @"-c", cmd, nil];
    [task setArguments: arguments];
    
    // 新建输出管道作为Task的输出
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    
    // 开始task
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    
    // 获取运行结果
    NSData *data = [file readDataToEndOfFile];
    NSLog(@"%@", data);
    NSLog(@"[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; %@", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
    return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}
  • 狗神的帖子
#import <spawn.h>

extern char **environment;

void run_cmd(const char *cmd)
{
	pid_t pid;
	const char *argv[] = {"sh", "-c", cmd, NULL};
	int status;

	status = posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)argv, environment);
	if (status == 0) {
		if (waitpid(pid, &status, 0) == -1) {
			perror("waitpid");
		}
	}
}
void your_function(void)
{
	run_cmd("reboot");
}
  • unix
system()

可以参考下这个回答

感谢.链接里面看到了另外一个方法POPEN

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void print_result(FILE *fp)
{
    char buf[100];
    
    if(!fp) {
        return;
    }
    printf("\n>>>\n");
    while(memset(buf, 0, sizeof(buf)), fgets(buf, sizeof(buf) - 1, fp) != 0 ) {
        printf("%s", buf);
    }
    printf("\n<<<\n");
}




int main(int argc, char * argv[]) {
    @autoreleasepool {
        FILE *fp = NULL;
        
        fp = NULL;
        fp = popen("ls /Applications", "r");
        if(!fp) {
            perror("popen");
            exit(EXIT_FAILURE);
        }
        print_result(fp);
        pclose(fp);
        sleep(1);
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
1 个赞

错误信息打印不出来

  • 比如dpkg -r com.lll.yuong

控制台可以打印,但是2楼的方法从代码中无法获取

dpkg: warning: ignoring request to remove com.lll.yuong   which isn't installed

FILE *popen(const char *command, const char *type);

当使用type 参数为 “r" 时,popen 会把执行 command 后的标准输出重定向到管道流。但是,command执行中的标准错误输出,在管道流中得不到。

那么,有没有办法来同时获取到 command 执行后的标准输出和标准错误输出呢?答案是肯定的!

只要在 command 中,将标准错误输出重定向到标准输出即可!

例如:

FILE *stream;

stream = popen("'your task code' 2>&1", "r");

while(fgets(s, 1024, stream))

{

printf(s);

}

引自原文

1 个赞

狗神代码里面的status不就是返回结果吗?
+(int)MJrun_cmd:(const char *)cmd
{
pid_t pid;
const char *argv[] = {“sh”, “-c”, cmd, NULL};
int status;

status = posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)argv, environ);
if (status == 0) {
    if (waitpid(pid, &status, 0) == -1) {
        perror("waitpid");
    }
}
return status;

}
返回值256 就是成功

1 个赞

请详细阅读…问题