zoukankan      html  css  js  c++  java
  • expect

    expect 语法:
      expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
      选项
        -c:从命令行执行expect脚本,默认expect是交互地执行的
          示例:expect -c 'expect " " {send "pressed enter "}
        -d:可以输出输出调试信息
          示例:expect -d ssh.exp
      expect中相关命令
        spawn:启动新的进程
        send:用于向进程发送字符串
        expect:从进程接收字符串
        interact:允许用户交互
        exp_continue 匹配多个字符串在执行动作后加此命令

      expect最常用的语法(tcl语言:模式-动作)
      单一分支模式语法:
        expect “hi” {send “You said hi "}
        匹配到hi后,会输出“you said hi”,并换行
      多分支模式语法:
        expect "hi" { send "You said hi " }
        "hehe" { send “Hehe yourself " }
        "bye" { send “Good bye " }
        匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
        expect {
        "hi" { send "You said hi "}
        "hehe" { send "Hehe yourself "}
        "bye" { send “Good bye "}
        }

    #!/usr/bin/expect
    spawn scp /etc/fstab 192.168.8.100:/app
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send “magedu " }
    }
    expect eof

    #!/usr/bin/expect
    spawn ssh 192.168.8.100
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send “magedu " }
    }
    interact
    #expect eof

    示例:变量

    #!/usr/bin/expect
    set ip 192.168.8.100
    set user root
    set password magedu
    set timeout 10
    spawn ssh $user@$ip
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send "$password " }
    }
    interact

    示例:位置参数

    #!/usr/bin/expect
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    spawn ssh $user@$ip
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send "$password " }
    }
    interact
    #./ssh3.exp 192.168.8.100 root magedu

    示例:执行多个命令

    #!/usr/bin/expect
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    set timeout 10
    spawn ssh $user@$ip
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send "$password " }
    }
    expect "]#" { send "useradd haha " }
    expect "]#" { send "echo magedu |passwd --stdin haha " }
    send "exit "
    expect eof
    #./ssh4.exp 192.168.8.100 root magedu

     

     

    示例:shell脚本调用expect

    #!/bin/bash
    ip=$1
    user=$2
    password=$3
    expect <<EOF
    set timeout 10
    spawn ssh $user@$ip
    expect {
      "yes/no" { send "yes ";exp_continue }
      "password" { send "$password " }
    }
    expect "]#" { send "useradd hehe " }
    expect "]#" { send "echo magedu |passwd --stdin hehe " }
    expect "]#" { send "exit " }
    expect eof
    EOF
    #./ssh5.sh 192.168.8.100 root magedu

     

     

     

     

     

     

  • 相关阅读:
    面试官是如何筛选简历?
    成为一名架构师得学习哪些知识?
    一个对话让你明白架构师是做什么的?
    教你一招用 IDE 编程提升效率的骚操作!
    80个让你笑爆肚皮的程序员段子,不好笑算我输!
    Java初学者最佳的学习方法以及会遇到的坑(内含学习资料)!
    作为程序员的你,一年看几本技术相关的书
    MEF 插件式开发之 DotNetCore 中强大的 DI
    MEF 插件式开发之 DotNetCore 初体验
    读 《CSharp Coding Guidelines》有感
  • 原文地址:https://www.cnblogs.com/tony3154/p/10080117.html
Copyright © 2011-2022 走看看