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
    
  • 相关阅读:
    vim讲解
    tar常用解包
    linux扩展权限
    为Virtualbox中的Solaris10安装VBoxAdditions
    Solaris10下Telnet、SSH、ftp使用root登录
    linux软链接和硬链接
    curl命令学习(转载的)
    linux磁盘分区fdisk命令详解
    在服务器上排除问题的头五分钟
    Java对文件压缩/加密/解密/解压缩的例子,DES/RSA
  • 原文地址:https://www.cnblogs.com/cy-8593/p/9513081.html
Copyright © 2011-2022 走看看