![다른 스크립트를 실행하는 동안 사용자 상호 작용을 시뮬레이션하는 방법](https://linux55.com/image/38710/%EB%8B%A4%EB%A5%B8%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EC%8B%A4%ED%96%89%ED%95%98%EB%8A%94%20%EB%8F%99%EC%95%88%20%EC%82%AC%EC%9A%A9%EC%9E%90%20%EC%83%81%ED%98%B8%20%EC%9E%91%EC%9A%A9%EC%9D%84%20%EC%8B%9C%EB%AE%AC%EB%A0%88%EC%9D%B4%EC%85%98%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
여러 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