macos開發(fā),在程序里面執(zhí)行shell腳本

上一篇我寫了在xcode里面添加run script來執(zhí)行腳本,可是打包之后發(fā)現(xiàn),只有用xcode運(yùn)行的時(shí)候才會(huì)執(zhí)行這個(gè)shell命令,查了很多資料,最后我覺得應(yīng)該是我理解錯(cuò)了,這里的run script很可能只是為了方便自動(dòng)化打包之類的場景適用,而并不是真正的運(yùn)行時(shí)執(zhí)行腳本。那該怎么實(shí)現(xiàn)呢?
參閱博客一
參閱博客二
參閱博客三

- (NSString *)cmd:(NSString *)cmd
{
    // 初始化并設(shè)置shell路徑
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/bash"];
    // -c 用來執(zhí)行string-commands(命令字符串),也就說不管后面的字符串里是什么都會(huì)被當(dāng)做shellcode來執(zhí)行
    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];
    
    // 獲取運(yùn)行結(jié)果
    NSData *data = [file readDataToEndOfFile];
    return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}

但是有一個(gè)問題,因?yàn)槟_本是:

#!/bin/bash
rm -rf ~/Downloads/keystore.xml
#chmod -x write.sh
/usr/local/bin/adb pull /data/data/com.whatsapp/shared_prefs/keystore.xml ~/Downloads/keystore.xml
cat ~/Downloads/keystore.xml

adb需要管理員權(quán)限,所以一直提示沒有權(quán)限執(zhí)行腳本。

NSString *script =  [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];

查了一下資料,可以用with administrator privileges,這時(shí)候又報(bào)"用戶名密碼不正確",不知道為啥,最終拋棄了NSTask,找到了這個(gè)庫,它自動(dòng)幫我們處理權(quán)限問題,最終:

- (void)runSTPrivilegedTask {
    NSString *cmd = @"/bin/sh launcher.sh";
    
    STPrivilegedTask *privilegedTask = [[STPrivilegedTask alloc] init];
    
    NSMutableArray *components = [[cmd componentsSeparatedByString:@" "] mutableCopy];
    NSString *launchPath = components[0];
    [components removeObjectAtIndex:0];
    
    [privilegedTask setLaunchPath:launchPath];
    [privilegedTask setArguments:components];
    [privilegedTask setCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
    
    // Set it off
    OSStatus err = [privilegedTask launch];
    if (err != errAuthorizationSuccess) {
        if (err == errAuthorizationCanceled) {
            NSLog(@"User cancelled");
            return;
        }  else {
            NSLog(@"Something went wrong: %d", (int)err);
            // For error codes, see http://www.opensource.apple.com/source/libsecurity_authorization/libsecurity_authorization-36329/lib/Authorization.h
        }
    }
    
    [privilegedTask waitUntilExit];
    
    // Success! Now, read the output file handle for data
    NSFileHandle *readHandle = [privilegedTask outputFileHandle];
    NSData *outputData = [readHandle readDataToEndOfFile]; // Blocking call
    NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
    //[self.outputTextField setString:outputString];
    NSLog(@"輸出%@",outputString);
    
    NSString *exitStr = [NSString stringWithFormat:@"Exit status: %d", privilegedTask.terminationStatus];
    NSString *result = [[outputString componentsSeparatedByString:@"\"client_static_keypair\">"]  [1]  componentsSeparatedByString:@"</string>"][0];
      NSLog(@"輸出臭豆腐的%@",exitStr);

            
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:[NSString stringWithFormat:@"%@==",result] forKey:@"client_static_keypair"];
            //  [dic setObject:[NSString stringWithFormat:@"%@==",parser.nodeDict[@"server_static_public"]] forKey:@"server_static_public"];
            // dic=parser.nodeDict;
             
          //   NSLog(@"-->%d",isYES);
        // }
     
    
}

按理說這樣應(yīng)該就沒啥問題了,可是這是由又報(bào)someting went wrong:6005,又是一頓google,最終發(fā)現(xiàn)

image.png

必須不能用sandbox功能,關(guān)閉sandbox,果然成功:
image.png

這樣每次執(zhí)行shell的時(shí)候都會(huì)要求授權(quán),終于搞定,最終我也因此獲得了客戶的贊譽(yù):
image.png

最終源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容