expect

expect知识梳理

二次信任 提交于 2019-12-05 16:51:03
1 expect   expect软件用于实现非交互式操作,实际应用中常用于批量部署,可以帮助运维人员管理成千上万台服务器.   expect实现非交互式操作主要是在程序发出交互式询问时,按条件传递程序所需的字符串,如询问yes/no时自动传递yes或no,或询问密码时传递已定义好的密码,这样实现了非交互操作. 1.1 expect安装   推荐使用yum安装,方便快捷,自动解决依赖问题;如果手动编译,需同时安装expect和tcl软件.   安装省略,直接yum安装expect即可. 1.2 expect脚本中经常使用的命令及说明 命令 说明 set timeout NUM 设置超时时间,NUM是数字,单位是秒. expect 进入expect环境下 spawn expect软件中的监控程序,监控后面程序/命令发出的交互式询问, spawn是expect环境下的内部命令. send 发送已定义的字符串或数字密码给程序,完成交互式操作 "string\r" 表示发送string字符串后执行回车操作,程序之间的交互一般使用 "\r" "string\n" 使用"string\n"表示发送指定的字符串后,并输出内容到终端, 方便用户查看结果. exp_continue 若问题不存在则继续下一步,回答下面的问题. 注意此项不能加在expect发送自动应答中的最后一项,否则报错.

shell示例4

假如想象 提交于 2019-12-05 16:25:56
Table of Contents 编写脚本,接受二个位置参数,magedu 和/www,判断系统是否有 magedu,如果没有则自动创建 magedu 用户,并自动设置家目录为/www #!/bin/bash # 判断是否传入两个参数 [ $# -ne 2 ] && echo $# && exit 1 # 判断是否用户存在 if ! `id $1 &> /dev/null`;then useradd -d /www $1 else echo "user: $1 is existed!" fi 使用 expect 实现自动登录系统。 分析 确定登录信息 ip 端口 密码 发起 ssh 进程 应答 ssh 的登录提示 代码 #!/usr/bin/expect # 设置登录的变量 set IP 192.168.10.6 set PORT 22 set PASS 123456 # ssh登录并应答 spawn ssh root@$IP -p $PORT expect { "yes/no" { send "yes\n";exp_continue} "password" { send "$PASS\n"} } interact 来源: https://www.cnblogs.com/chaoyiyang/p/11933056.html

unittest参数化

让人想犯罪 __ 提交于 2019-12-05 05:09:56
import parameterized import unittest,BeautifulReport #数据驱动 data = [   ['admin','123456',True,'正常登录'],   ['admin','1122',False,'冻结用户登录'],   ['sdfsdf','1111',False,'黑名单用户登录'] ] data2 = [   ['admin','123456',True],   ['admin','1122',False],   ['sdfsdf','1111',False] ] def login(user,password):   if user=='admin' and password=='123456':     return True   return False class LoginTest(unittest.TestCase):  # 定义个类,继承unittest   @parameterized.parameterized.expand(data)    def test_login(self,user,password,expect,desc):     self._testMethodDoc = desc   # 自己指定     result = login(user,password)     self

expect utility is not working when executing from jenkins

走远了吗. 提交于 2019-12-05 04:16:56
we have a unix script which uses expect utility for interactive execution. This script works well when we run from unix server. If we run this script from Jenkins, it is not working. Below is the script var="xxxxx" expect -c " spawn sudo cp /abcd/sjws/config/obj.conf /abcd/sjws/config/obj.conf_jenkins expect { "Password:" { send $var\r;interact } } exit " Below is the output when we run from jenkins spawn sudo cp /abcd/sjws/config/obj.conf /abcd/sjws/config/obj.conf_jenkins Password: Password: Build step 'Execute shell' marked build as failure Finished: FAILURE Since Jenkins doesn't run it

Python之unittest参数化

坚强是说给别人听的谎言 提交于 2019-12-05 03:18:07
unittest如何做参数化呢? 我们在写case 的时候如果就是参数不同,我们只需要把参数都写到一个list里面, 然后循环去执行这个case即可。 unittest中有一个模块parameterized,可以帮我们循环,不需要自己写循环,直接pip install parameterized安装即可。 例如:一个登陆的case import parameterized import unittest,BeautifulReport data = [ ['admin','123456',True], #第三列表示预期结果 ['admin','1122',False], ['sdfsdf','1111',False] ] data1 = [ ['admin','123456',True,'正常登陆'], #第四列表示描述 ['admin','1122',False,'冻结用户登陆'], ['sdfsdf','1111',False,'黑名单用户登陆'] ] def login(user,password): if user=='admin' and password=='123456': return True return False class LoginTest(unittest.TestCase): @parameterized.parameterized.expand

gets not waiting for user input in expect script

£可爱£侵袭症+ 提交于 2019-12-05 00:01:00
问题 When i try to run the following expect script, it just finishes running instead waiting for user input. Could someone tell me what i am doing wrong? #!/usr/bin/expect puts -nonewline stdout "Enter device id:" flush stdout gets stdin id puts -nonewline stdout "Enter device name:" flush stdout gets stdin name 回答1: Expect alters the Tcl gets command so that it doesn't wait for standard input; to read a line while waiting for it, you need to do this instead of gets stdin id : # Read input to

shell编程之Expect免交互

点点圈 提交于 2019-12-04 23:55:51
shell编程之Expect免交互 一、前言 ​ shell脚本存在的核心意义就在于基于shell命令简化甚至省略可避免的人工操作,通过各种控制流程结构以及正则表达式等方法,逐步实现自动化操作的整个过程,由此也可见,shell并没有面向对象的思想,类似C语言,毕竟C语言是操作系统或者说是内核的核心语言。 ​ 所以,语言并无优劣之分,只是每个人使用的习惯与方式不同,换句话说,难易的不是语言,而是思想与突如其来的灵感。 二、Expect概述与安装 Expect概述 ​ Expect是建立在TCL基础上的一个工具,Expect是用来进行自动化控制和测试的工具。主要解决shell脚本中不可交互的问题。对于大规模的Linux运维很有帮助。 ​ 在Linux运维和开发中,我们经常需要远程登录服务器进行操作,登录的过程是一个交互过程,需要输入yes/no password等信息。为了模拟这种输入,可以使用Expect脚本。 Expect的安装:yum install -y expect 三、基本命令 send:向进程发送字符串,用于模拟用户的输入,但不支持换行 一般需要加上 \r expect:内部命令 ​ 判断上次输出结果里是否包含指定的字符串,有则返回,反之就等待超时时间后返回。 ​ 只能捕捉由spawn启动的进程的输出。 spawn:启动进程,并跟踪后续交互信息。 interact

Run cat on remote computer and send output a variable using expect

喜你入骨 提交于 2019-12-04 22:06:55
I have a bash+expect script which has to connect via ssh to the remote comp (and i can't use ssh keys, need password identification in here), read the file there, find specific line with the "hostname" (like "hostname aaaa1111") and store this hostname into the variable to be used after while. How can i get the value of the "hostname" parameter? I thought that line content will be in $expect_out(buffer) variable (so i can scan it and analyze), but it's not. My script is: #!/bin/bash ----bash part---- /usr/bin/expect << ENDOFEXPECT spawn bash -c "ssh root@$IP" expect "password:" send "xxxx\r"

expect交互脚本

只愿长相守 提交于 2019-12-04 21:11:27
expect是一个免费的编程工具语言,用来实现自动和交互式任务通信的编程工具语言。 expect 安装 expect基础应用 toc expect 安装 源码包安装需要先 Tcl 库,因为expect是在 Tcl 基础上创建起来的,所以这里我就不用源码包安装了,直接用yum安装 ## expect 在base源,这里我用的是阿里云的YUM源,有base源可以忽略 [root@Shell ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@Shell ~]# yum makecache ## 用yum安装expect [root@Shell ~]# yum install expect -y expect基础应用 expect脚本一般以 #!/usr/bin/expect 开头 expect脚本常以.exp或者.ex结束。 expect主要命令 spawn 新建一个由expect控制的进程 expect 等待接受进程返回的字符串,直到超时时间 send 发送字符串给expect控制的进程 set 设定变量 exp_continue 重新从当前expect块的开始执行 [lindex 获 取 脚 本 的 第 个 参 数 , 相 当 于 位 置 变

TCL_REGEXP::How to grep words from tcl variable and put into a text file, seperated with comma?

家住魔仙堡 提交于 2019-12-04 21:09:34
set line { Jul 24 21:06:40 2014: %AUTH-6-INFO: login[1765]: user 'admin' on 'pts/1' logged Jul 24 21:05:15 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.58.net. Flow: 0x2 Jul 24 21:04:39 2014: %DATAPLANE-5-: Unrecognized HTTP URL static.58.com. Flow: Jul 24 21:04:38 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.google-analytics.com. Flow: 0x2265394048. Jul 24 21:04:36 2014: %DATAPLANE-5-: Unrecognized HTTP URL track.58.co.in. Flow: 0 Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.google.co.in. Flow: 0x87078800 Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client