expect

https://www.cnblogs.com/chenjo/p/12892894.html

借助expect处理交互的命令可以将交互过程如ssh登录,ftp登录等写入一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同 操作的环境中,可以大大提高系统管理员的工作效率。

step1:安装expect

yum -y install expect

语法:expect [选项]      注意:最后一条不会执行

选项 -c:从命令行执行expect脚本,默认expect是交互地执行

    示例:expect -c  'expect " " {   send "pressed enter "   }'                //遇到 发送pressed enter         

选项-d:输出调试信息

     示例:expect -d ssh.exp

expect中的相关命令 spawn:启动新的进程,执行一条命令

send:向进程发送字符串

expect:从进程接收字符串

exp_continue: 匹配多个字符串时在执行动作后加此命令 

 

interact:允许用户交互

单一分支模式的语法:

   expect "hi" { send "You said hi " }          //匹配到 hi 后,会输出"you said hi",并换行

多分支模式的语法:

expect "hi" { send "You said hi " } 

           "hehe" { send “Hehe yourself " } 

           "bye*" { send "Goodbye " }             //匹配 hi, hehe, bye 中的任意字符串时, 发送相应字符串。

等同于: expect { "hi" { send "You said hi " } "hehe" { send "Hehe yourself " } "bye" { send "Goodbye " } }

原文地址:https://www.cnblogs.com/krystal-LA-zx/p/14256101.html