Cómo simular la interacción del usuario al ejecutar otro script

Cómo simular la interacción del usuario al ejecutar otro script

Cuando quiero ejecutar un montón de comandos de Linux, lo hago bien, pero ¿cómo doy información al usuario cuando se llama a otro script y solicita información mientras se ejecuta? Instalo la misma configuración de servidor en las aulas con frecuencia, por lo que quiero automatizarla por completo.

#!/bin/bash
sudo apt-get install python -y
sudo apt-get install python-m2crypto -y
sudo apt-get install git-core -y
git clone https://github.com/learningequality/ka-lite.git
cd ka-lite/
./setup_linux.sh

#the script works until here
#now, while the setup script is running, the following needs to happen
#I need to press enter twice
#enter the password twice
#press enter twice
#press y and then enter

#here are some things I tried, none of them worked
send "yes\n"
send "yes\n"
#echo | <Press [enter] to continue...> #enter
#echo | <return> #enter
Password8 #password
Password8 #password
echo | <yourfinecommandhere> #enter
echo | <return> #enter
y

Respuesta1

Puede utilizar TCL Expect oPerl::Esperar. Después de probar ambos, prefiero el último porque estoy más familiarizado con Perl.

Este es un fragmento de un script que utilizo para enviar ssh a varios servidores de prueba (no recomendado para servidores de producción sensibles):

if( defined $password ) {
  $exp->expect(
    $timeout,
    [   qr/no\)\?\s+$/i => sub {
        my $self = shift;
        $self->send("yes\n");
        exp_continue;
      }
    ],
    [   qr/password:\s+$/i => sub {
        my $self = shift;
        $self->send("$password\n");
        exp_continue;
      }
    ],
    '-re',
    qr/~/,    #' wait for shell prompt, then exit expect
  );
}

Puedes consultar la fuente completa aquí:https://github.com/DavidGamba/bin/blob/master/cssh

información relacionada