从objective-c运行sudo命令 [英] Run sudo command from objective-c

查看:47
本文介绍了从objective-c运行sudo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Objective-c 运行 sudo 命令?我可以使用以下方法运行不需要 sudo 的简单命令:

NSTask *task;任务 = [[NSTask alloc] init];[任务设置启动路径:@"/bin/sh"];NSArray *arguments = [NSArray arrayWithObjects:@"-c" ,[NSString stringWithFormat:@"%@", cmd], nil];NSLog(@"运行命令:%@",cmd);[任务 setArguments: 参数];NSPipe *管道;管道 = [NSPipe 管道];[任务集标准输出:管道];NSFileHandle *文件;file = [pipe fileHandleForReading];【任务启动】;NSData *数据;数据 = [文件 readDataToEndOfFile];NSString *输出;output = [[NSString alloc] initWithData: 数据编码:NSUTF8StringEncoding];返回输出;

当我运行 sudo 命令时出现错误:不允许操作.

根据我在网上找到的内容,我需要获得授权.我阅读了 Apple 的授权文档,但我没有指定说明点.

来自 Apple 的示例 EvenBetterAuthorizationSample 不起作用.

应用程序使用 Apple ID 签名.

解决方案

你应该研究授权服务编程指南,但如果您想使用 sudo 执行此操作,则可以.>

sudo 需要一个密码来授权执行,它通常从 终端 获取 - 这与标准相同input 虽然在命令外壳中,默认情况下标准输入也连接到终端.sudo-S 选项指示它从标准输入读取密码.

按照您现有的设计,您可以:

1) 将 -S 添加到 sudo 命令

2) 输入密码的提示和任何错误信息,例如无效密码,都会由 sudo 写入标准错误.创建一个管道并将其附加到任务 standardError,或将现有管道附加到 standardOutputstandardError,以捕获此输出.

3) 创建另一个管道并将其附加到任务standardInput.将密码后跟换行符写入该管道.当任务运行时 sudo 将读取密码并执行您的命令.当然,如果命令本身需要也将从管道读取的输入 - 所以您必须将其写入管道.

HTH

How can I run sudo commands from objective-c? I am able to run simple commands that don't require sudo with the following method:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];

NSArray *arguments = [NSArray arrayWithObjects:@"-c" ,[NSString stringWithFormat:@"%@", cmd], nil];


NSLog(@"run command: %@",cmd);
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;

When I run a sudo command i get the error: Operation not permitted.

From what I found online I need the gain authorisation. I read the Authorisation documentation from Apple but I doesn't specify a stating point.

The example EvenBetterAuthorizationSample from Apple doesn't work.

The application is signed with an Apple ID.

解决方案

You should study the Authorization Services Programming Guide, but if you want to do this using sudo you can.

What sudo requires is a password to authorise the execution, it normally gets this from the terminal - that is not the same as standard input though in a command shell the standard input by default is also connected to the terminal. The -S option of sudo instructs it to read the password from the standard input.

Following your existing design you can:

1) Add -S to the sudo command

2) The prompt for the password and any error messages, such as invalid password, will be written by sudo to the standard error. Create a pipe and attached it to the tasks standardError, or attach your existing pipe to both standardOutput and standardError, to capture this output.

3) Create another pipe and attach it to the tasks standardInput. Write the password followed by a newline to that pipe. When the task runs sudo will read the password and execute your command. Of course if the command itself requires input that will also be read from the pipe - so you must write it to the pipe.

HTH

这篇关于从objective-c运行sudo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆