回答編集履歴

2

コマンド修正

2020/11/08 13:03

投稿

hondayuki0606
hondayuki0606

スコア2

test CHANGED
@@ -12,9 +12,13 @@
12
12
 
13
13
  [task setLaunchPath:@"/bin/bash"];
14
14
 
15
+ // 以下のコマンドで管理者権限ユーザ名取得
16
+
15
17
  [task setArguments:[NSArray arrayWithObjects: @"-c", @"dscl . -read /Groups/admin GroupMembership", nil]];
16
18
 
19
+ // 以下のコマンドでユーザ名取得
20
+
17
- //[task setArguments:[NSArray arrayWithObjects: @"-c", @"users", nil]];
21
+ //[task setArguments:[NSArray arrayWithObjects: @"-c", @"id -un", nil]];
18
22
 
19
23
 
20
24
 

1

実装方法修正

2020/11/08 13:02

投稿

hondayuki0606
hondayuki0606

スコア2

test CHANGED
@@ -8,30 +8,32 @@
8
8
 
9
9
 
10
10
 
11
- NSTask *task = [[NSTask alloc] init];
11
+ NSTask *task = [[NSTask alloc] init];
12
12
 
13
- NSPipe *pipe = [[NSPipe alloc] init];
13
+ [task setLaunchPath:@"/bin/bash"];
14
14
 
15
- [task setLaunchPath:@"/bin/ls"];
15
+ [task setArguments:[NSArray arrayWithObjects: @"-c", @"dscl . -read /Groups/admin GroupMembership", nil]];
16
16
 
17
- [task setStandardOutput:pipe];
17
+ //[task setArguments:[NSArray arrayWithObjects: @"-c", @"users", nil]];
18
-
19
- [task launch];
20
18
 
21
19
 
22
20
 
23
- NSFileHandle *handle = [pipe fileHandleForReading];
21
+ NSPipe *pipe = [[NSPipe alloc] init];
24
22
 
25
- NSData *data = [handle readDataToEndOfFile];
23
+ [task setStandardOutput:pipe];
26
24
 
25
+ [task launch];
26
+
27
+ NSFileHandle *handle = [pipe fileHandleForReading];
28
+
29
+ NSData *data = [handle readDataToEndOfFile];
30
+
27
- NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
31
+ NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
32
+
33
+ NSLog(string);
28
34
 
29
35
 
30
36
 
31
- NSLog(string);
37
+ 出力結果
32
38
 
33
-
34
-
35
- [task release];
36
-
37
- [pipe release];
39
+ GroupMembership: root testuser