execList method
void
execList({})
execList runs multiple commands in a single request to the host.
The commands are chained into a single command line with the bash command delimiter ';' between each command.
If you set sudo to true then each command is run under sudo.
If you set password then the password is passed to the sudo command.
If you don't pass progress, the default Progress uses encoding.
If you pass progress, encoding is ignored.
Remote.exec(
host: fqdn,
command: ['mkdir -p /tmp/dir/fred', 'cp /home/bsutton/*.dart /tmp/dir/fred', 'ls /tmp/dir/fred/*.dart'],
sudo: true,
password: password,
progress: Progress.print());
Throws RunException. @Throwing(RunException)
Implementation
void execList({
required String host,
required List<String?> commands,
bool agent = true,
bool sudo = false,
String? password,
Progress? progress,
Encoding encoding = utf8,
}) {
final cmdArgs = <String>[];
// enable agent forwarding only
// if the user doesn't pass a password.
if (agent && password == null) {
cmdArgs.add('-A');
}
// disable psuedo terminal
cmdArgs
..add('-T')
..add('--')
..add(host);
final cmdLine = StringBuffer();
for (var command in commands) {
if (sudo && password == null) {
command = 'sudo $command';
}
if (cmdLine.isNotEmpty) {
// bash command delimiter.
cmdLine.write(';');
}
cmdLine.write(command);
}
cmdArgs.add('"$cmdLine"');
progress ??= Progress.devNull(encoding: encoding);
try {
if (sudo && password != null) {
_runWithPassword(
host: host,
// One sudo process authenticates once. The child closes the SSH
// input before any caller-supplied command can read it, including
// when sudo does not request a password (NOPASSWD policy).
command: buildPasswordedCommand(cmdLine.toString()),
password: password,
progress: progress,
);
} else {
startFromArgs('ssh', cmdArgs, progress: progress);
}
} on RunException catch (e) {
final error = _sshErrors[e.exitCode!];
throw RunException(
e.cmdLine,
e.exitCode,
red('ssh exit code: ${e.exitCode} - $error'),
stackTrace: e.stackTrace,
);
}
}