import std.stdio;
import std.process;
import std.format;
import std.algorithm;
// Copied the interface from executeShell
auto executeShellSudo(scope const(char)[] command,
const string[string] env = null,
Config config = Config.none,
size_t maxOutput = size_t.max,
scope const(char)[] workDir = null,
string shellPath = nativeShell) {
// First assume the user is super user:
auto suCommand = format!"sudo --non-interactive %s"(command);
auto execute() {
return executeShell(suCommand, env, config, maxOutput, workDir, shellPath);
}
auto result = execute();
if ((result.status == 1) &&
(result.output == "sudo: a password is required\n")) {
// Have sudo ask for password. (Alternatively, sudo can be invoked
// with the --askpass switch.)
suCommand = format!"sudo %s"(command);
result = execute();
}
return result;
}
void main() {
auto result = executeShellSudo("cat /dev/null");
writeln(result);
}
Ali
windows
有执行壳
函数在这里
找ShellExecuteA
函数.
同样,这里可以找windows的api
.
这样:
alias runas = compose!(x => to!bool((cast(int) x) > 32), x => ShellExecute(null, "runas", "cmd", cast(wchar*)`/c "cd /d %s && %s"`.format(getcwd(), x).to!wstring, null, SW_HIDE);