tekartik / process_run.dart

Process run helper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to run sudo?

goldcoders opened this issue · comments

is it possible to run sudo command using this package?
i have an app that need to run sudo command.
Is there any documentation how we go about it for development and production?

You can run your dart program using sudo to run all you child scripts as a super user.

Running a shell script with sudo from inside a dart script ran in non super user mode
is a little bit trickier as it requires user interaction. One solution (tried on Ubuntu) is to use
sudo --stdin to specify reading the password from the stdin.

You can then run script using the following:

await shell.run('sudo --stdin lsof -i:22');

A shared stdin object could be used to redirect dart program input to shell objects.

Here is a more complex example:

import 'package:process_run/shell.dart';

/// Only works on linux, list the process listening on port 22
void main(List<String> arguments) async {
  /// We have to use a shared stdin if we want to reuse it.
  var stdin = sharedStdIn;

  /// Use sudo --stdin to read the password from stdin
  /// Use an alias for simplicity (only need to refer to sudo instead of sudo --stdin)
  var env = ShellEnvironment()..aliases['sudo'] = 'sudo --stdin';
  var shell = Shell(
      stdin: sharedStdIn,
      // lsof return exitCode 1 if not found
      environment: env,
      throwOnError: false);

  await shell.run('sudo lsof -i:22');
  // second time should not ask for password
  await shell.run('sudo lsof -i:80');

  /// Stop shared stdin
  await stdin.terminate();
}

im using this one on flutter app, so passing in stdin isnt an option for me , user input would be coming from a form submittion.
on mac i can use osascript to invoke a gui prompt for sudo command.
on windows i havent tried but i think invoding runas on cmd will prompt for admin password gui
linux is a different thing, which might need extra gui apps depending on distro

@alextekartik
I did not understand how do I have to give Linux sudo password to flutter app.
After running await shell.run('sudo --stdin lsof -i:22'); and typing sudo password, nothing happened.
Can you explain please?

From flutter app, one solution is to use a custom stdin for you command. Something like:

import 'dart:io';

import 'package:http/http.dart';
import 'package:process_run/shell.dart';

void main(List<String> arguments) async {
  // Assuming you have the password in `pwd` variable
  var pwd = '__your_root_password__';

  // Use sudo --stdin to read the password from stdin
  // Use an alias for simplicity (only need to refer to sudo instead of sudo --stdin)
  // Also set an empty prompt here for demo purpose.
  var env = ShellEnvironment()..aliases['sudo'] = "sudo --stdin --prompt=''";

  // Create a fake stdin stream from the password variable
  var stdin =
      ByteStream.fromBytes(systemEncoding.encode(pwd)).asBroadcastStream();

  // Execute!
  var shell = Shell(stdin: stdin, environment: env);

  // Should not ask for password
  //await shell.run('sudo lsof -i:22');
  await shell.run('sudo whoami');
}