別のスクリプトを実行するときにユーザー操作をシミュレートする方法

別のスクリプトを実行するときにユーザー操作をシミュレートする方法

一連の Linux コマンドを実行したいときは問題ありませんが、別のスクリプトが呼び出され、実行中に入力を要求された場合、ユーザーに入力を提供するにはどうすればよいですか。同じサーバー設定を教室に頻繁にインストールするため、完全に自動化したいと考えています。

#!/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

答え1

TCL ExpectまたはPerl::期待値両方試した後、私は Perl に慣れているので後者の方が好きです。

これは、いくつかのテスト サーバーに SSH 接続するために使用するスクリプトの一部です (機密性の高い実稼働サーバーには推奨されません)。

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
  );
}

完全なソースはここで見ることができます:https://github.com/DavidGamba/bin/blob/master/cssh

関連情報