"리눅스 expect"의 두 판 사이의 차이

17번째 줄: 17번째 줄:
| spawn || 명령을 시작
| spawn || 명령을 시작
|}
|}
==예시:Hello World==
* 사용자가 hello를 입력하면 world가 출력됨
<source lang="bash">
#!/usr/bin/expect
expect "hello"
send "world"
</source>
:→ expect 명령으로 hello가 입력 되기를 기다리며 hello를 입력하면 world를 출력해줌


==예시: FTP 작업 자동화==
==예시: FTP 작업 자동화==

2016년 12월 31일 (토) 01:01 판

1 개요

리눅스 expect
  • CLI 상호작용 자동화 프로그램
  • Tcl 스크립트 언어의 확장판
  • 라이선스: 퍼블릭 도메인
  • SSH, SFTP, SSH 작업 등 자동화 가능
  • Expect 스크립트는 일반적으로 .exp 확장자를 가짐

2 주요 명령어

명령어 설명
send 문자열을 프로세스에 보냄
expect 프로세스로 부터 특정 문자열을 기다림
spawn 명령을 시작

3 예시:Hello World

  • 사용자가 hello를 입력하면 world가 출력됨
#!/usr/bin/expect
expect "hello"
send "world"
→ expect 명령으로 hello가 입력 되기를 기다리며 hello를 입력하면 world를 출력해줌

4 예시: FTP 작업 자동화

set timeout -1
spawn ftp $remote_server
expect "username:"
send "$my_user_id\r"
expect "password:"
send "$my_password\r"
expect "ftp>"
send "bin\r"
expect "ftp>"
send "prompt\r"
expect "ftp>"
send "mget *\r"
expect "ftp>"
send "bye\r"
expect eof

5 예시: SFTP 작업 자동화

#!/usr/bin/env expect -f
proc connect {passw} {
  expect {
    "Password:" {
      send "$passw\r"
        expect {
          "sftp*" {
            return 0
          }
        }
    }
  }
  return 1
}
set user [lindex $argv 0]
set passw [lindex $argv 1]
set host [lindex $argv 2]
set location [lindex $argv 3]
set file1 [lindex $argv 4]
set file2 [lindex $argv 5]
if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" || $file2 == "" }  {
  puts "Usage: <user> <passw> <host> <location> <file1 to send> <file2 to send>\n"
  exit 1
}

spawn sftp $user@$host
set rez [connect $passw]
if { $rez == 0 } {
  send "cd $location\r"
  set timeout -1
  send "put $file2\r"
  send "put $file1\r"
  send "ls -l\r"
  send "quit\r"
  expect eof
  exit 0
}
puts "\nError connecting to server: $host, user: $user and password: $passw!\n"
exit 1

6 같이 보기

7 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}