다른 스크립트를 실행하는 동안 사용자 상호 작용을 시뮬레이션하는 방법

다른 스크립트를 실행하는 동안 사용자 상호 작용을 시뮬레이션하는 방법

여러 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에 더 익숙하기 때문에 후자를 선호합니다.

다음은 여러 테스트 서버에 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

관련 정보