ทดสอบกับ ubuntu 8.04
ต้นฉบับจาก http://expect.nist.gov
ติดตั้งด้วยคำสั่ง sudo apt-get install expect
ตัวอย่างการเขียน expect ใช้เพื่อ telnet ไปหา imap mail server
เพื่อตรวจสอบ username / password แล้วกลับออกมา เขียนประมาณว่า
set SERVER "mail.your.domain"
set USER "yourname"
set PASSWORD "yourpassword"
set PROMPT " OK"
spawn telnet $SERVER 143
expect "* OK"
send ". login $USER $PASSWORD\r"
expect ". OK"
send ". logout\r"
ตัวอย่างการเขียน expect ใช้เพื่อ telnet ไปหา pop3 mail server
เพื่อตรวจสอบ username / password แล้วกลับออกมา เขียนประมาณว่า
set SERVER "mail.your.domain"
set USER "yourname"
set PASSWORD "yourpassword"
set PROMPT "+OK"
spawn telnet $SERVER 110
expect "$PROMPT"
send "user $USER\r"
expect "$PROMPT"
send "pass $PASSWORD\r"
expect "$PROMPT"
send "quit\r"
ตัวอย่างการเขียน expect ใช้เพื่อ telnet ไปหา pop.gmail.com server
เพื่อตรวจสอบ username / password แล้วกลับออกมา เขียนประมาณว่า
set SERVER "pop.gmail.com:995"
set USER "yourname@gmail.com"
set PASSWORD "yourpassword"
set PROMPT "+OK"
spawn openssl s_client -connect $SERVER
expect "$PROMPT"
send "user $USER\r"
expect "$PROMPT"
send "pass $PASSWORD\r"
expect "$PROMPT"
send "quit\r"
ตัวอย่างการเขียน expect ใช้เพื่อ telnet ไปหา pop3s live.com server
เพื่อตรวจสอบ username / password แล้วกลับออกมา เขียนประมาณว่า
set SERVER "pop3.live.com:995"
set USER "yourname@live.com"
set PASSWORD "yourpassword"
set PROMPT "+OK"
spawn openssl s_client -connect $SERVER -crlf
expect "$PROMPT"
send "user $USER\r"
expect "$PROMPT"
send "pass $PASSWORD\r"
expect "$PROMPT"
send "quit\r"
ตัวอย่างการเขียน expect ใช้เพื่อ telnet เข้าไปยัง switch router เพื่ออ่านค่าจากคำสั่ง arp แล้วกลับออกมา เขียนประมาณว่า
set ROUTER "10.0.0.1"
set USER "yourname"
set PASSWORD "yourpassword"
set PROMPT "<switch>"
spawn telnet $ROUTER
match_max 100000
expect "Username:"
send "$USER\r"
expect "Password:"
send "$PASSWORD\r"
expect "$PROMPT"
send "display arp\r"
expect {
"More --" { send " " ; exp_continue }
"$PROMPT" { send "exit\r" }
}
ตัวอย่างการเขียน expect ใช้เพื่อ ssh เข้าไปยัง linux router เพื่ออ่านค่าจากคำสั่ง arp แล้วกลับออกมา เขียนประมาณว่า
set ROUTER "10.0.0.1"
set USER "yourname"
set PASSWORD "yourpassword"
set PROMPT "\$"
spawn ssh $USER@$ROUTER
match_max 100000
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$PASSWORD\r"}
}
expect "$PROMPT"
send "arp -n\r"
expect "$PROMPT"
send "exit\r"</switch>