====== expect ====== * [[http://linux.die.net/man/1/expect|expect(1) - Linux man page]] * shell script를 자동화 해주는 툴이다. * [[https://github.com/pexpect/pexpect|Python pexpect]]로도 유사한 작업이 가능하다. * [[http://www.thegeekstuff.com/2010/10/expect-examples/|6 Expect Script Examples to Expect the Unexpected (With Hello World)]] * [[http://ktdsoss.tistory.com/149|ktds OpenSource Group :: [UNIX/LINUX] 자동화를 위한 interactive 스크립트 - expect (1) 기본 사용법]] * [[http://ktdsoss.tistory.com/151|ktds OpenSource Group :: [UNIX/LINUX] 자동화를 위한 interactive 스크립트 - expect (2) 예제]] * [[http://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/|Expect Script Tutorial: Expressions, If Conditions, For Loop, and While Loop Examples]] * ''sshpass''와 함께 혹은 대신 사용하여 로그인 자동화가 가능하다. * ''sshpass -f 파일경로'' 사용시 파일 경로에 ''~''로 사용자 디렉토리 지칭하는 것이 작동하지 않았다. ===== 기본 명령 ===== * ''spawn 명령어'' : 명령어를 실행한다. * ''expect "특정문자열"'' : "if 특정 문자열이 나타나면" 정도의 의미 * ''send "보낼문자열\r"'' : 문자열을 전송한다. ''\r''은 엔터키를 의미한다. * ''interact'' : 이제는 expect를 끝내고 사용자에게 제어권을 넘긴다. * ''set 변수 [lindex $argv 0]'' : 0번째 명령행 인자를 변수에 지정한다. * ''send "$변수"'' : 변수에 저장된 문자열을 전송한다. * ''expect eof'' : expect 종료 ===== expect 구문 옵션 ===== * ''-nocase'' : 대소문자 구별안함 ===== ssh login example ===== #!/usr/bin/expect set timeout 20 set ip [lindex $argv 0] set user [lindex $argv 1] set password [lindex $argv 2] spawn ssh "$user\@$ip" expect "Password:" send "$password\r"; interact ===== sh shell script 안에서 호출 ===== #!/bin/bash # 원하는 bash script 작업 SOME_ENV_VAR1="값" SOME_ENV_VAR2="값2" expect < * ''%%<<%%EOF'' 방식을 사용하면 STDIN이 이걸로 소진돼 버려서 ''interact''가 작동하지 않는다. 이 경우에는 ''-f expect명령모음파일명'' 옵션으로 외부 파일로 expect 스크립트를 빼두면 된다. * ''expect -f 파일명 옵션1 옵션2'' 등으로 실행하고 ''set 변수명 [lindex $argv 0]'' 혹은 숫자 1 등으로 바꿔가면서 옵션을 전달할 수 있다.