https://www.y2b.com/watch?v=rwbXOS1jLqg
https://www.y2b.com/watch?v=CIyDUx3-5pY
https://www.y2b.com/watch?v=iYwzexnRxYk
https://www.y2b.com/watch?v=vNI6CRk5ogg
exp_study.exp (可以使用 exp_study.sh)
# ./exp_study.exp 执行
#!/usr/bin/expect
spawn ssh root@192.168.80.131
expect {
"yes/no" { send "yes\r" ;exp_continue} #exp_continue表示如果没有yes/no,就继续往下执行,,,,在Expect脚本中,'exp_continue'是一个特殊的标记,用于告诉Expect继续执行下一个'expect'语句,而不是立即比较输出。它常常用在'case'语句中,当某些情况的处理还没有结束,需要继续等待更多的输出再进行比较。
"password:" { send "123456789\r"};
}
interact #进入交互
exp_study2.exp (可以使用 exp_study.sh)
# ./exp_study2.exp 执行
#!/usr/bin/expect
set ip 192.168.80.131
set user root
set password 123456789
set timeout 5 #设置超时5秒
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" };
}
interact
exp_study3exp (可以使用 exp_study.sh)
# ./exp_study3.exp 192.168.80.131 root 执行
#!/usr/bin/expect
set ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password 123456789
set timeout 5
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" };
}
interact
exp_study4exp (可以使用 exp_study.sh)
# ./exp_study4.exp 192.168.80.131 root 执行
#!/usr/bin/expect
set ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password 123456789
set timeout 5
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" };
}
#interact
expect "#"
send "useradd yangyang\r"
send "pwd\r"
send "exit\r"
expect eof #结束expect
exp_study5exp (可以使用 exp_study.sh)
# ./exp_study5.exp 192.168.80.131 root 执行
#!/usr/bin/expect
set ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password J123456789
set timeout 5
spawn scp -r /etc/hosts $user@$ip:/tmp
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" };
}
expect eof #结束expect