C ++で "sudo -S"コマンドを使用して端末にパスワード入力をバイパスする方法は? [コピー]

C ++で

Linux端末で「sudo」を渡さずに、rootとして次のコマンドを実行する必要があります。 。 (「安全」コメントを投稿しないでください...)

「hcitool CCアドレス」

「sudo -S」オプションを使用できます。

cで書かれた追加のsudo / pipeコードがあります。

#include#include#含む

int main() { std::string userPass{"myPassword"}; std::string command{"sudo -S ps -lf 2>/dev/null <<_EOF\n" + userPass + "\n_EOF"}; FILE *pipe = popen(command.c_str(), "r");文字* line = NULL; ssize_t rlen; while( (rlen = getline(&line, &len, パイプライン)) > 0) [--rlen] = '\0'; // 末尾の改行 std::cout << line << '\n' }

pclose(pipe);
free(line);

}

これが私が今まで持っているものです

FILE  *cmd;
int    status;

QFile *pipe;

cmd = popen("/usr/bin/sudo -S id -un 2>/dev/null", "w");

// MODIFY  for c++
QString Password = "q";
QString Command =" sudo -S ps -lf 2>/dev/null <<_EOF\n" + Password + "\n_EOF";

// std::string userPass{"myPassword"};
// std::string command{"sudo -S ps -lf 2>/dev/null <<_EOF\n" + userPass + "\n_EOF"};
pipe->open ???


I could use help finishing converting the c code into C++.

Qtを使用していますが、一般的なC ++コードも問題ありません。

「オプション」「ps」を理解していません。 man ドキュメントに見つかりません。例では、「cmdファイル」がどのように機能するのかわかりません。

ありがとう

ベストアンサー1

おすすめ記事