zoukankan      html  css  js  c++  java
  • expect脚本中,变量的写法

     

    一、expect相关命令

    命令 作用
    send 用于向进程发送字符串
    expect 从进程接收字符串
    spawn 启动新的进程
    interact 允许用户交互

    二、expect简单样例

    #!/usr/tcl/bin/expect

    set timeout 20
    set host "10.10.10.100"
    set username "root"
    set password "123456"

    spawn ssh $username@$host
    expect "*password*" {send "$password "}
    interact #执行完后保持交互状态,把控制权交给控制台,就可以手工操作了。

    三、shell脚本和expect脚本的结合样例

    shell脚本中定义时间变量的写法:
    time=`date "+%Y%m%d"`

    ==》》
    直接照搬到expect中,设置的变量是不生效的:
    set time `date "+%Y%m%d"`
    这样的写法是错误的,无法识别。

    ==》》
    应该这样写:
    set time [exec date +%Y%m%d]
    才是正确的写法。

    将expect加入到shell脚本后 set 定义的变量会失效;
    解决办法 :将变量提到expect之外定义

    #!/bin/bash
    time=`date "+%Y%m%d"`
    expect << EOF
    cd /xxx
    spawn scp user@ipaddr:/ /
    set timeout 300
    expect "assword:"
    send "passwd "
    expect "sftp>"
    send "cd dir "
    expect "sftp>"
    send "get *admin*.$time* "
    sleep 5
    expect "sftp>"
    sleep 5
    #set timeout 300
    EOF

  • 相关阅读:
    入栈的方式
    出栈的方式
    入栈的方式
    累加数据段中的前3个字型数据
    累加数据段中的前3个字型数据
    出栈的方式
    入栈的方式
    python中如何清空列表
    python中统计列表元素出现的次数
    python中删除列表元素
  • 原文地址:https://www.cnblogs.com/yihr/p/7652616.html
Copyright © 2011-2022 走看看