#################################################

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 基础的更多相关文章

  1. expect基础及实例

    expect基础及实例 http://blog.csdn.net/zhuying_linux/article/details/6900805

  2. Liunx之基础学习

    用户提权命令之-sudo sudo命令用来以其他身份来执行命令,预设的身份为root.在/etc/sudoers中设置了可执行sudo指令的用户.若其未经授权的用户企图使用sudo,则会发出警告的邮件 ...

  3. Liunx Shell入门

    本人也是初学习Liunx,如有错误请指出.Liunx版本:Ubuntu 14.04 一.Liunx命令基础 在Ubuntu下打开终端快捷键为:ctrl+Alt+T Liunx命令的基本格式:comma ...

  4. expect简介和使用例子

    expect简介和使用例子   expect简介 expect是一款自动化的脚本解释型的工具. expect基于tcl脚本,expect脚本的运行需要tcl的支持. expect对一些需要交互输入的命 ...

  5. Linux expect详解

    随处可见的expect第一次见expect这个命令还是我第一次参加全量上线的时候,那是公司的一个牛人用Shell脚本写的一套自动部署.MD5 比对.发布的全量上线工具,没事的时候,看了下其中的几个脚本 ...

  6. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...

  7. Linux下的expect

    expect简介 expect是一款自动化的脚本解释型的工具. expect基于tcl脚本,expect脚本的运行需要tcl的支持. expect对一些需要交互输入的命令很有帮助,比如ssh ftp ...

  8. 000_linux之Ubuntu安装

    今天2018/6/1 今天是六一儿童节,天气凉爽,心情挺好的.然后本着开开心心的心情,将前面忘记写linux的Ubuntu没安装的写一下,以后自己回来看就很方便了.使用的是白问网制作的ubuntu,假 ...

  9. 很有用的shell脚本

    基础知识 expect基础知识 exp_continue是匹配一行后,从当前expect块第一行开始匹配 expect块的每一行匹配后,直接退出当前expect块,往下一个expect块开始匹配 ex ...

随机推荐

  1. 指纹协查统计sql

     select dic.name, NVL(zc.zc_djzs,0),NVL(zc.zc_shzs,0),NVL(zc.zc_bzzs,0), NVL(zt.zt_djzs,0),NVL(zt.zt ...

  2. 关于换行这个动作,win 和 mac 的实现

    ‘\r'是回车,前者使光标到行首,(carriage return)'\n'是换行,后者使光标下移一格,(line feed) \r 是回车,return\n 是换行,newline 对于换行这个动作 ...

  3. Java 8-接口的默认方法和静态方法

    Java 8-接口的默认方法和静态方法 Java 8使用两个新概念扩展了接口的含义:默认方法和静态方法.默认方法使得接口有点类似traits,不过要实现的目标不一样.默认方法使得开发者可以在 不破坏二 ...

  4. [Oracle]记一次由sequence引发的enq sv-contention等待事件

    数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增,大约到了80个会话.通过查看EM的SQL信息,发现等待产生于SQL语句 se ...

  5. 1001. 温度转换 (Standard IO)

    1001. 温度转换 (Standard IO) 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   题目描述 将输入的华氏温度转换为摄氏温度.由华氏温度F与摄氏温度C的转换 ...

  6. putty登录出现access denied的解决办法

    [转]https://www.aliyun.com/jiaocheng/152659.html 在/etc/ssh/sshd_config 中有个 PermitRootLogin, 改成“Permit ...

  7. WebGl 二维纹理贴图(矩形)

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  8. golang学习总结

    目录 1. 初识go语言 1.1 Hello World 1.2 go 数据类型 布尔: 整型: 浮点型: 字符类型 字符串型: 复数类型: 1.3 变量常量 局部变量: 全局变量 常量 1.5 字符 ...

  9. HIVE基本语法以及HIVE分区

    HIVE小结 HIVE基本语法 HIVE和Mysql十分类似 建表规则 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name da ...

  10. Office 365部分安装及同时安装Visio的方法

    From MWeb Win版本的Office 365安装包默认安装所有组件,没有选择的页面,在安装Office 365后再安装下载的Visio 2016专业版时,会显示计算机上已经安装了即插即用Off ...