zoukankan      html  css  js  c++  java
  • shell项目-分发系统-expect讲解

    shell项目-分发系统-expect讲解

    yum install -y expect

    1. 自动远程登录

    #! /usr/bin/expect
    set host "192.168.133.132"
    set passwd "123456"
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes
    "; exp_continue}
    "assword:" { send "$passwd
    " }
    }
    interact
    

    2. 自动远程登录后,执行命令并退出

    #!/usr/bin/expect
    set user "root"
    set passwd "123456"
    spawn ssh $user@192.168.133.132
    
    expect {
    "yes/no" { send "yes
    "; exp_continue}
    "password:" { send "$passwd
    " }
    }
    expect "]*"
    send "touch /tmp/12.txt
    "
    expect "]*"
    send "echo 1212 > /tmp/12.txt
    "
    expect "]*"
    send "exit
    "
    

    3. 传递参数

    #!/usr/bin/expect
    
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set passwd "123456"
    set cm [lindex $argv 2]
    spawn ssh $user@$host
    
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect "]*"
    send "$cm
    "
    expect "]*"
    send "exit
    "
    

    4. 自动同步文件

    #!/usr/bin/expect
    set passwd "123456"
    spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect eof
    

    5. 指定host和要同步的文件

    #!/usr/bin/expect
    set passwd "123456"
    set host [lindex $argv 0]
    set file [lindex $argv 1]
    spawn rsync -av $file root@$host:$file
    expect {
    "yes/no" { send "yes
    "}
    "password:" { send "$passwd
    " }
    }
    expect eof
    
  • 相关阅读:
    回溯 八皇后问题 与 0-1背包
    技术学习
    线程间通信
    计算机网络基础知识总结(各种协议)
    大数据Phoenix专题
    java中HashMap详解
    使用LocalBroadcastManager解决BroadcastReceiver安全问题
    Paint类常用方法
    链表结构对象池的一种实现
    android内存及内存溢出分析
  • 原文地址:https://www.cnblogs.com/cy-8593/p/9513081.html
Copyright © 2011-2022 走看看