2017-11-03 13 views
0

ssh가 5 개의 상자에 bash 스크립트를 실행하고, aptitude dist-upgrade -d를 실행하고, 패키지를 압축 한 다음 원본 상자로 다시 스크랩합니다.긴 스크립트없이 반복적 인 경우를 기대합니까?

모든 상자에는 동일한 암호가 있으며 모든 상자에는 dist 업그레이드에 예라고 말하기 위해 "y"입력이 필요합니다.

나의 현재, 스크립트는 다음

## Original box 
expect "password" 
send "Password\n" 
## Box sshing to 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 

내 질문은의 반복 하중 길고 지저분 기대 어떻게해야합니까 "중복 ​​제거"이 암호가 암호를 전송 요구되는 경우가 있으므로, 계속 프롬프트가 표시되면 y를 보냅니다.

#!/usr/bin/expect -f 
set timeout -1 
## This uses manualpatching.sh, used to zip up a list of the latest Debian packages 
spawn ./manualpatching.sh 
## BOX1 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
## BOX2 
expect "password" 
send "Password\n" 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
expect "password" 
send "Password\n" 
expect "connecting" 
send "yes\n" 
## BOX3 
expect "password" 
send "Password\n" 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
expect "password" 
send "Password\n" 
## BOX4 
expect "password" 
send "Password\n" 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
expect "password" 
send "Password\n" 
## BOX5 
expect "password" 
send "Password\n" 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
expect "password" 
send "Password\n" 
## BOX6 
expect "password" 
send "Password\n" 
expect "password" 
send "Password\n" 
expect "want to continue?" 
send "y\n" 
expect "password" 
send "Password\n" 
## END 
expect eof 
+0

스크립트를 보여주세요! – Gaurav

+0

자, 지금 게시를 수정합니다 –

+0

거기에, 당신이 생각하는 것을 알려주세요. –

답변

3
:

아래

전체 스크립트 (내가 "예"보내야 할 경우 때때로 키 검사가 혼합으로 발생합니다) 나는 때 올 것이다 것의 완벽한 순서를 얻을 필요가 없도록

올바른 방향으로 나를 가리켜 주신 Gaurav에게 감사드립니다.

내가 바로 exp_continue를 사용하여 아래로 내 스크립트를 깎았

, 그것은 훨씬 더 깨끗하고 다목적 :

#!/usr/bin/expect -f 
set timeout -1 
spawn ./manualpatching.sh 
expect { 
     "Are you sure you want to continue connecting (yes/no)" { 
      send "yes\r" 
      exp_continue 
     } 
     "password" { 
      send "Password\r" 
      exp_continue 
     } 
     "want to continue?" { 
      send "y\r" 
      exp_continue 
     } 
} 
+0

"계속하고 싶다"액션 블록 다음에'eof'를 추가합니다. –

+0

내 무지 때문에 죄송합니다. 그게 어떻게됩니까? –

+1

지금은 expect 루프에서 벗어날 패턴이 없습니다. 모든 브랜치는'exp_continue'를 사용합니다. 'eof'를 기대하는 것은 생성 된 프로그램이 끝나기를 기대하는 (그리고이 스크립트를 유지할 다른 누군가) 당신을위한 명시적인 알림이며 스크립트는 계속 이어질 수 있습니다. –