expect

expect正则捕获返回结果

谁说我不能喝 提交于 2020-02-11 20:48:53
expect正则捕获返回结果 expect: expect -re "([0-9]*)([a-zA-Z]*)" send_user "num is $expect_out(1,string), string is $expect_out(1,string)" 这里[0-9]*表示一个或多个数字,[a-zA-Z]*表示多个字母。()用于分组,它们分别存放在$expect_out(1,string)和$expect_out(2,string)中。 pexpect:python中的expect child.expect("([0-9]*)([a-zA-Z]*) print "num is %s, string is %s" % (child.match.group(1),child.match.group(2)) 注意,pexpect 匹配字符串是从sendline的命令开始算的, 而不是命令返回结果开始。 比如,我现在执行pgrep ssh0,它的返回是ssh0的pid,如果我的expect re用"(\d+)",最后output匹配的结果是0, 其中child.before = ‘pgrep ssh’,child.after = '0' child.sendline (“pgrep shh0”) child.expect("(\d+)") output = child.match

Linux expect

筅森魡賤 提交于 2020-02-11 20:41:53
Linux expect Linux expect 使用简介 一.登陆到远程主机 脚本代码如下: ############################################## #!/usr/bin/expect set timeout 30 spawn ssh -l username hostip expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "hostpassword\r" } } interact ############################################## 1. #!/usr/bin/expect   这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash 2. set timeout 30   设置 脚本超时时间 3. spawn ssh -l username hostip   spawn是进入expect环境后才可以执行的expect内部命令,主要的功能是给ssh运行进程加个壳,用来传递交互指令。 4. expect {       "yes/no" { send "yes\r";exp_continue }       "password:" { send "hostpassword

Linux之expect命令免交互实现

夙愿已清 提交于 2020-02-11 05:09:37
expect命令,是基于tcl的一门语言,其内在原理和早期的chat一样; 是用于实现用户和程序之间的交互自动化进行的工具。 一、expect语法构成:   expect可单独写一个脚本:     #!/usr/bin/expect -f     set timeout 5     spawn COMAND     expect PATTERN0 {       send "STRING0\r"       exp_continue       }     expect {       PATTERN1 {           send "STRING1\r"           exp_continue         }       PATTERN2 {           send "STRING2\r"           exp_continue         }     }     expect "PATTERN3"     exp_send "STRING3\r"   以上混用了三种不同的expect书写格式;   其中,PATTERN默认只支持glob通配规则(*和?等),只需-re PATTERN 即可支持扩展正则表达式匹配了。   注意:所有{ } 的前面都需要一个空格隔开; 二、子命令解释:   set   创建变量,[lindex $argv 0]

Linux expect

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-11 05:08:44
  expect 是由 Don Libes 基于 Tcl 语言开发并广泛用于交互式操作和自动化测试场景中,通过 expect 可以让 shell脚本无需人为干预自动进行交互式通信。   expect 的核心功能是根据设定好的匹配形式,以执行相匹配的动作,进入自动化的人机交互。   以下以Ubantu上Demo做案例说明: 安装 expect sudo apt-get install expect 实例   以 SSH 登陆本机在$HOME下创建kitty文件夹作为例子,下面是 shell脚本源码。 #!/usr/bin/expect -fset timeout 30 set param1 [lindex $argv 0] spawn ssh -p 222 genter@127.0.0.1 expect { "(yes/no)?" {send "yes\r";exp_continue} } expect "password:" send "a123456\r" expect "@" send "cd /home/genter\r" expect "@" send "mkdir $param1\r" expect "@" send "exit\r" expect eof   [#!/usr/bin/expect -f]:告诉操作系统该脚本代码使用 expect 执行   [set

利用shell脚本提高效率(切换用户不需要输入密码)

强颜欢笑 提交于 2020-02-11 05:04:40
一、简便切换用户: 切换用户,每次输入密码,比较麻烦,可以使用脚本交互式登录实现,每次只要输入./su_ 并tab,执行此脚本,即可切换至root用户,比较简单。如下: hao@hao-ubuntu:~$ cat su_root.sh #!/usr/bin/expect set timeout 3 spawn su expect "Password:" exec sleep 1 send "root\r" expect "#" interact hao@hao-ubuntu:~$ ./su_root.sh spawn su Password: root@hao-ubuntu:/home/hao# 当然也可以增加一些if判断之类,让其他用户切换也不必输入密码。具体根据个人喜欢自己修改完善。 二、交互式登录: 通过expect,实现交互式登录,且看如下脚本: hao@hao-ubuntu:~$ cat login.sh #!/usr/bin/expect -f set ipaddr "192.168.77.58" set passwd "hao" spawn ssh hao@$ipaddr #spawn 意思是执行命令,expect内命令,shell中不存在 expect { "yes/no" { send "yes\r"; exp_continue} "password:" {

你学会测试了吗(3):测试语法之断言介绍

柔情痞子 提交于 2020-02-09 08:28:48
前言 这个简短的系列一一讲解一下.Net下测试的相关知识,希望对初学者有所帮助。 在这个系列 第一篇 中从测试工具入手推荐TestDriven.NET。官方下载TestDriven.NET-2.14.2190 Beta版 (直接下载) 和TestDriven.NET-2.13.2184正式版 (直接下载) 。 第二篇 中我选择了最为经典的NUnit单元测试框架来介绍TestDriven.NET所支持的一些重要的属性。这一篇继续使用这个框架,介绍单元测试的核心——断言Assert。 概述 在测试框架中,断言是单元测试的核心,我们在测试中要对其程序断言,如果某个断言失败,方法的调用不会返回值,并且会报告一个错误。如果一个测试包含多个断言,那些紧跟失败断言的那些断言都不会执行,因此每个测试方法最好只有一个断言。 下面看看NUnit框架吧,来2张图: 断言 现在,我们使用经典的NUnit框架的最新版本,可以用三种方法来写我们的断言: 标准模式 :过去比较经典的写法。这些方法在NUnit.Framework命名空间下的Assert类中以静态方法提供,对其不同的类型(字符串、集合、文件)NUnit.Framework框架还提供了字符串断言、集合断言、文件断言。 约束模式 :全新的写法,使用Assert.That()方法来约束扩展所有的断言,使用新增NUnit.Framework

Using #!/usr/bin/expect and bash

好久不见. 提交于 2020-02-08 08:28:35
问题 Is there any way I can use bash script along with /usr/bin/expect commands? Could I, for instance, use an if statement in a usr/bin/expect script? 回答1: expect is an extension of tcl - therefore you can use the programming constructs of tcl - these include 'if'. Tcl command man pages are available: tcl.tk/man/tcl8.5/TclCmd/contents.htm 回答2: Here is a very simple script that connects to a server via ssh from bash: #!/bin/bash /usr/bin/expect <<EOF spawn ssh user@server expect { "assword:" {

CallServerInterceptor拦截器分析

こ雲淡風輕ζ 提交于 2020-02-06 20:21:29
首先看调用接口intercept @Override public Response intercept(Chain chain) throws IOException { RealInterceptorChain realChain = (RealInterceptorChain) chain; //获取HttpCodec HttpCodec httpCodec = realChain.httpStream(); StreamAllocation streamAllocation = realChain.streamAllocation(); RealConnection connection = (RealConnection) realChain.connection(); Request request = realChain.request(); long sentRequestMillis = System.currentTimeMillis(); realChain.eventListener().requestHeadersStart(realChain.call()); //// 发送状态行和头部数据到服务端 httpCodec.writeRequestHeaders(request); realChain.eventListener()

Making decisions on expect return

喜你入骨 提交于 2020-02-06 11:13:59
问题 I am trying to create an expect script which will send a different password string based on the "expect" Condition A: If a cisco device has not been setup with a username then the first prompt will simply be "Password:" - then it should use passwordA (no username) Condition B: If it has been setup with a username then the prompt will be "Username:" followed by "Password:" - then it should use Username and PasswordB #!/bin/bash # Declare host variable as the input variable host=$1  # Start the

Making decisions on expect return

核能气质少年 提交于 2020-02-06 11:12:20
问题 I am trying to create an expect script which will send a different password string based on the "expect" Condition A: If a cisco device has not been setup with a username then the first prompt will simply be "Password:" - then it should use passwordA (no username) Condition B: If it has been setup with a username then the prompt will be "Username:" followed by "Password:" - then it should use Username and PasswordB #!/bin/bash # Declare host variable as the input variable host=$1  # Start the