2012. 12. 23. 21:03

expect tip


# expect script에 대한 설명


# 명령어 list

0. reference
- Exploring Expect by Don Libes (O'REILLY)
1. 개요
-

2. 간단 설명
- #!/usr/local/bin/expect --
-- may be used to delimit the end of the options. This is useful if you want to
pass an option-like argument to your script without it being interpreted by Expect.
- #!/usr/local/bin/expect -f
Expect reads cmdfile for a list of commands to execute.
- 명령어 list
: spawn, expect, send, set, send_user
- eof/close의 의미는
: 맨 끝줄에 expect eof를 넣어줘라
: 안 넣어주면 맨 마지막 줄이 수행되지 않고 끝나더라
: close를 넣어줘도 마찬가지네
- timeout
: set timeout 60
telnet 할때 Login 프롬프트가 늦게 뜨면 expect가 멈추는데 이때 사용하자.
- sleep
- 조건에 따른 행동은 아래처럼(예제2)
: Prompt가 m402이면 time을 실행하고 아니면 date를 실행하라
expect {
"m402*" { send "time\r" }
"m40*" { send "date\r" }
}
[ Three command ]
1. general
a. execute the expect file
- expect filename
- To insert the line "#! /user/bin/expect --" and say "chmod +x filename; rehash".
1. send
1. expect
1. spawn

[ Example ]
1. example
a. example
while 1 {
expect {
eof {break}
"UNDER*CLEAR\\?" {send "date\n"}
"OVER INODE*FIX\\?" {send "time\n"}
"\\? " {interact +}
}
}
1. regular expression
a. \\ : The \\ prevents the next characeter from being interpreted as a wild card.
a. * : * is a wild card
1. loop
a. while
1. interact +
a. When done, you can exit or return control to the script, here triggered by pressing the plus kye.
1. arbitrary
a. example
interact {
"y2" {send "set def qwk/term=vt100"}
"~~d" {send [exec date]}
}

1. 예제
- host에 ssh로 접속해서 password넣고 명령어 하나 실행하고 exit하는...
#!/usr/bin/expect
set pass=admin
spawn ssh admin@sc1

expect "admin@sc1's password:" { send "$pass\n" }
expect "MBOS>" { send "show subscriber-management subscriber-station | include Idle\n" }

expect "MBOS>" { send "exit\n" }
2. 예제2
#!/usr/bin/expect -f
spawn telnet m404
sleep 1
expect "*login*" { send "hmi\r" }
expect "assword*" { send "hmi hmi\r" }
expect {
"m402*" { send "time\r" }
"m40*" { send "date\r" }
}
expect "m402*" { send "pwd\r" }
expect "m402*" { send "date\r" }
expect "m402*" { send "exit\r" }

[ 풀리지 않는 의문, 숙제, 해결해야 할 것들 ]
1. ssh user@host처럼 했을때 접속은 되고 prompt가 떨어지지 않으면 어떻게 해야나?