1
1

More than 1 year has passed since last update.

[macos][swift]管理者でプロセス実行

Last updated at Posted at 2021-10-31
import Foundation

let sudo = Process()

// ls を実行してみる
// -S: パスワードを端末ではなく標準入力から読み込む
sudo.launchPath = "/usr/bin/sudo"
sudo.arguments = ["-S", "/bin/ls"]

// パイプを設定
let stdin = Pipe()
let stdout = Pipe()
sudo.standardOutput = stdout
sudo.standardError = stdout
sudo.standardInput = stdin

// 実行
sudo.launch()

// パスワードの末尾には改行が必要
let password = "password"
let passwordWithNewline = password + "\n"

// パスワードを標準入力へ書き込み
stdin.fileHandleForWriting.write(passwordWithNewline.data(using: .utf8)!)
try? stdin.fileHandleForWriting.close()

// 終了まで待機
sudo.waitUntilExit()

// 標準出力を取得
if let data = try? stdout.fileHandleForReading.readToEnd(),
   let output = String(data: data, encoding: .utf8) {
    print(output)
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1