
여러 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