Liunx expect 基础
#################################################
a script for study except
#################################################
#!/usr/bin/expect
声明文件内的语法使用 expect 的语法来执行。
send
send: 向进程发送字符串,用于模拟用户的输入。注意要加 \n 回车
expect
expect: 从 shell 进程接收字符串, " " 表示提示框里面的内容
expect {},多行期望,匹配到哪条执行哪条
spawn
spawn: 启动进程(由spawn启动的进程的输出可以被expect所捕获)。
spawn ssh $user@10.10.10.10
spawn启动一个进程,进程执行ssh命令,程序后面可以通过expect/send和新起的进程进行交互。
set 变量赋值
set timeout 60: 设置相应的时间,如果脚本执行或者网络问题超过了这个时间将不执行
set user "arlen"
set 嵌套命令
set user [lindex $argv 0]
set password [lindex $argv 1]
把命令行第一个参数赋给 user,第二个参数赋给 password 。
puts 输入输出
puts stderr "Usage: $argv0 login passwaord.n "
puts "hello world"
puts stdout "1234"
命令行参数
$argc,$argv 0,$argv 1 ... $argv n
argc表示命令行参数个数,后面分别表示各个参数项,0表示第一个参数,1表示第二个参数,以此类推,可以通过lindex获取对应参数值(lindex $argv 0)。
llength argv 表示参数的个数, argv0 表示脚本的名称
if
if 判断需要用 {} 括起来, 并且与 {} 之间需要有空格。
else / elseif 不能单独放一行,所以 else / elseif 要跟在 } 后面。
两个花括号之间必须有空格隔开,比如if {} {}。
使用{来衔接下一行,所以if的条件后需要加左花括号{ 。
grep
grep 到指定字符 $? 返回 0, grep 不到指定字符 $? 返回 1 。
函数定义和调用
proc do_console_login {login pass} {
}
do_console_login $user $password
循环
while ($done) {
}
条件分支 switch
switch -- $var {
0 {
}
1 {
}
2 {
}
}
示例:
#!/usr/bin/expect
set timeout 60
set remote_host [lindex $argv 0]
set type [lindex $argv 1]
set target "output-0"
set invalid "output-1"
spawn ssh -o "no" $remote_host
set chan [open ansible.log a]
expect {
"$ " {
send "sudo systemctl status ***.service | grep 'active (running)' >&null; echo output-\$?\n"
}
timeout {
puts "could not connect to $remote_host!"
exit 1
}
}
expect {
"*output-0*" {
puts $chan "***.service is ok"
}
"*output-1*" {
puts "***.service is inactive!!!"
send "exit"
exit 1
}
timeout {
puts "***.service service status is wrong(Timeout)!"
send "exit"
exit 1
}
}
if { "$type" == "***" } {
send "sudo systemctl status ***.service | grep 'active (running)' >&null; echo output-\$?\n"
expect {
"*output-0*" {
puts $chan "$type ***.service is ok !\n"
}
"output-1" {
puts "$type ***.service is wrong !\n"
exit 1
}
timeout {
puts "$type status is wrong(Timeout) !\n"
exit 1
}
}
} elseif { "$type" == "***" } {
send "sudo systemctl status ***.service | grep 'active (running)' >&null; echo output-\$?\n"
expect {
"*output-0*" {
puts $chan "$type ***.service is ok !\n"
}
"output-1" {
puts "$type ***.service is wrong !\n"
exit 1
}
timeout {
puts "$type status is wrong(Timeout) !\n"
exit 1
}
}
} else {
puts "it is not in these types"
exit 1
}
puts $chan "$type check service is done!!!"
exit 0
参考文章:
linux下expect使用教程: http://www.cnblogs.com/arlenhou/p/learn_expect.html
Liunx expect 基础的更多相关文章
- expect基础及实例
expect基础及实例 http://blog.csdn.net/zhuying_linux/article/details/6900805
- Liunx之基础学习
用户提权命令之-sudo sudo命令用来以其他身份来执行命令,预设的身份为root.在/etc/sudoers中设置了可执行sudo指令的用户.若其未经授权的用户企图使用sudo,则会发出警告的邮件 ...
- Liunx Shell入门
本人也是初学习Liunx,如有错误请指出.Liunx版本:Ubuntu 14.04 一.Liunx命令基础 在Ubuntu下打开终端快捷键为:ctrl+Alt+T Liunx命令的基本格式:comma ...
- expect简介和使用例子
expect简介和使用例子 expect简介 expect是一款自动化的脚本解释型的工具. expect基于tcl脚本,expect脚本的运行需要tcl的支持. expect对一些需要交互输入的命 ...
- Linux expect详解
随处可见的expect第一次见expect这个命令还是我第一次参加全量上线的时候,那是公司的一个牛人用Shell脚本写的一套自动部署.MD5 比对.发布的全量上线工具,没事的时候,看了下其中的几个脚本 ...
- expect脚本远程登录、远程执行命令和脚本传参简单用法
expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...
- Linux下的expect
expect简介 expect是一款自动化的脚本解释型的工具. expect基于tcl脚本,expect脚本的运行需要tcl的支持. expect对一些需要交互输入的命令很有帮助,比如ssh ftp ...
- 000_linux之Ubuntu安装
今天2018/6/1 今天是六一儿童节,天气凉爽,心情挺好的.然后本着开开心心的心情,将前面忘记写linux的Ubuntu没安装的写一下,以后自己回来看就很方便了.使用的是白问网制作的ubuntu,假 ...
- 很有用的shell脚本
基础知识 expect基础知识 exp_continue是匹配一行后,从当前expect块第一行开始匹配 expect块的每一行匹配后,直接退出当前expect块,往下一个expect块开始匹配 ex ...
随机推荐
- Spring Boot 构建一个 RESTful Web Service
1 项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...
- UGA,PGA
tom认为UGA不包含 sort工作区,所以下面的图都是错误的 The UGA is, in effect, your sessions state. It is memory that your ...
- win 10 如何关闭自动更新
1.右键“此电脑”图标,点击“管理”,打开“计算机管理”窗口; 2.在“计算机管理”窗口中找到“服务和应用程序”,点击“服务”,打开“服务”窗口; 3.在“服务”中找到“Windows Update” ...
- 小程序中 function (res)的理解
刚看到小程序里面一段代码 success: function (res) { console.log('搜索结果:'); console.log(res); wx.hideToast(); if (r ...
- helpera64开机bootlogo-BUG
环境: HelperA64开发板 Linux3.10内核 时间:2019.01.12 目标:修改开机bootlogo的BUG 问题: 1.24bit深度的bootlogo.bmp图片会导致Qt5有色差 ...
- ie的盒模型和标准模型
使用 box-sizing:content-box || border-box || inherit 原理图 计算 怪异模型|IE模型 div宽度(定死) = 内容宽度+border宽度+paddin ...
- GC错误
如果出现GC错误,可设置客户端 set mapreduce.map.java.opts 设置一下 R的GC错误,在顶端设置这个参数 options(java.parameters = "-X ...
- postgresql 安装插件
最近由于工作需要,学习了citusdata 插件,在按照官方文档装好postgresql 之后,不能在psql 中正常启用其它插件 如 : "create extension pg_trg ...
- Reflow & Repaint
http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ http://segmentfault.com/a/1190000002 ...
- 20+ Docs and Guides for Front-end Developers (No. 5)
It’s that time again to choose the tool or technology that we want to brush up on. If you feel like ...